How to Convert ipynb to PDF

Learn how to convert ipynb to PDF using Jupyter’s print menu, nbconvert, and Python scripts. Explore quick and customizable methods to export Jupyter Notebooks to PDF effortlessly.

So you’ve got a Jupyter Notebook, and you’re ready to share it with your colleagues. One problem, sending it over as an .ipynb is risky. Does your manager have the same version of Python, what packages does he have installed, can he install packages. Better send over a PDF instead.

Tried, tested and not exactly straightforward. Generating PDFs from a Jupyter notebook can be done in a few ways. Before you get started you’ll want to decide if you want to a quick and dirty PDF, or you’re willing to open up the terminal and write a quick script — also pretty quick, to be fair.

Option 1: Using the print menu in Jupyter

This method is fairly straightforward, you open your Notebook and then select print. Then when the print dialog appears, select “Save as PDF” under Destination.

Pros:

  • Quick as it gets.
  • Not additional code/packages required.

Cons:

  • Not all outputs are included.
  • Lack of customizability — can’t hide code cells.

Option 2: Installing nbconvert to Generate PDF from ipynb

This method is a bit more involved, but not much more than you’d think.

First, install nbconvert by running this command in your terminal:

pip install nbconvert[webpdf]

If you encounter an error, try:

pip install nbconvert\\[webpdf\\]

Once installed, you can convert your notebook by running:

jupyter nbconvert --to webpdf --allow-chromium-download YOUR_NOTEBOOK_NAME.ipynb

Replace YOUR_NOTEBOOK_NAME.ipynb with your actual file name.

Customizing the nbconvert Export

To create a PDF without the code cells, add the --no-input option:

jupyter nbconvert --to webpdf --allow-chromium-download --no-input YOUR_NOTEBOOK_NAME.ipynb

To execute the code cells in your Jupyter Notebook:

jupyter nbconvert --to webpdf --execute YOUR_NOTEBOOK_NAME.ipynb

Option 3: Creating a Script to Convert ipynb to PDF

Maybe opening the terminal every time you want to create a PDF is too much of a hassle. No worries, we can use nbconvert to create a script to create our PDF.

Let’s start by importing the packages we’ll use:

import nbformat
from nbconvert.exporters.webpdf import WebPDFExporter

Next, we’ll open the notebook:

with open("YOUR_NOTEBOOK.ipynb", "r", encoding="utf-8") as file:
	notebook_content = nbformat.read(file, as_version=4)

Next, we create and use the exporter:

pdf_exporter = WebPDFExporter(
	allow_chromium_download=True,
	exclude_input=False      
)

pdf_data, resources = pdf_exporter.from_notebook_node(notebook_content)

Finally, we can save the PDF:

with open("OUTPUT_PATH/YOUR.PDF", "wb") as file:
    file.write(pdf_data)

Here’s the full script, slightly expanded, and turned into a function:

import nbformat
from nbconvert.exporters.webpdf import WebPDFExporter

def convert_notebook_to_pdf(notebook_path, output_path=None, hide_code=False):
    """
    Convert a Jupyter notebook to PDF using nbconvert library

    Args:
        notebook_path: Path to the notebook file
        output_path: Path for the output PDF file (optional)
        hide_code: If True, code cells will be hidden in the output (optional)

    Returns:
        Path to the generated PDF file
    """
    # Check if file exists
    if not os.path.exists(notebook_path):
        raise FileNotFoundError(f"Notebook file '{notebook_path}' not found.")

    print(f"Converting notebook: {notebook_path} to PDF...")

    # Read the notebook
    with open(notebook_path, "r", encoding="utf-8") as file:
        notebook_content = nbformat.read(file, as_version=4)

    # Create the exporter with parameters
    pdf_exporter = WebPDFExporter(
        allow_chromium_download=True,
        exclude_input=hide_code  # This parameter hides code cells
    )

    # Export to PDF
    pdf_data, resources = pdf_exporter.from_notebook_node(notebook_content)

    # Determine output path
    if output_path is None:
        output_path = os.path.splitext(notebook_path)[0] + ".pdf"

    # Save the PDF
    with open(output_path, "wb") as file:
        file.write(pdf_data)

    print(f"✓ Conversion successful! PDF saved to: {output_path}")
    if hide_code:
        print("Note: Code cells have been hidden in the PDF.")
    return output_path

Wrapping up

Converting a Jupyter Notebook to a PDF doesn’t have to be a hassle. Whether you need a quick export using the print menu, a more structured approach with nbconvert, or a fully automated script, there’s a method to fit your workflow. If you just need a basic PDF, the print option is the easiest. For more control, nbconvert allows customization, including hiding code cells and executing notebooks before exporting. And if you find yourself frequently converting notebooks, automating the process with a script can save time in the long run.

No matter which method you choose, these options ensure that sharing your work with colleagues is as smooth and professional as possible—without worrying about dependencies or setup issues.

Subscribe to Transition from Excel to Python | Mito

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe