Convert Jupyter notebooks to PDF


Jupyter lab is the next-generation web-based UI experience for Jupyter notebook users. It facilitates a tab-based programming interface that is highly extensible and interactive. It supports 40+ programming languages. We have already discussed how we can use Jupyter notebooks for interactive data analysis with SQL Server. With the help of Jupyter notebooks, we can keep headings, comments, code, output, and advanced charts and visuals in a single document in an orderly fashion. It helps Data Scientists and Data Analysts to have highly interactive presentations. In case you have already installed Jupyter notebooks and want to know how we can change the home directory for Jupyter notebooks, visit the blog “Change Jupyter Notebook startup folder on Windows and Mac OS “. Let’s discuss how we can Convert Jupyter notebooks to PDF documents directly from the web-browser or using nbconvert command from command prompt.

Convert Jupyter notebooks to PDF

During this post, we will consider Windows as the underlying operating system. However, this method should also work for macOS with a little tweak. To export/convert the Jupyter notebook into a PDF file using the below methods.

Method 1: From web-browser

To convert a Jupyter notebook into a PDF file from a web browser (running Jupyter Lab), click on File ->Export Notebook As… -> Export Notebook to PDF.

Export notebook to PDF

Export notebook to PDF

Method 2: Using Command prompt

To convert a notebook located at D:\JupyterFiles named as DataAnalysisReport.ipynb using the command prompt utility, use below command:

jupyter nbconvert D:\JupyterFiles\DataAnalysisReport.ipynb –to pdf

However, if we have not installed Latex and/or pandoc on our machine, we will get an error message which is as below:

500: Internal server error

500: Internal server error

Fix 500: Internal Server Error – Jupyter Notebook

Now, to fix this issue, we need to download and install Latex and Pandoc software on our machine and add their installation location entries to the Path environment variable of the machine. Let’s follow the below steps to fix the internal server error now.

Step 1: Download and install MikTex on windows

LaTeX is a document preparation system that is used for the production of technical and scientific documentation. It is used by nbconvert tool to convert the Jupyter notebooks into PDF files. LaTeX is the de facto standard for the communication and publication of scientific documents in the community. It is free software and can be downloaded and installed as free software on our machine. To get the LaTex for Windows, we need to download MiKTeX and install it on our machine. To download MiKTeX, visit this link and download the installer.

Once, we have downloaded the executable file, double click on it and follow the on-screen instructions with default values. It will install the MikTeX (LaTex) on our Windows machine.

Install MikTex

Install MikTex

Step 2: Download and install pandoc

Now, we need to install pandoc universal document converter tool, otherwise, we might get an error if we have used any HTML tag in the notebook. We can use pip command to install pandoc:

pip install pandoc

Or, we can download the installer and execute it to install pandoc on our machine. To download the pandoc, visit the pandoc official website. Click on the “Download the test installer for Windows” button to download the pandoc software.

Install pandoc

Install pandoc

Step 3: Add MikTex and Pandoc to the windows path environment variable

Once, we have downloaded both the software and installed them on our machine, we need to add the path of these executable in the Path variable. To do that:

  • Right-click on “This PC” and click on the Properties
  • In the left pane, click on the Advanced system settings. it will display the system properties dialog box.
  • Click on Environment variables at the bottom right.
  • In system variables, select the Path variable and click on the Edit button.
  • Add below entries to the end of the list:
    1. C:\Program Files\MiKTeX 2.9\miktex\bin\x64\
    2. C:\Users\adminuser\AppData\Local\Pandoc\pandoc.exe

Note: Replace these values with the appropriate folder location values you have on your machine for MikTex and pandoc installation.

Step 4: Execute nbconvert command to convert into PDF

jupyter nbconvert D:\JupyterFiles\DataAnalysisReport.ipynb –to pdf

It should fix the above issue and generate the PDF file from the specified notebook. Also, now the File ->Export Notebook As… -> Export Notebook to PDF command from the web-browser should work smoothly.

Thanks for the reading. Please share your inputs in the comment section.

Rate This
[Total: 4   Average: 4/5]

Gopal Krishna Ranjan

About Gopal Krishna Ranjan

Gopal is a passionate Data Engineer and Data Analyst. He has implemented many end to end solutions using Big Data, Machine Learning, OLAP, OLTP, and cloud technologies. He loves to share his experience at https://www.sqlrelease.com/. Connect with Gopal on LinkedIn at https://www.linkedin.com/in/ergkranjan/.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.