13
$\begingroup$

I have a Jupyter notebook that contains markdown, code, and outputs (graphs). I would like to generate PDF from this notebook.

I tried to hide code using HTML code which I get from here then I tried to download it as pdf but again code shows up. But when I download it as HTML it don't show any code but again when I tried to convert HTML to pdf it again shows code.

$\endgroup$

5 Answers 5

11
$\begingroup$

Try this:

jupyter nbconvert --to pdf --TemplateExporter.exclude_input=True  my_notebook.ipynb

This also works for html output. You will find the documentation for this and other options here.

FYI, for complex notebooks, this may generate errors depending on your version of nbconvert, LaTeX and other components. In that case try to convert to html then print to pdf using a browser.

$\endgroup$
1
  • $\begingroup$ It is good solution. but this approch break markdown like heading shows up as # heading etc. each markdown line contains some unwanted section number like 0.0.2. $\endgroup$ Jul 8, 2020 at 18:04
2
$\begingroup$

Make sure you are working with Qt Console (anaconda):

Install Jupiter extensions:

!pip install jupyter_contrib_nbextensions

!jupyter contrib nbextension install --user 

Enable nbextension:

!jupyter nbextension enable codefolding/main

Install pyppeteer:

!python -m pip install -U notebook-as-pdf

!pyppeteer-install

MAKE SURE YOUR WORKING DIRECTORY IS WHERE YOUR Untitled.ipynb FILE IS SAVED Save file to HTML format without codes:

!jupyter nbconvert Untitled.ipynb --no-input --no-prompt --to html

EXPORT TO PDF FORMAT:

!jupyter-nbconvert Untitled.ipynb --no-input --no-prompt --to pdfviahtml

PS. Exporting HTML and PDF format are mutually exclusive commands, you can use either.

$\endgroup$
2
  • $\begingroup$ This is exactly what I was looking for... No latex neccessary. Thank you $\endgroup$ Apr 29, 2022 at 15:00
  • $\begingroup$ Looks like this method no longer works because the last step (!jupyter-nbconvert Untitled.ipynb --no-input --no-prompt --to pdfviahtml) produces an error: PyPDF2.errors.DeprecationError: PdfFileWriter is deprecated and was removed in PyPDF2 3.0.0. Use PdfWriter instead. $\endgroup$
    – Christoph
    Nov 15 at 7:47
0
$\begingroup$

Make your program generate markdown output (you can save images and add references to them) and convert it using pandoc. Or don't convert, upload directly to Gitlab or Github and view.

$\endgroup$
0
$\begingroup$

Refer below link. It is the easiest and legitimate way of downloading jupyter notebook as pdf. No third party app or converter needed.

https://youtu.be/Q1J7MpI5PQk

$\endgroup$
3
  • 1
    $\begingroup$ please summarize the contents from the link to make the answer self-contained. link-only answers are discouraged. $\endgroup$
    – oW_
    Jun 16, 2022 at 16:06
  • $\begingroup$ As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    Jun 16, 2022 at 17:00
  • $\begingroup$ the linked video does not answer the question. It shows someone printing their notebook as a pdf, so the code is inevitably included. $\endgroup$
    – Christoph
    Nov 15 at 7:33
0
$\begingroup$

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

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.