Quick Export your Jupyter Notebook to PDF
Sometimes when we are applying for jobs, they ask us to send our portfolio. But instead of sharing your Jupyter Notebook — which they might won’t read it as it requires more effort to open — what if I tell you there are some way to simply export your beloved notebook to PDFs?
Here we go!
Using online converter
Pretty straightforward, of course!
vertopal.com
I found this tool is very helpful and easy to use. It basically can convert picture, document, spreadsheet, presentation, and many more. Just go straight to their website vertopal.com, choose IPYNB as source input and PDF as target output. Then drop your file — or you can choose from Google Drive, Dropbox, or OneDrive but it will requires you to log in. Once it uploaded successfully, we can just click continue to convert. It might take a while, and finally we can download it as pdf version!
It translate our markdown and notebook cells as well correctly! Super ✨
Using nbconvert
Unsatisfied with the result? Don’t worry, nbconvert come to rescue!
Install nbconvert, Pandoc, and LaTeX
First of all, we need to install nbconvert package to our system using either pip or conda. Note that nbconvert use LaTeX to convert our notebook to pdf, so it is mandatory to install it as mentioned in Kessie’s Article.
In my findings, I need to install Pandoc and LaTeX. There are different OS-specific distribution for installing LaTeX, since I am using MacOS I need to install MacTeX (Windows user will need MikTex). If you are wondering am I on the right path downloading this large file — yes, you are on the right path. The file size is around 4.7GB that might take a while to download.
Once your download is complete, the next step is just to install it — as it format already in .pkg we can just double click on it — super easy!
Install template
As mentioned in Kessie’s Article too, the default output of nbconvert cut off the side of the page. Therefore, we need to install additional template. Here are the steps I did:
- Run
pip install``nb_pdf_template
- Create an empty folder named
latex
inside/usr/local/lib/python3.9/site-packages/nbconvert/templates
. - Run
python -m nb_pdf_template.install
- Open
latex
folder and make sure there are 2 files namedclassic.tplx
andclassicm.tplx
.
Time to convert!
Finally, run this command on your terminal.
jupyter nbconvert --to pdf <your_filename_here>
In my opinion, now our code is more readable than Vertopal’s pdf version — thanks to Tyler Makaro as creator of the nbconvert template. Personally, I like this version more than the first one. Super! ✨
Conclusion
We covered 2 ways to convert Jupyter Notebook to PDFs. Each produce different styles of pdf. The advantages of using online converter is you dont have to install anything on your machine, but sometimes there are some limitations on the result. In the other hand, you can get more flexibility (sometimes) by installing some stuffs on your machine like nbconvert. Although, maybe some of you doesn’t think nbconvert is a good way as it will takes our 4.7GB space just for installing LaTeX. But yeah, the decision is on your hand)
If you found this article is useful, please don’t forget to clap and follow me for more Data Science / Machine Learning contents. In case you are interested to read more, follow our medium Data Folks Indonesia and don’t forget join us Jakarata AI Research on Discord!