问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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:
- Linux : TeX Live
- Mac : MacTex
- Windows : MikTex
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.
回答5:
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).
回答6:
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()
回答7:
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
回答8:
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
In your Jupyter Notebook, click Cell on top of the jupyter notebook
Next click All output --> Toggle scrolling for removing auto scroll.
回答9:
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.
回答10:
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.
回答11:
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.
回答12:
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.
回答13:
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')
回答14:
To convert .ipynb into pdf, your system should contain 2 components,
nbconvert: Is part of jupyter allows to convert ipynb to pdf
pip install nbconvert OR conda install nbconvertXeTeX: 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
回答15:
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 pandocAlso,
nbconvertis 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
回答16:
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.
回答17:
A quick, non-nbconvert, non-pandoc solution:
- File -> Download as -> HTML
- Convert html to pdf online
来源:https://stackoverflow.com/questions/29156653/ipython-jupyter-problems-saving-notebook-as-pdf