5

I convert my Jupyter Notebooks to PDF files with the 'nbconvert' and 'notebook-as-pdf' modules with the following on the command line:

jupyter nbconvert --to PDFviaHTML <mynotebook.ipynb>

The images(png files)referenced in Markdown cells that show in the notebook, won't be rendered in the generated PDF file. I reference the images in MD cells either with:

![sometext](relativepath/filename.png)

or

<img src="relativepath/filename.png">

As a workaround i can download the notebook as HTML within the Menu "Download as...". The images are properly rendered in the generated HTML file (given the images are provided at the right location, here 'relativepath' subfolder of the notebook's directory).

Does anyone know of a working solution? (I primarly tried to use the PDF via LaTex converting solution and unfortunately had issues with it on my M1 Mac Laptop, which is why I used the PDFviaHTML solution in the first place).

Rinkalaone
  • 86
  • 1
  • 5
  • I've found Reportlab to be a far simpler solution than nbconvert. https://www.reportlab.com/dev/docs/ – stever Mar 20 '22 at 18:55
  • In our case the conversion works out of the box with no problem. The images are preserved. May be --embed-images If this option is provided, embed images as base64 urls in the resulting HTML file. will change your behavior see https://nbconvert.readthedocs.io/en/latest/usage.html – Wolfgang Fahl Mar 22 '22 at 14:36
  • @WolfgangFahl can you please precisely display the command you use or the workflow to help reproduce? Note: I do not want an HTML resulting file since I want to produce a PDF. – Rinkalaone Mar 25 '22 at 11:43

1 Answers1

0
jupyter nbconvert --to pdfviahtml notebook.ipynb

works out of the box for me with

pip install notebook-as-pdf

using https://pypi.org/project/notebook-as-pdf/

The installation uses chromium and obviously this is a neat trick to make sure the html rendering is used as the pdf input instead of going the more elaborate route via latex. The approach is "what you see is what you get" and exactly what we needed and i guess is also your motivation to ask.

See also the lengthy discussion on how to get this incorporated into a ploomber pipeline at https://github.com/ploomber/ploomber/issues/658 and the corresponding issue https://github.com/ploomber/ploomber/issues/675

May be just the upper/lowercase way of

PDFviaHTML

does not work or something similar obvious/simple problem is in the way?

more details on our environment:

python --version
Python 3.9.10

jupyter --version
jupyter core     : 4.7.1
jupyter-notebook : 6.4.3
qtconsole        : 5.2.2
ipython          : 7.27.0
ipykernel        : 6.3.1
jupyter client   : 7.0.2
jupyter lab      : 3.3.2
nbconvert        : 5.6.1
ipywidgets       : 7.7.0
nbformat         : 5.1.3
traitlets        : 4.3.3

The computers are a Mac OS box for testing and Ubuntu 20.04 LTS or miniconda docker image for production.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • I never said things are not working for your :-) You asked: Does anyone know of a working solution? Yes i do - the out of the box solution in our environment. We get what you see is what you get including images. – Wolfgang Fahl Mar 26 '22 at 15:35
  • Quite a misunderstanding here: I never said I could not generate a PDF which I do with the notebook-as-pdf module myself (if you read my post carefully enough). My only issue is with the images in my notebook file, that are not rendered in the generated pdf file. If you can render images yourself, is there a specific step you are taking for this? This is what I was asking you and this would be very interesting to know. – Rinkalaone Mar 26 '22 at 15:41
  • No special step - it works out of the box as expected we use local and remote img urls and both work – Wolfgang Fahl Mar 26 '22 at 18:21
  • 2
    but how do you reference your images in the markdown cells?...I am suspecting you are not embedding your images in MD cells but in Code cells am I right?...this works all right but is not what I need. I need to embed images with Text in MD cells – Rinkalaone Mar 28 '22 at 12:18