Skip to content

Instantly share code, notes, and snippets.

@DocumentAlchemy
Created March 12, 2016 04:17
Show Gist options
  • Save DocumentAlchemy/a52dcecbbab862e8d005 to your computer and use it in GitHub Desktop.
Save DocumentAlchemy/a52dcecbbab862e8d005 to your computer and use it in GitHub Desktop.
Converts Word to PDF using https://documentalchemy.com/
#!/bin/bash
# docx2pdf <WORD-DOC> [<PDF-FILE>]
# Uses <https://documentalchemy.com/> to convert an MS Word document to PDF.
# SET YOUR DOCUMENT ALCHEMY API KEY HERE (OR PASS AS AN ENV VAR).
# SIGN UP AT <https://documentalchemy.com/> TO GET YOUR FREE API KEY.
DA_API_KEY=${DA_API_KEY:-"EDs2UQEprGn9aD4vg6HCPhFvgQFDahQgRzzIfocJ"}
# PRINT USAGE MESSAGE
show_help() {
echo ""
echo "Use: docx2pdf <WORD-DOC> [<PDF-FILE>]"
echo ""
echo "Set DA_API_KEY to your DocumentAlchemy API key value."
echo "(Currently \"$DA_API_KEY\".)"
echo ""
echo "For example:"
echo " docx2pdf myDocument.docx myDocument.pdf"
echo "or"
echo " DA_API_KEY=myApiKey docx2pdf myDocument.docx myDocument.pdf"
echo ""
}
# SANITY CHECK FOR COMMAND LINE PARAMETERS AND --help.
if [ "$#" -eq "0" ]; then
show_help
exit 1;
elif [[ $1 =~ ^--?(h(elp)?)|\?$ ]]; then
show_help
exit 0;
fi
# PARSE COMMAND LINE ARGUMENTS
SOURCE_FILE=$1
DEST_FILE=${2:-$SOURCE_FILE.pdf}
# EXECUTE REQUEST
if [ -s "$SOURCE_FILE" ]; then
curl -X POST --form "document=@$SOURCE_FILE" -H "Authorization: da.key=$DA_API_KEY" https://documentalchemy.com/api/v1/document/-/rendition/pdf -o "$DEST_FILE"
else
echo "File \"$SOURCE_FILE\" not found."
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment