/ Hacks

Converting a docx document to PDF

For a new web application that I am working on, I needed to convert a docx document to PDF.

Pandoc seems to be the most preferred option to do this.

Installation: Head over to the download page as mentioned in the installation page.

To output to PDF on a Mac, LaTeX is needed. I just downloaded MacTeX as mentioned again in the installing page. Its 2GB but hey that's way better than pirating movies! :-)

Symlink pdflatex properly as mentioned here using the below line in the OSX terminal.

sudo ln -s /usr/texbin/pdflatex /usr/local/bin/

Check for the pdflatex version using:

pdflatex -v

Conversion: For conversion, lets use a sample markdown file as input to convert it .docx and then we will convert from .docx to .pdf.

# test document

wget https://gist.githubusercontent.com/georgiana-gligor/fd247d02f8a44ce745db/raw/86e56346ba5593525da7bf1d69cc97f09e00b496/osx-pdf-from-markdown.markdown

# create the docx file
pandoc -o test.docx osx-pdf-from-markdown.markdown

# create the pdf file
pandoc -o test.pdf test.docx

# of course you could do it directly too! More on why later!
pandoc -o test2.pdf osx-pdf-from-markdown.markdown