-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfcat
executable file
·52 lines (46 loc) · 2.83 KB
/
pdfcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# pdfcat uses PhantomJS to retrieve websites as PDFs, then catenates the PDFs
# into a single PDF with Ghostscript. Spooky!
#
# Usage: pdfcat https://scrty.io/start-here \
# https://medium.com/starting-up-security/starting-up-security-87839ab21bae \
# ... any number of URLs.
outputfile="merged.pdf"
tempdir="__pdfcat_temp__"
# Check for tools
if ! [ -x "$(command -v gs)" ]; then
blog error 'Ghostscript (gs) is not installed.' >&2
exit 1
fi
if ! [ -x "$(command -v phantomjs)" ]; then
blog info "Installing PhantomJS from Homebrew."
brew tap homebrew/cask
brew cask install phantomjs
fi
# Manage intermediates in temporary directory.
mkdir -p ${tempdir}
cd ${tempdir}
# PhantomJS's rasterization exmample, minified.
# https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js
echo "'use strict';var page = require('webpage').create(),system = require('system'),address, output, size, pageWidth, pageHeight;if (system.args.length < 3 || system.args.length > 5) {phantom.exit(1);} else {address = system.args[1];output = system.args[2];page.viewportSize = { width: 600, height: 600 };if (system.args.length > 3 && system.args[2].substr(-4) === '.pdf') {size = system.args[3].split('*');page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } : { format: system.args[3], orientation: 'portrait', margin: '1cm' };} else if (system.args.length > 3 && system.args[3].substr(-2) === 'px') {size = system.args[3].split('*');if (size.length === 2) {pageWidth = parseInt(size[0], 10);pageHeight = parseInt(size[1], 10);page.viewportSize = { width: pageWidth, height: pageHeight };page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight };} else {console.log('size:', system.args[3]);pageWidth = parseInt(system.args[3], 10);pageHeight = parseInt(pageWidth * 3/4, 10); console.log ('pageHeight:',pageHeight);page.viewportSize = { width: pageWidth, height: pageHeight };}}if (system.args.length > 4) {page.zoomFactor = system.args[4];}page.open(address, function (status) {if (status !== 'success') {console.log('Unable to load the address!');phantom.exit(1);} else {window.setTimeout(function () {page.render(output);phantom.exit();}, 200);}});}" > rasterize.js
# Fetch PDFs of each individual site.
COUNTER=0
for URL in "$@"; do
if [ ${URL: -4} == ".pdf" ]
then
blog info "${COUNTER}: downloading ${URL} as ${COUNTER}.pdf..."
wget --quiet -O "${COUNTER}.pdf" "${URL}"
blog info "Done!"
else
blog info "${COUNTER}: PDF-ing ${URL}... "
phantomjs rasterize.js "${URL}" "${COUNTER}.pdf" "Letter"
blog info "Done!"
fi
let COUNTER=COUNTER+1
done
blog info "Catenating PDFs. This might take a while...\n"
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="${outputfile}" $(ls *.pdf | sort -V)
# Clean up intermediates.
mv ${outputfile} ..
cd ..
rm -rf ${tempdir}