David Yang
David Yang

Reputation: 2141

Jupyter Notebook save to PDF without Code

This has been an ongoing issue for me, as I would love to use Jupyter Notebook to write my research reports, but have found it very difficult to export my Jupyter Notebooks to PDF without code and without large formatting errors.

I am able to download the notebooks as PDF, but have not found a way to hide the code, or have the PDF resemble the formatting of the notebook.

No solution I've found on SO has been sufficient for my issue, so it may be possible that this is not the intended functionality of Jupyter Notebook.

Upvotes: 12

Views: 17992

Answers (4)

Sebastian
Sebastian

Reputation: 1

If you manually create the PDF, there is a simpler solution: add the following code block & execute it

from IPython.core.display import HTML
HTML('<style>.input, .jp-InputArea {display: none !important}</style>')

Code will disappear both in the "normal" UI and in print preview / export to HTML. Open print preview, generate the PDF - it looks great now, clear the output of this one cell to bring the code back, done

Upvotes: 0

Eduardo
Eduardo

Reputation: 1413

nbconvert

nbconvert is an official package, and other answers have mentioned it. However, they did not mention there are two ways to convert to PDF.

The default option uses LaTeX, and it's often a pain to install:

jupyter nbconvert notebook.ipynb --to=pdf

pdf

A newer option is to use the webpdf converter, this does not require LaTeX

jupyter nbconvert notebook.ipynb --to=webpdf

webpdf

Quarto

Quarto is the newest option. The defaults are prettier than nbconvert, and it has advanced features to customize the output PDF. However, it uses pandoc (and pandoc uses LaTeX), so you must install both first.

quarto render notebook.ipynb --to pdf

Quarto is a new project, and I often run into problems (I couldn't convert the sample notebook); however, the project is in active development and I'm sure it will get better.

Online converters

The last option is to use one of the many online services. The two main caveats are 1) You might not want to upload your notebooks to some random website, and 2) It's often unclear what engine they use to convert the notebooks. I've tried many of them with mostly negative results.

I created one online converter that uses nbconvert and deletes your notebook as soon as it's converted.

Upvotes: 2

user10023347
user10023347

Reputation: 141

I followed the suggestion by 0xffff above but the PDF output did not behave as intended (code blocks were still included).

However, their suggestion inspired me to try converting to html first using the following call: jupyter nbconvert path/to/your/ipynb --to=html --TemplateExporter.exclude_input=True

This behaved as intended, and from there, it was straightforward to print the output to PDF in a browser.

Regarding the issue with the --to=pdf flag, I've opened up an issue on the Jupyter Notebook git repo: https://github.com/jupyter/notebook/issues/3804. Will report back once I get a response.

Upvotes: 13

0xffff
0xffff

Reputation: 191

Have you tried this:

jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True

For more on the flags, you can refer to nbconvert config options

I have successfully converted .ipynb to .html exclude the code blocks

Upvotes: 19

Related Questions