Skip to content

Instantly share code, notes, and snippets.

@cobalamin
Last active February 17, 2019 15:23
Show Gist options
  • Save cobalamin/36a7b2e9bd8ceeb6cc09c876ea398bd4 to your computer and use it in GitHub Desktop.
Save cobalamin/36a7b2e9bd8ceeb6cc09c876ea398bd4 to your computer and use it in GitHub Desktop.
Download a PDF via `rclone` to the reMarkable

Overview

This is a script to grab a PDF file from any cloud path available to rclone and make it available on the reMarkable tablet.

Dependencies

This script depends on

  • rclone being installed and set up on the device
  • uuidgen being available, which can be installed via remarkable_entware

Installation

Install the dependencies mentioned above.

Copy grab_pdf.sh to your reMarkable device via SSH (I just put it in the root home folder), open an SSH session on the device and make the script executable with chmod +x grab_pdf.sh.

Usage

./grab_pdf.sh <rclone source path> [<visible name>]

Where <visible name> is optional and can be used to set the name that will be visible in the document list. If this option is not set, the visible name is simply set to the filename of the source path.


An example:

./grab_pdf.sh dropbox:/uni/differential-equations.pdf "Differential Equations"

(depends on dropbox being a defined remote of rclone.)

#!/bin/bash
set -e
# Extract some info from the given remote path
REMOTE_PATH="$1"
REMOTE_FILENAME="$(basename $REMOTE_PATH)"
REMOTE_EXTENSION="${REMOTE_FILENAME##*.}"
# Expect the filename given in the remote to end in .pdf
if [ "$REMOTE_EXTENSION" != "pdf" ]
then
echo "ERROR: Remote file does not end in .pdf! Quitting."
exit
fi
# Set the visible name to the second argument to the script,
# or if that's not given, to the remote filename
if [ -z "$2" ]
then
VISIBLE_NAME=$REMOTE_FILENAME
else
VISIBLE_NAME="$2"
fi
# Generate a UUID and locations for the local copy
UUID=$(uuidgen)
LOCAL_BASE="/home/root/.local/share/remarkable/xochitl/$UUID"
LOCAL_PATH="$LOCAL_BASE.pdf"
# Copy the file from the remote to the generated location
echo "Downloading the PDF..."
rclone copyto "$REMOTE_PATH" "$LOCAL_PATH"
echo "Success."
# Generate the .content file contents
PAGECOUNT=$(strings < "$LOCAL_PATH" | sed -n 's|.*/Count -\{0,1\}\([0-9]\{1,\}\).*|\1|p' | sort -rn | head -n 1)
CONTENT=$(cat <<EOF
{
"fileType": "pdf",
"fontName": "",
"lastOpenedPage": 1,
"lineHeight": -1,
"margins": 100,
"orientation": "portrait",
"pageCount": $PAGECOUNT,
"textScale": 1,
"transform": {
"m11": 1,
"m12": 0,
"m13": 0,
"m21": 0,
"m22": 1,
"m23": 0,
"m31": 0,
"m32": 0,
"m33": 1
}
}
EOF
)
# Generate the .metadata file contents
LAST_MODIFIED=$(date +"%s000" -r $LOCAL_PATH | cut -b1-13)
METADATA=$(cat <<EOF
{
"deleted": false,
"lastModified": "$LAST_MODIFIED",
"metadatamodified": false,
"modified": false,
"parent": "",
"pinned": false,
"synced": false,
"type": "DocumentType",
"version": 1,
"visibleName": "$VISIBLE_NAME"
}
EOF
)
# Write the .content and .metadata files
echo "Generating .metadata and .content files..."
echo "$CONTENT" > "$LOCAL_BASE.content"
echo "$METADATA" > "$LOCAL_BASE.metadata"
echo "Success."
# Restart xochitl to make it notice the new document
echo "Restarting xochitl..."
systemctl restart xochitl
echo "Success. All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment