While working on my Jupyter Notebook, I sometimes want to share my processed data set — complete with the plot and markdown explanation I created — in a readable format. Luckily, there’s an easy way to convert Jupyter Notebooks into PDF files. As an example, I’ll convert the notebook below to a PDF using Python and pip.
How Do You Convert a Jupyter Notebook to PDF?
- Install the notebook-as-pdf package by running this code:
pip install -U notebook-as-pdf
. - Run this code:
pyppeteer-install
. - Download the file as a PDF.
Here’s the Jupyter Notebook in its original format.
And here it is as a PDF.
Now, here’s how you too can convert your notebook in a few simple steps.
3 Steps to Convert Your Jupyter Notebook to PDF
1. Install the notebook-as-pdf Package Using pip
First, install the necessary package. For this example, I’m installing the package notebook-as-pdf
.
Run the following code in your command prompt to install the package.
pip install -U notebook-as-pdf
2. Run the pyppeteer-install Code
You’ll also need a setup for Chromium
to perform the HTML to PDF conversion, which can be done by using pyppeteer-install
.
After installing notebook-as-pdf
, run this code in your code prompt:
pyppeteer-install
3. Open the Notebook and Download as PDF via HTML
Now, open the Jupyter notebook you want to convert to a PDF. In your notebook, click the file menu bar, select “Download as,” then select “PDF via HTML.”
Just like that, your notebook is a PDF file.
If you’d rather use a command prompt to convert the notebook, you can do so with the following code.
jupyter-nbconvert --to PDFviaHTML example.ipynb
Because I titled the example Jupyter Notebook example.ipynb
, the result is titled example.pdf
.
This is everything you need to know to convert your Jupyter Notebook into a PDF file. It’s simple, but it’ll prove useful in the long run.
Frequently Asked Questions
How do I convert a Jupyter notebook to a PDF?
To convert a Jupyter notebook to a PDF:
- Install the notebook-as-pdf package by running
pip install -U notebook-as-pdf
in command prompt - Run the
pyppeteer-install
command (downloads Chromium and is used to convert the notebook to a PDF) - Open the Jupyter notebook and click “File” > “Download as” > “PDF via HTML”
How do I export a Jupyter notebook as a PDF?
To export a Jupyter notebook as a PDF, install the notebook-as-pdf
package and run the pyppeteer-install
command. Then, open the Jupyter notebook and click “File” > “Download as” > “PDF via HTML” to export it as a PDF.
What if I can't convert the Jupyter notebook to PDF?
If you can’t convert Jupyter notebook to PDF using the notebook-as-pdf
package, ensure the package is properly installed and the pyppeteer-install
command was properly run (this command downloads and sets up Chromium, which is needed to convert the notebook HTML to PDF format). If using Linux, make sure the necessary APT (advanced package tool) packages are also installed.