Have you ever wanted to export your Jupyter Notebook to PDF file? The PDF is an abbreviation for Portable Document Format. It can be displayed on any operating system. That makes it format of choice for many who wants to share their results. In this post I will show you 4 different ways how to export Jupyter Notebook as PDF file.
1. Print Jupyter Notebook to PDF
The most straightforward way is just to use web browser feature of print to PDF. There is no need to install any additional packages.
The big advantage of this approach is that we don’t need to install additional libraries to make it works! However, it is manual approach - hard to automate. What is more, we can’t control the process of export (for example, we can’t hide code in the exported file).
2. Download Jupyter Notebook as PDF
The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking
Download as ->
PDF via LaTeX (or
PDF via HTML - not visible in the screenshot).
This approach requires you to install some additional packages. For me, the option that exports with LaTeX is difficult. It requires you to install much more packages than option of export via HTML. If you don’t have required packages you will get the error message:
nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex.
This is scary, isn’t it? The option to export via HTML requires you to download Chromium (headless browser).
I rarely use this approach to be honest. It requires additional installation (that is not trivial) and is manual. This approach under the hood is using
3. Export to PDF with
That’s one of my favourite approaches. Why? Because it is a command line tool that can be used in the scripts :)
Still you need to install additional packages to make it work. The same as in Jupyter Notebook GUI there are two options:
- export to PDF via LaTex,
- export to PDF via HTML.
The latter is my choice. It requires
pyppeteer and Chromium to be download. This can be easily achieved by running:
pip install nbconvert[webpdf]
and adding a flag
--allow-chromium-download when converting a notebook:
jupyter nbconvert --to webpdf --allow-chromium-download your-notebook-file.ipynb
The flag should be added only one time. It is not necessary after Chromium installation. The
nbconvert has many optional arguments that control the export. For example, you can easily hide the code with
jupyter nbconvert --to webpdf --no-input your-notebook-file.ipynb
Additionally, you can apply more options to the export like removing selected cells or change the templates.
4. Share Notebook with Mercury
There is an open-source framework
Mercury that makes Python notebooks sharing painless. It converts notebooks to interactive documents (web apps, reports, slides, dashboards). You can share a notebook with interactive widgets. Your end-users can tweak widgets values and execute the notebook with new values (without changing the code). The
Mercury allows to export the executed notebook into standalone HTML or PDF file. The end-user just need to clik the
Saving notebooks to PDF is a great way to persist results in a shareble format. PDFs can be easily published online or send in the email. There are several ways to convert Jupyter Notebook as PDF. The automatic conversion can be easily achieved with
nbconvert tool. Notebooks shared with Mercury framework can be easily converted to PDF. The PDF notebook can be manually downloaded from the website.
Articles you might find interesing
💌 Join our newsletter 💌
Subscribe to our newsletter to receive product updates