Converting IPython notebooks to PDFs

Sun 04 January 2015 | tags: python

A few weeks ago I wanted to share an IPython notebook with a friend who did not have IPython installed. The natural choice was to convert it to a PDF, which turned out to be more painful than expected. The following instructions are written for Windows users and IPython v2.3. The only dependency is wkhtmltopdf (webkit HTML to PDF).

  1. In the notebook editor, export to HTML.
    a. File -> Download as -> HTML (.html)
    b. You will need to manually collect required images and place them in the correct relative path to the exported file or change src="..." paths in the HTML.
  2. If you use a custom.css file then either edit the exported HTML file to point to it or copy the file to the expected path.
    a. custom.css lives in ~/.ipython/profile_default/static/custom/
  3. Open the exported HTML file in your browser and save it (File -> Save As).
    a. While this step seems unnecessary, without it my MathJax markup was the wrong font size in the resulting PDF.
  4. Run wkhtmltopdf on the saved copy from the previous step.
    a. wkhtmltopdf.exe -s Letter -L 40mm -R 40mm -T 20mm -B 20mm --javascript-delay 5000 infile.html outfile.pdf

The -s argument sets the page size. The -L, -R, -T, -B arguments set the left/right/top/bottom margins. The --javascript-delay argument gives the MathJax javascript time to run (the time required probably depends on document size). There are many options available in wkhtmltopdf, which can be viewed by calling the executable from the command line without any arguments.

Comments !

social