14

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.

Ethan
  • 1,657
  • 9
  • 25
  • 39
GIRISH kuniyal
  • 263
  • 1
  • 2
  • 8

5 Answers5

12

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.

Zephyr
  • 997
  • 4
  • 11
  • 20
Donald S
  • 2,079
  • 3
  • 9
  • 28
2

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.

Pluviophile
  • 4,203
  • 14
  • 32
  • 56
0

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

0

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.

Valentas
  • 1,412
  • 1
  • 10
  • 22
-1

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