ブログに戻る

Convert Jupyter Notebook to PDF in VS Code (Without the Headaches)

A complete guide to converting IPYNB to PDF in Visual Studio Code. Covers the built-in Jupyter extension, nbconvert integration, and how to avoid the LaTeX trap.

  • #vscode
  • #guide
  • #jupyter

VS Code has become the most popular editor for Jupyter Notebooks, thanks to the excellent Jupyter extension. But "Export to PDF" in VS Code has a dirty secret: by default, it shells out to nbconvert, which shells out to LaTeX, which — if you don't have it installed — fails with a cryptic error.

This guide walks through how to convert notebooks to PDF in VS Code without falling into the LaTeX trap.

Method 1 — The Built-in "Export to PDF" (And Why It Often Fails)

The Jupyter extension adds an "Export" button to the top of every .ipynb file. Click it, choose "PDF", and... you'll probably get an error like:

xelatex not found

or

Pandoc failed: command not found

This happens because the default export path runs nbconvert --to pdf, which requires a full LaTeX distribution (MacTeX, TeX Live, or MiKTeX — 2-4 GB).

If you want to make this work, install LaTeX:

  • macOS: brew install --cask mactex-no-gui
  • Ubuntu/Debian: sudo apt install texlive-xetex texlive-fonts-recommended texlive-latex-extra
  • Windows: Install MiKTeX

Then restart VS Code and try again.

But you probably don't want to install LaTeX just for this. Read on.

Method 2 — Configure VS Code to Use webpdf (Recommended)

VS Code's Jupyter extension supports nbconvert's webpdf exporter, which uses headless Chromium instead of LaTeX. Much smaller, much faster, no TeX dependency.

Setup

  1. Install the prerequisites in your terminal:

    pip install "nbconvert[webpdf]"
    playwright install chromium
    
  2. Open VS Code Settings (Cmd/Ctrl + ,).

  3. Search for jupyter.exportFormat. You can't change the default here, but you can override it per-export.

  4. Alternatively, set it in settings.json:

    {
      "jupyter.exportFormat": "webpdf"
    }
    

    Wait — this setting doesn't actually exist in current versions. The right way is below.

The Right Way

VS Code's Jupyter extension doesn't expose webpdf as a separate menu option in all versions. The reliable path:

  1. Open the Command Palette (Cmd/Ctrl + Shift + P).
  2. Search for Jupyter: Export to PDF.
  3. If it offers only "PDF" (LaTeX-based), you'll need to configure nbconvert directly.

Create a jupyter_nbconvert_config.py in your project or ~/.jupyter/:

c = get_config()
c.TemplateExporter.template_name = "lab"
# Default to webpdf when 'pdf' is requested
c.NbConvertApp.export_format = "webpdf"

Then run nbconvert from the VS Code integrated terminal:

jupyter nbconvert --to webpdf notebook.ipynb

Method 3 — HTML Export + Browser Print

The most reliable path in VS Code:

  1. Open the notebook in VS Code.
  2. Command Palette → Jupyter: Export to HTML.
  3. Save the HTML file.
  4. Open the HTML in Chrome/Firefox/Safari.
  5. Cmd/Ctrl + P → Save as PDF.

This bypasses nbconvert's PDF path entirely and uses the browser's print engine. The output quality is excellent, especially in Safari on macOS.

Method 4 — Use the Integrated Terminal

VS Code has a terminal built in (`Ctrl + ``). Run nbconvert directly:

# webpdf (no LaTeX)
jupyter nbconvert --to webpdf notebook.ipynb

# or HTML + browser print
jupyter nbconvert --to html notebook.ipynb && open notebook.html

# or LaTeX (if installed)
jupyter nbconvert --to pdf notebook.ipynb

This is the most flexible path — you get all of nbconvert's options (templates, preprocessors, cell filtering) without fighting the UI.

Method 5 — Use the Online Converter from VS Code

If you don't have Python locally or just want the fastest path:

  1. Right-click the .ipynb file in VS Code's Explorer.
  2. Choose "Reveal in Finder" (macOS) or "Reveal in Explorer" (Windows).
  3. Drag the file onto ipynbtopdf.org.
  4. Download the PDF ~3 seconds later.

This is genuinely the fastest path for one-off conversions, and works regardless of your Python environment.

VS Code-Specific Tips

Tip 1 — Set Up a Task for Conversion

Create .vscode/tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Convert to PDF (webpdf)",
      "type": "shell",
      "command": "jupyter nbconvert --to webpdf ${file} --output-dir ${fileDirname}",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": []
    }
  ]
}

Now Cmd/Ctrl + Shift + B converts the current notebook to PDF.

Tip 2 — Keyboard Shortcut for Export

Add to keybindings.json (Cmd/Ctrl + Shift + P → "Open Keyboard Shortcuts (JSON)"):

[
  {
    "key": "cmd+shift+e",
    "command": "jupyter.exportaspdf",
    "when": "resourceLangId == jupyter"
  }
]

Now Cmd+Shift+E triggers PDF export.

Tip 3 — Use VS Code's Built-in diff

After converting, you can diff two notebooks using VS Code's "Compare Selected":

  1. Right-click notebook A → "Select for Compare".
  2. Right-click notebook B → "Compare with Selected".

VS Code's Jupyter extension renders the diff cell-by-cell, which is dramatically more useful than a JSON diff.

Tip 4 — Virtual Environments

If you're using a virtual environment, make sure VS Code is using it:

  1. Command Palette → Python: Select Interpreter.
  2. Choose your venv.

VS Code's nbconvert integration will use the selected environment's nbconvert. If webpdf isn't working, you probably installed it in the wrong environment.

Common VS Code Errors

"NbConvert is not installed"

The Jupyter extension can't find nbconvert. Fix:

pip install nbconvert

Or, if you're using a conda environment:

conda install -c conda-forge nbconvert

Then restart VS Code.

"Playwright browser not found"

You installed nbconvert[webpdf] but forgot to install the browser:

playwright install chromium

On Linux, you may also need system dependencies:

playwright install chromium --with-deps

"Export to PDF" hangs forever

This usually means nbconvert is trying to use LaTeX and waiting for a missing package prompt (common on Windows with MiKTeX). Switch to webpdf or HTML export.

Output looks different from the editor

nbconvert's templates don't perfectly match VS Code's notebook rendering. Expect minor differences in:

  • Font choices
  • Code highlighting colors
  • Cell spacing

If exact-match output matters, use webpdf (which renders via the same Chromium engine VS Code uses for previews).

The Recommended Setup

For most VS Code users, this setup gives the best experience:

  1. Install:

    pip install jupyterlab nbconvert "nbconvert[webpdf]"
    playwright install chromium
    
  2. Add the tasks.json entry from Tip 1 above.

  3. Use Cmd/Ctrl + Shift + B to convert the current notebook.

  4. For one-off conversions, use ipynbtopdf.org — faster than any local method.

This setup avoids LaTeX entirely, works on every OS, and produces clean PDFs in seconds.

Conclusion

VS Code is a great notebook editor, but its built-in PDF export inherits nbconvert's LaTeX dependency. By configuring nbconvert to use webpdf, or by using HTML export + browser print, or by falling back to an online converter, you can skip the LaTeX headache entirely. Once you've set up a tasks.json shortcut, converting a notebook to PDF becomes a single keystroke.