[pdf] IPython/Jupyter Problems saving notebook as PDF

So, i've been trying to save a jupyter notebook as PDF but i just can't figure out how to do this. The first thing i try is from the file menu just download as PDF, but doing that results in:

nbconvert failed: PDF creating failed

the next thing i try is try to do the conversion from the Command Prompt like this

$ ipython nbconvert --to latex --post PDF MyNotebook.ipynb 

but again, this results in an error message

ImportError: No module named 'PDF'

and if i try

$ ipython nbconvert --to latex MyNotebook.ipynb 

this results in

IPython.nbconvert.utils.pandoc.PandocMissing: Pandoc wasn't found:
Please check that pandoc is installed

if i try to install pandoc (pip install pandoc), this gives me

ImportError: No module named 'ConfigParser'

and this is where i get stuck because i just don't know what else to do. Anyone have idea how to fix whatever is wrong?

This question is related to pdf ipython jupyter

The answer is


If you are on a Mac and have Homebrew installed, open a terminal shell and install pandoc typing the command:

brew install pandoc

be patient, time to install and compile can take a while on slow internet connections or older systems.


2015-4-22: It looks like an IPython update means that --to pdf should be used instead of --to latex --post PDF. There is a related Github issue.


I am using Anaconda-Jupyter Notebook on OS: Ubuntu 16.0 for Python programming.

Install Nbconvert, Pandoc, and Tex:

Open a terminal and implement the following commands in it.

Install Nbconvert: though it's part of the Jupyter ecosystem still install it once again

$conda install nbconvert

Or

$pip install nbconvert

But I will recommend using conda instead pip if you are using anaconda

Install Pandoc: since Nbconvert uses Pandoc for converting markdown to formats other than HTML. Type following command

$sudo apt-get install pandoc

Install TeX: For converting to PDF, nbconvert uses the TeX. Type following command

$sudo apt-get install texlive-xetex

After execution of these commands, close the opened notebooks refresh the home page Or restart the kernel of the opened notebook. Now try to download notebook as a pdf :)

Note: For more details, please refer the official documentation:
https://nbconvert.readthedocs.io/en/latest/install.html


This problem was experienced with both Ubuntu and Mac OSX. After a frantic set of searches and trials, both of them were solved. This requires both tex and pandoc; both jumbo external programs cannot installed by Python's pip.

Mac OSX: using MacPorts installation of pandoc

port install pandoc

This should take nearly an hour to complete (in the usual case). If the problem persists, you might have to install MacTeX distro. of TeXLive.

For Ubuntu: install vanilla TeXLive from the network installer -- not through apt-get. Then install pandoc using apt-get.

sudo apt-get install pandoc

A complete installation of TeXLive would require a upto to 4.4 GB on disk.

To save all this trouble, the recommeded way to use IPython/Jupyter Notebook would be to install Anaconda Python distribution.


For converting any Jupyter notebook to PDF, please follow the below instructions:

(Be inside Jupyter notebook):

On Mac OS:

command + P --> you will get a print dialog box --> change destination as PDF --> Click print

On Windows:

Ctrl + P --> you will get a print dialog box --> change destination as PDF --> Click print

If the above steps doesn't generate full PDF of the Jupyter notebook (probably because Chrome, some times, don't print all the outputs because Jupyter make a scroll for big outputs),

Try performing below steps for removing the auto scroll in the menu:-

Credits: @ÂngeloPolotto

  1. In your Jupyter Notebook, click Cell on top of the jupyter notebook enter image description here

  2. Next click All output --> Toggle scrolling for removing auto scroll.

enter image description here


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.


To convert notebooks to PDF you first need to have nbconvert installed.

pip install nbconvert
# OR
conda install nbconvert

Next, if you aren't using Anaconda or haven't already, you must install pandoc either by following the instructions on their website or, on Linux, as follows:

sudo apt-get install pandoc

After that you need to have XeTex installed on your machine:

You can now navigate to the folder that holds your IPython Notebook and run the following command:

jupyter nbconvert --to pdf MyNotebook.ipynb

for further reference, please check out this link.


What I found was that the nbconvert/utils/pandoc.py had a code bug that resulted in the error for my machine. The code checks if pandoc is in your environmental variables path. For my machine the answer is no. However pandoc.exe is!

Solution was to add '.exe' to the code on line 69

if __version is None:
    if not which('pandoc.exe'):
        raise PandocMissing()

The same goes for 'xelatex' is not installed. Add to the file nbconvert/exporters/pdf.py on line 94

    cmd = which(command_list[0]+'.exe')

For Ubuntu users, an answer can be found here. I also quote it:

The most probable cause, is that you have not installed the appropriate dependencies. Your Ubuntu system has to have some packages installed regarding conversion of LaTeX and XeTeX files, in order to save your notebook as PDF. You can install them by:

sudo apt-get install texlive texlive-xetex texlive-generic-extra texlive-generic-recommended pandoc

Also, nbconvert is another dependency that is usually automatically installed with jupyter. But you can install it just to be sure, while having your virtual environment activated:

pip install -U nbconvert

As a brand new member, I was unable to simply add a comment on the post but I want to second that the solution offered by Phillip Schwartz worked for me. Hopefully people in a similar situation will try that path sooner with the emphasis. Not having page breaks was a frustrating problem for quite a while so I am grateful for the discussion above.

As Phillip Schwartz said: "You'll need to install wkhtmltopdf: [http://wkhtmltopdf.org/downloads.html][1]

and Nbconvert "

You then add a cell of the type "rawNBConvert" and include:

<p style="page-break-after:always;"></p>

That seemed to do the trick for me, and the generated PDF had the page break at the corresponding locations. You don't need to run the custom code though, as it seems the "normal" path of downloading the notebook as HTML, opening in browser, and printing to PDF works once those utilities are installed.


To make it work, I installed latex, typical latex extra, and pandoc.

With ubuntu:

sudo apt-get install texlive texlive-latex-extra pandoc

it takes some times: several 100 Mb to download. I read somewhere that you can use --no-install-recommends for texlive and extra to reduce to the dl.


I had all kinds of problems figuring this out as well. I don't know if it will provide exactly what you need, but I downloaded my notebook as an HTML file, then pulled it up in my Chrome browser, and then printed it as a PDF file, which I saved. It captured all my code, text and graphs. It was good enough for what I needed.


I had this error in Windows 10. I followed these three steps and it solved my problem:

  1. Install nbconvert

    pip install nbconvert

  2. Install pandoc

https://pandoc.org/installing.html

  1. Install miktex

https://miktex.org/download


Also it is good to update libraries:

pip install jupyter --upgrade
pip install --upgrade --user nbconvert

As the comments to the question say, you will need pandoc and latex (e.g. TeXShop). I installed pandoc with Homebrew, it took just a second. Having pandoc and TeXShop, I could generate latex but not pdf (on the command line).

ipython nbconvert --to latex mynotebook.ipynb

Exploring the latex (.tex) file with TeXShop, the failure was due to missing stylesheets and defs. After installing all of these (adjustbox.sty, adjcalc.sty, trimclip.sty, collectbox.sty, tc-pgf.def, ucs.sty, uni-global.def, utf8x.def, ucsencs.def), it did finally work.

However, the result looks a little too funky for my taste. It is too bad that printing the html from Safari loses the syntax coloring. Otherwise, it doesn't look so bad. (This is all on OS X).


In any system, the basic steps to correctly setup nbconvert to convert ipython notebooks to pdf/latex are

  1. Install nbconvert
  2. Install pandoc
  3. Install Texlive

Installing nbconvert

pip install nbconvert

or conda install nbconvert

Installing pandoc

sudo apt-get install pandoc for Ubuntu

or sudo yum install pandoc for CentOS

for others visit pandoc-installation

Installing texlive

You can install recommended packages or full install. For Ubuntu

sudo apt-get install texlive texlive-xetex texlive-generic-extra texlive-generic-recommended

`

For others and to full install texlive follow the instructions given at tug as per your system and choice.

I downloaded tar.gz file from tug-texlive-download and followed instructions given at TeX Live - Quick install. Installation instructions in summary:

  1. Clean up

    rm -rf /usr/local/texlive/2019

    rm -rf ~/.texlive2019

  2. Run installer

    unpack the zip file

    cd /your/unpacked/directory

    perl install-tl

    Enter command: i

  3. Setting path

    sudo vi /etc/bash.bashrc and insert

    PATH=/usr/local/texlive/2019/bin/x86_64-linux:$PATH; export PATH

    MANPATH=/usr/local/texlive/2019/texmf-dist/doc/man:$MANPATH; export MANPATH

    INFOPATH=/usr/local/texlive/2019/texmf-dist/doc/info:$INFOPATH; export INFOPATH

  4. Setting default papersize

    tlmgr paper letter

The commands may differ as per your system but the basic steps remains the same.


I had problems correctly displaying some symbols with regular download as pdf. So downloaded as tex jupyter nbconvert --to latex "my notebook.ipynb", made some tweaks with notepad (as an example, in my case I needed these lines for my language

\usepackage{tgpagella}
\usepackage[lithuanian,english]{babel}

) and then exported to pdf with latex --output-format=pdf "my notebook.tex".

But finally, however, to retain the same characters as you see in a browser I ended up using my Chrome browser printing: Ctrl+P Print to pdf. It adds unnecessary header and footer but everything else remains as it is. No more errors processing tqdm progress bar, no more code going out of the page and so on. Simple as that.


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


To convert .ipynb into pdf, your system should contain 2 components,

  1. nbconvert: Is part of jupyter allows to convert ipynb to pdf

    pip install nbconvert
    OR 
    conda install nbconvert
    
  2. XeTeX: Convert ipynb to .tex format and then converting to pdf.

    sudo apt-get install texlive-xetex
    

Then you can use below command to convert to pdf,

ipython nbconvert --to pdf YOURNOTEBOOK.ipynb

In case, it doesn't work, install pandoc and try again.

sudo apt-get install pandoc

This Python script has GUI to select with explorer a Ipython Notebook you want to convert to pdf. The approach with wkhtmltopdf is the only approach I found works well and provides high quality pdfs. Other approaches described here are problematic, syntax highlighting does not work or graphs are messed up.

You'll need to install wkhtmltopdf: http://wkhtmltopdf.org/downloads.html

and Nbconvert

pip install nbconvert
# OR
conda install nbconvert

Python script

# Script adapted from CloudCray
# Original Source: https://gist.github.com/CloudCray/994dd361dece0463f64a
# 2016--06-29
# This will create both an HTML and a PDF file

import subprocess
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename

WKHTMLTOPDF_PATH = "C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf"  # or wherever you keep it

def export_to_html(filename):
    cmd = 'ipython nbconvert --to html "{0}"'
    subprocess.call(cmd.format(filename), shell=True)
    return filename.replace(".ipynb", ".html")


def convert_to_pdf(filename):
    cmd = '"{0}" "{1}" "{2}"'.format(WKHTMLTOPDF_PATH, filename, filename.replace(".html", ".pdf"))
    subprocess.call(cmd, shell=True)
    return filename.replace(".html", ".pdf")


def export_to_pdf(filename):
    fn = export_to_html(filename)
    return convert_to_pdf(fn)

def main():
    print("Export IPython notebook to PDF")
    print("    Please select a notebook:")

    Tk().withdraw() # Starts in folder from which it is started, keep the root window from appearing 
    x = askopenfilename() # show an "Open" dialog box and return the path to the selected file
    x = str(x.split("/")[-1])

    print(x)

    if not x:
        print("No notebook selected.")
        return 0
    else:
        fn = export_to_pdf(x)
        print("File exported as:\n\t{0}".format(fn))
        return 1

main()

Examples related to pdf

ImageMagick security policy 'PDF' blocking conversion How to extract table as text from the PDF using Python? Extract a page from a pdf as a jpeg How can I read pdf in python? Generating a PDF file from React Components Extract Data from PDF and Add to Worksheet How to extract text from a PDF file? How to download PDF automatically using js? Download pdf file using jquery ajax Generate PDF from HTML using pdfMake in Angularjs

Examples related to ipython

How to increase image size of pandas.DataFrame.plot in jupyter notebook? Importing .py files in Google Colab Selection with .loc in python IOPub data rate exceeded in Jupyter notebook (when viewing image) Purpose of "%matplotlib inline" Installing a pip package from within a Jupyter Notebook not working convert json ipython notebook(.ipynb) to .py file In which conda environment is Jupyter executing? How to make inline plots in Jupyter Notebook larger? %matplotlib line magic causes SyntaxError in Python script

Examples related to jupyter

How to increase image size of pandas.DataFrame.plot in jupyter notebook? What is the difference between Jupyter Notebook and JupyterLab? Removing Conda environment How to open local file on Jupyter? Jupyter notebook not running code. Stuck on In [*] Can I run Keras model on gpu? IOPub data rate exceeded in Jupyter notebook (when viewing image) How to execute a * .PY file from a * .IPYNB file on the Jupyter notebook? how to make a new line in a jupyter markdown cell Running Jupyter via command line on Windows