0

The Jupyter ecosystem offers different ways to create a PDF, however, none suits me well so far – either because the process is buggy, because not all features are supported, or because it looks cluttered.

To narrow down the question: I generally like the output of pdfviahtml:

jupyter nbconvert --to pdfviahtml notebook.ipynb

Specifically, I like the math typesetting, the typesetting in general, the support of vector graphics (SVGs), the aesthetics of code blocks and also the custom format, (almost) single-page PDF.

The last function in particular contributes to readability, as longer code segments are displayed as a single block.

However, unfortunately, external images that are added to a Markdown cell are lost (see this issue). Also, it displays links to sections within the same notebooks. However, the links are not operational.

For me, the next best alternative is to convert the notebook to HTML (which also looks great), and to create a PDF by using the browser's printing / save as PDF feature.

jupyter nbconvert --to html notebook.ipynb
# + save HTML as PDF in browser

However, the result is a multi-page PDF that is not easy to read. Therefore the...

Question: Does anyone now how to create a custom-format / single-page PDF from a HTML page that maintains links and generally preserves the appearance? A solution directly using Jupyter is preferred, but the use of browser extensions or other tricks are also welcome.


Details: I explored the following methods with Jupyter and nbconvert

# 1: --to pdf: 
#   Pro: Generated using LaTeX
#   Con: Creates a multi-page PDF, and therefore looks cluttered
jupyter nbconvert --to pdf 01-signals-solutions.ipynb

# 2: --to pdfviahtml:
#   Pro: Nice code blocks, nice typesetting, also of equations 
#   Pro: Custom-format / single-page PDF
#   Con: Main problem: external images in Markdown cells are not included
#   
jupyter nbconvert --to pdfviahtml 01-signals-solutions.ipynb

# 3: --to latex + separate call to pdflatex or xelatex
#   Pro: Looks like a LaTeX dokument
#   Con: Looks similar to option 1
jupyter nbconvert --to latex 01-signals-solutions.ipynb

# 4: --to html + save as PDF
#   Pro: Looks great in HTML
#   Con: Features are not preserved when saved as PDF

# 5: --to webpdf
#   Similar as 2: --to pdfviahtml
#   Pro: Similar output as --to pdfviahtml,
#   Pro: Within-document links are operational
#   Con: Code-generate plots are often cropped
#   Con: External images in Markdown cells are lost
jupyter nbconvert --to html 01-signals-solutions.ipynb

# 6: --to PDFviaHTML
#   Similar as 2, if not same
jupyter nbconvert --to PDFviaHTML 01-signals-solutions.ipynb

I tested with Jupyter 1.0.0 and nbconvert 7.16.2, with Python 3.10 and 3.11.

2
  • An option: diagnose and fix the bugs of jupyter nbconvert --to pdfviahtml which are bothering you, or hire someone to fix them. Commented Mar 5, 2024 at 22:06
  • 1
    @KJ You solved my problem. "Save as PDF" in Opera creates a single-page PDF, and the external images are shown too. Thanks for the hint! Commented Mar 5, 2024 at 22:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.