17 may
2019

How to convert IPython notebooks to PDF and HTML?

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

COMENTARIOS

root

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).

Matt

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.

AnneTheAgile

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. http://ipython.org/ipython-doc/rel-1.0.0/interactive/nbconvert.html $ 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' 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.

karel

  1. Convert the IPython notebook file to html.

    ipython nbconvert --to html notebook.ipynb  
    

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

    Update: For HTML output, you should now use jupyter in place of ipython:

    jupyter nbconvert --to html notebook.ipynb
    
  2. Convert the html file notebook.html into a pdf file called notebook.pdf. In Windows, Mac 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   
    

Lefteris Xris

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.

ralston3

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).

Ciro Santilli ??????996ICU????

Also pass the --execute flag to get the output

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: 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: How to run an .ipynb Jupyter Notebook from terminal?

For an HTML fragment without header: How to export an IPython notebook to HTML for a blog post?

Tested in Jupyter 4.4.0.

hlkw

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.

user9522821

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

Saurabh Jain

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 https://www.sejda.com/html-to-pdf 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 to https://www.sejda.com/html-to-pdf 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

Yogesh

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.

partizanos

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 * 
}

DEJA TU COMENTARIO

© 2017 website by Rubit Corporation