In this recipe, you'll learn how to export an IPython Notebook to a PDF file. There are two methods which you can try. If the first works for you, then great, but if it doesn't, then try Method 2.
Before attempting a conversion to PDF, add the following line to your ~/.jupyter/jupyter_nbconvert_config.py
file. You may need to create this file:
c.ExecutePreprocessor.timeout = 300
This will ensure that a PDF can be generated, and it won't timeout while running the notebook, which can happen, especially if you're importing a few million rows to run through an analysis. Depending on how much data your notebook needs to import before running, you may need to increase or decrease the timeout value.
Once everything is installed, change into the directory where your notebook file is and run the following command:
jupyter nbconvert --to pdf --execute 'name_of_notebook.ipynb'
This looks almost identical to the previous recipe with one exception—instead of using --to html
, we are using --to pdf
.
If you try this and it does not work, try Method 2.
If Method 1 fails due to library conflicts on your computer, you can use wkhmltopdf
, another open source command-line tool, to convert your notebook to a PDF.
First, install wkhtmltopdf by visiting http://wkhtmltopdf.org/index.html and downloading the version for your operating system.
Next, convert the notebook to HTML using the command that we saw in the previous recipe:
jupyter nbconvert –to html –execute 'name_of_notebook.ipynb'
After that, use wkhtmltopdf to convert the HTML file to a PDF file:
wkhtmltopdf name_of_notebook.html name_of_notebook.pdf