Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for dimensions of background pdf #12

Merged
merged 3 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ The following binaries are required running these tools:
* ssh
* scp
* convert or rsvg-convert
* optional: ghostscript and pdfinfo to account for original pdf dimensions

If you are using a Debian-based Linux system, the following command should
install all requirements:

sudo apt-get install python3 librsvg2-bin pdftk openssh-client
sudo apt-get install python3 librsvg2-bin pdftk openssh-client ghostscript

## rM2svg

Expand Down
29 changes: 27 additions & 2 deletions tools/exportNotebook
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# - convert (imagemagick)
# - pdftk (pdftk)
# - rsvg-convert (optional, to avoid rasterizing of lines)
# - gs & pdfinfo (optional, to account for original pdf size)

if [[ $# -eq 0 ]] ; then
echo "Usage: ./exportNotebook (Partial)NotebookName AdditionalrM2svgArguments"
Expand Down Expand Up @@ -47,8 +48,32 @@ echo "Exporting notebook ${filename} ($(wc -l "${tmpfolder}"/*.pagedata | cut -d

if [ -f "${tmpfolder}"/*.pdf ]
then
ln -s "${tmpfolder}/"*.pdf "${tmpfolder}/background_original.pdf"
echo "Found underlying document PDF, using as background."
cp "${tmpfolder}/"*.pdf "${tmpfolder}/background.pdf"

if command -v "gs" > /dev/null && command -v "pdfinfo" > /dev/null
then
# Read PDF dimensions for scale correction
size=$(pdfinfo ${tmpfolder}/background_original.pdf | grep "Page size" | awk '{print $3,$5}')
width=$(echo ${size} | cut -f1 -d " ")
height=$(echo ${size} | cut -f2 -d " ")

# Calculate new width and necessary offset (rM dimensions: 1404x1872)
new_width=$(echo "scale=5; ${height} / 1872 * 1404" | bc)
offset=$(echo "scale=5; ${new_width} - ${width}" | bc)

echo "Original PDF dimensions are (${width}x${height}), correcting by offset of ${offset} to fit rM foreground."

# Add offset to background.pdf to match foreground dimensions
gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=${tmpfolder}/background_with_offset.pdf \
-dDEVICEWIDTHPOINTS=${new_width} -dDEVICEHEIGHTPOINTS=${height} -dFIXEDMEDIA \
-c "{${offset} 0 translate}" \
-f "${tmpfolder}/background_original.pdf"

ln -s ${tmpfolder}/background_with_offset.pdf ${tmpfolder}/background.pdf
else
ln -s ${tmpfolder}/background_original.pdf ${tmpfolder}/background.pdf
fi
else
# Getting template files
sort -u "${tmpfolder}"/*.pagedata | while read -r tpl; do
Expand All @@ -73,7 +98,7 @@ fi
filename=$(basename -s .pdf ${filename//\"/})

# Use multistamp instead of multibackground to preserve transparency
pdftk "${tmpfolder}"/foreground.pdf multistamp "${tmpfolder}"/background.pdf output "${filename}.pdf"
pdftk "${tmpfolder}"/background.pdf multistamp "${tmpfolder}"/foreground.pdf output "${filename}.pdf"

filesize=$(ls -la "${filename}.pdf" | awk '{print $5}' | numfmt --to=iec-i --suffix=B --format="%.3f")
echo "Written ${filesize} to ${filename}.pdf"
Expand Down