How to convert IPython notebooks to PDF and HTML?

PythonJupyter NotebookIpython

Python Problem Overview


I want to convert my ipython-notebooks to print them, or simply send them in html format. I have noticed that there exists a tool to do that already, nbconvert. Although I have downloaded it, I have no idea how to convert the notebook, with nbconvert2.py since nbconvert says that it is deprecated. nbconvert2.py says that I need a profile to convert the notebook, what is it? Does there exist a documentation about this tool?

Python Solutions


Solution 1 - Python

If you have LaTeX installed you can download as PDF directly from Jupyter notebook with File -> Download as -> PDF via LaTeX (.pdf). Otherwise follow these two steps.

  1. For HTML output, you should now use Jupyter in place of IPython and select File -> Download as -> HTML (.html) or run the following command:

     jupyter nbconvert --to html notebook.ipynb  
    

    This will convert the Jupyter document file notebook.ipynb into the html output format.

    Google Colaboratory is Google's free Jupyter notebook environment that requires no setup and runs entirely in the cloud. If you are using Google Colab the commands are the same, but Google Colab only lets you download .ipynb or .py formats.

  2. Convert the html file notebook.html into a pdf file called notebook.pdf. In Windows, macOS (brew install wkhtmltodf) or Linux, install wkhtmltopdf. wkhtmltopdf is a command line utility to convert html to pdf using WebKit. You can download wkhtmltopdf from the linked webpage, or in many Linux distros it can be found in their repositories.

     wkhtmltopdf notebook.html notebook.pdf   
    

Original (now almost obsolete) revision: Convert the IPython notebook file to html.

ipython nbconvert --to html notebook.ipynb

Solution 2 - Python

Also pass the --execute flag to generate the output cells

jupyter nbconvert --execute --to html notebook.ipynb
jupyter nbconvert --execute --to pdf notebook.ipynb

The best practice is to keep the output out of the notebook for version control, see: https://stackoverflow.com/questions/18734739/using-ipython-notebooks-under-version-control

But then, if you don't pass --execute, the output won't be present in the HTML, see also: https://stackoverflow.com/questions/35545402/running-ipynb-from-terminal

For an HTML fragment without header: https://stackoverflow.com/questions/12502187/ipython-notebook-to-html-for-blog-post

Tested in Jupyter 4.4.0.

Solution 3 - Python

From the docs:

>If you want to provide others with a static HTML or PDF view of your notebook, use the Print button. This opens a static view of the document, which you can print to PDF using your operating system’s facilities, or save to a file with your web browser’s ‘Save’ option (note that typically, this will create both an html file and a directory called notebook_name_files next to it that contains all the necessary style information, so if you intend to share this, you must send the directory along with the main html file).

Solution 4 - Python

nbconvert is not yet fully replaced by nbconvert2, you can still use it if you wish, otherwise we would have removed the executable. It's just a warning that we do not bugfix nbconvert1 anymore.

The following should work :

./nbconvert.py --format=pdf yourfile.ipynb 

If you are on a IPython recent enough version, do not use print view, just use the the normal print dialog. Graph beeing cut in chrome is a known issue (Chrome does not respect some print css), and works much better with firefox, not all versions still.

As for nbconvert2, it still highly dev and docs need to be written.

Nbviewer use nbconvert2 so it's pretty decent with HTML.

List of current available profiles:

$ ls -l1 profile|cut -d. -f1

base_html
blogger_html
full_html
latex_base
latex_sphinx_base
latex_sphinx_howto
latex_sphinx_manual
markdown
python
reveal
rst

Give you the existing profiles. (You can create your own, cf future doc, ./nbconvert2.py --help-all should give you some option you can use in your profile.)

then

$ ./nbconvert2.py [profilename] --no-stdout --write=True <yourfile.ipynb>

And it should write your (tex) files as long as extracted figures in cwd. Yes I know this is not obvious, and it will probably change hence no doc...

The reason for that is that nbconvert2 will mainly be a python library where in pseudo code you can do :

 MyConverter = NBConverter(config=config)
 ipynb = read(ipynb_file)
 converted_files = MyConverter.convert(ipynb)
 for file in converted_files :
     write(file)

Entry point will come later, once the API is stabilized.

I'll just point out that @jdfreder (github profile) is working on tex/pdf/sphinx export and is the expert to generate PDF from ipynb file at the time of this writing.

Solution 5 - Python

  1. Save as HTML ;
  2. Ctrl + P ;
  3. Save as PDF.

Solution 6 - Python

> Only this answer would be useful to you if you have math, scientific formulae in your document. Even if you don't have them it works fine.

GUI way

  • open the jupyter notebook open the jupyter notebook

  • Go to Files > Download as > HTML or PDF via LaTeX Go to Files > Download as > HTML or PDF via LaTeX

  • Then check your Downloads folder for the file. PS: If LaTeX had any errors while compiling the PDF, it will fail. If this happens, download the HTML file and then use Web page to PDF tool or any other similar service to convert the HTML to PDF.

Command-Line way

  • Open the terminal
  • Navigate to the folder containing the jupyter notebook
  • type "jupyter nbconvert --to pdf your_jupyter_notebook.ipynb "

PS: If it fails, try Yogesh's answer.

Solution 7 - Python

If you are using sagemath cloud version, you can simply go to the left corner,
select File → Download as → Pdf via LaTeX (.pdf)
Check the screenshot if you want.

Screenshot Convert ipynb to pdf

If it dosn't work for any reason, you can try another way.
select File → Print Preview and then on the preview
right click → Print and then select save as pdf.

Solution 8 - Python

You can do it by 1st converting the notebook into HTML and then into PDF format:

Following steps I have implemented on: OS: Ubuntu, Anaconda-Jupyter notebook, Python 3

1 Save Notebook in HTML format:

  1. Start the jupyter notebook that you want to save in HTML format. First save the notebook properly so that HTML file will have a latest saved version of your code/notebook.

  2. Run the following command from the notebook itself:

    !jupyter nbconvert --to html your_notebook_name.ipynb

After execution will create HTML version of your notebook and will save it in the current working directory. You will see one html file will be added into the current directory with your_notebook_name.html name

(your_notebook_name.ipynb --> your_notebook_name.html).

2 Save html as PDF:

  1. Now open that your_notebook_name.html file (click on it). It will be opened in a new tab of your browser.
  2. Now go to print option. From here you can save this file in pdf file format.

Note that from print option we also have the flexibility of selecting a portion of a notebook to save in pdf format.

Solution 9 - Python

Easy Direct way Just

1-Click Ctrl + P while you inside Jupiter notebook

2-Save as PDF enter image description here

Solution 10 - Python

I find that the easiest method to convert a notebook which is on the web to pdf is to first view it on the web service nbviewer. You can then print it to a pdf file. If the notebook is on your local drive, then upload it to a github repository first and use its url for nbviewer.

Solution 11 - Python

For those who can't install wkhtmltopdf in their systems, one more method other than many already mentioned in the answers to this question is to simply download the file as an HTML file from the jupyter notebook, upload that to HTML to PDF, and download the converted pdf files from there.

Here you have your IPython notebook (.ipynb) converted to both PDF (.pdf) & HTML (.html) formats.

Solution 12 - Python

I can't get pdf to work yet. The docs imply I should be able to get it to work with latex, so maybe my latex is not working.

$ ipython --version
1.1.0
$ ipython nbconvert --to latex  --post PDF myfile.ipynb
[NbConvertApp] ...
    raise child_exception
    OSError: [Errno 2] No such file or directory
$ ipython nbconvert --to pdf myfile.ipynb
  [NbConvertApp] CRITICAL | Bad config encountered during initialization:
  [NbConvertApp] CRITICAL | The 'export_format' trait of a NbConvertApp instance must be any of ['custom', 'html', 'latex', 'markdown', 'python', 'rst', 'slides'] or None, but a value of u'pdf' <type 'unicode'> was specified.

However, HTML works great using 'slides', and it is beautiful!

$ ipython nbconvert --to slides myfile.ipynb
...
[NbConvertApp] Writing 215220 bytes to myfile.slides.html

Update 2014-11-07Fri.: The IPython v3 syntax differs, it is simpler:

$ ipython nbconvert --to PDF myfile.ipynb

In all cases, it appears that I was missing the library 'pdflatex'. I'm investigating that.

Solution 13 - Python

I've been searching for a way to save notebooks as html, since whenever I try to download as html with my new Jupyter installation, I always get a 500 : Internal Server Error The error was: nbconvert failed: validate() got an unexpected keyword argument 'relax_add_props' error. Oddly enough, I've found that downloading as html is as simple as:

  1. Left click in the notebook
  2. Click 'Save As...' in the dropdown menu
  3. Save accordingly

No print preview, no print, no nbconvert. Using Jupyter Version: 1.0.0. Just a suggestion to try (obviously not all setups are the same).

Solution 14 - Python

Other suggested approaches:

  1. Using the 'Print and then select save as pdf.' from your HTML file will result in loss of border edges, highlighting of syntax, trimming of plots etc.

  2. Some other libraries have shown to be broken when it comes to using obsolete versions.

Solution: A better, hassle-free option is to use an online converter which will convert the *.html version of your *.ipynb to *.pdf.

Steps:

  1. First, from your Jupyter notebook interface, convert your *.ipynb to *.html using:

>File > Download as > HTML(.html)

  1. Upload the newly created *.html file here and then select the option HTML to PDF.

  2. Your pdf file is now ready for download.

  3. You now have .ipynb, .html and .pdf files

Solution 15 - Python

I combined some answers above in inline python that u can add to ~/.bashrc or ~/.zshrc to compile and convert many notebooks to a single pdf file

function convert_notebooks(){
  # read anything on this folder that ends on ipynb and run pdf formatting for it  
  python -c 'import os; [os.system("jupyter nbconvert --to pdf " + f) for f in os.listdir (".") if f.endswith("ipynb")]'
  # to convert to pdf u must have installed latex and that means u have pdfjam installed
  pdfjam * 
}

Solution 16 - Python

The plain python version of partizanos's answer.

  • open Terminal (Linux, MacOS) or get to point where you can execute python files in Windows
  • Type the following code in a .py file (say tejas.py)
import os

[os.system("jupyter nbconvert --to pdf " + f) for f in os.listdir(".") if f.endswith("ipynb")]
  • Navigate to the folder containing the jupyter notebooks
  • Ensure that tejas.py is in the current folder. Copy it to the current folder if necessary.
  • type "python tejas.py"
  • Job done

Solution 17 - Python

notebook-as-pdfInstall

python -m pip install notebook-as-pdf pyppeteer-install

Use it

You can also use it with nbconvert:

jupyter-nbconvert --to PDFviaHTML filename.ipynb

which will create a file called filename.pdf.

or pip install notebook-as-pdf

create pdf from notebook jupyter-nbconvert-toPDFviaHTML

Solution 18 - Python

There is a library called "notebook as PDF" pip install notebook-as-pdf.

It converts the notebooks into pdf via HTML. once the library is installed, you can find 'PDF via HTML (.pdf)' in the 'Download as' options of the 'File' tab.

File>Download as>PDF via HTML(.pdf)

enter image description here

Using the Ctrl+P

  • option may not work properly, at times when there are many cells.

  • Also, the paging won't be proper as well. Making it much harder for the reader to read & understand.

I found this method very intuitive, fast and better compared with other options presented here.

Solution 19 - Python

The simplest way I think is 'Ctrl+P' > save as 'pdf'. That's it.

Solution 20 - Python

Your solutions are great, But I have an image file in a different folder. when converting to the HTML or ipynb. I can't get the image, I just have a text-only. Can anybody resolve this! Pleaseeeee!

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnunzio13nView Question on Stackoverflow
Solution 1 - PythonkarelView Answer on Stackoverflow
Solution 2 - PythonCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 3 - PythonrootView Answer on Stackoverflow
Solution 4 - PythonMattView Answer on Stackoverflow
Solution 5 - Pythonuser9522821View Answer on Stackoverflow
Solution 6 - PythonTejas ShettyView Answer on Stackoverflow
Solution 7 - PythonLefteris XrisView Answer on Stackoverflow
Solution 8 - PythonYogesh Awdhut GadadeView Answer on Stackoverflow
Solution 9 - PythonMohamed FathallahView Answer on Stackoverflow
Solution 10 - PythonhlkwView Answer on Stackoverflow
Solution 11 - PythonShayan ShafiqView Answer on Stackoverflow
Solution 12 - PythonAnneTheAgileView Answer on Stackoverflow
Solution 13 - Pythonsemore_1267View Answer on Stackoverflow
Solution 14 - PythonSaurabh JainView Answer on Stackoverflow
Solution 15 - PythonpartizanosView Answer on Stackoverflow
Solution 16 - PythonTejas ShettyView Answer on Stackoverflow
Solution 17 - PythonDhiren BirenView Answer on Stackoverflow
Solution 18 - PythonDeepak HarishView Answer on Stackoverflow
Solution 19 - Pythonuser11534866View Answer on Stackoverflow
Solution 20 - PythonAravind RView Answer on Stackoverflow