Skip to main content

Here is a small script (stackprinter) which uses Stack Printer from the command-line:

#!/bin/sh
type wkhtmltopdf || { echo >&2 "wkhtmltopdf command is required. Aborting."; exit 1; }
: ${1?"Usage: $0 (url)"}
URL=$1 && shift
HOST=$(echo "$URL" | grep -Eo "([.[:alnum:]]*?)\.(com|net|org)" | head -c-5)
QID=$(echo "$URL" | grep -o "[0-9].*/" | head -c-2)
PRINTURL="http://www.stackprinter.com/export?format=HTML&printer=true&service=$HOST&question=$QID"
FILENAME=$(curl -s "$URL" | grep -o "<title>[^<]*" | tail -c+8)
wkhtmltopdf $* "$PRINTURL" "$FILENAME.pdf" && echo "'$FILENAME.pdf' saved."

Usage:

./stackprinter (url)

Example (tested on OS X):

$ ./stackprinter http://stackapps.com/questions/179/stackprinter-the-stack-exchange-printer-suite
'app - StackPrinter: The Stack Exchange Printer Suite - Stack Apps.pdf' saved.
kenorb
  • 173
  • 1
  • 6