Reputation: 1505
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?
Upvotes: 134
Views: 516032
Reputation: 5839
There are 4 ways to export Jupyter Notebook to PDF format that I am aware of and described below.
Please open the notebook that you would like to export in Jupyter Notebook app, and select print from your web browser options. There should be an option Save as PDF
. It is the simplest approach.
There is an option to export PDF directly in the Jupyter Notebook. Click File
-> Download as
-> PDF via LaTeX
or PDF via HTML
. This solution is using nbconvert
tool under the hood. You need to have installed additional packages to make it works, otherwise you will get 500: Internal Server Error
. Please check the nbconvert
installation documentation for details.
nbconvert
The solution is similar to point 2 but is using the command-line tool nbconvert
. Assuming that all additional packages are installed the conversion is pretty simple:
jupyter nbconvert --to webpdf --allow-chromium-download your-notebook-file.ipynb
The --allow-chromium-download
is only needed during the first use.
There is an open-source framework Mercury
for sharing notebooks. It converts notebooks to interactive documents (can hide the code). In Mercury
the user can tweak widgets values and execute the notebook with new parameters. The resulting notebook can be downloaded as PDF (or HTML) file.
Upvotes: 4
Reputation: 1469
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.
Upvotes: 4
Reputation: 5850
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.
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.
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
Upvotes: 152
Reputation: 1486
Easy Direct way Just
1-Click Ctrl + P while you inside Jupiter notebook
Upvotes: 5
Reputation: 123
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)
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.
Upvotes: 3
Reputation: 382442
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: 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.
Upvotes: 37
Reputation: 715
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
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
PS: If it fails, try Yogesh's answer.
Upvotes: 10
Reputation: 10240
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.
Upvotes: 3
Reputation: 1000
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.
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.
Upvotes: 7
Reputation: 1702
Other suggested approaches:
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.
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:
File > Download as > HTML(.html)
Upload the newly created *.html file here and then select the option HTML to PDF.
Your pdf file is now ready for download.
You now have .ipynb, .html and .pdf files
Upvotes: 3
Reputation: 530
python -m pip install notebook-as-pdf pyppeteer-install
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
Upvotes: 3
Reputation: 715
The plain python version of partizanos's answer.
import os
[os.system("jupyter nbconvert --to pdf " + f) for f in os.listdir(".") if f.endswith("ipynb")]
Upvotes: 4
Reputation: 1126
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 *
}
Upvotes: 3
Reputation: 2708
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
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.
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
).
your_notebook_name.html
file (click on it). It will be opened in a new tab of your browser.Note that from print option we also have the flexibility of selecting a portion of a notebook to save in pdf format.
Upvotes: 6
Reputation: 41
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.
Upvotes: 4
Reputation: 1437
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:
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).
Upvotes: 3
Reputation: 80336
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).
Upvotes: 21
Reputation: 27833
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.
Upvotes: 15