nbconvert: Pdf doesn't include images

When exporting to PDF it does not include images inserted via markdown

When using the markdown ![Description](pb1.png) I got this error:

LaTeX Warning: File `pb1.png' not found on input line 320.

! Unable to load picture or PDF file 'pb1.png'.
<to be read again> 
                   }
l.320 \includegraphics{pb1.png}
                               
? 
! Emergency stop.
<to be read again> 
                   }
l.320 \includegraphics{pb1.png}
                               
No pages of output.
Transcript written on notebook.log.

When using the image html tag <img src=\"pb1-bfs.png\"/> I got no image as an output

Everything is displayed correctly in the notebook for both case.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 42
  • Comments: 36 (8 by maintainers)

Most upvoted comments

Hi, I had the same issue, but I solved it by modifying the path of image file. Below is what I did:

Background:

  • Previously, I set ![ex 2.13](./image/ex2.13.png), the path is kind of relative path; and the notebook can not convert it to PDF

How I solved it:

  • I changed the format of the path to ![ex 2.13](image/ex2.13.png). Notice that I removed ./ which is to represent the current path.

My guess:

  • I did not check the source code and how it works, but I guess it append the path image/ex2.13.png to a ``base_dir’'. And if we used ./ at the head of path, it will raise errors.

In case it helps people out, a surprisingly works-great workaround for this issue is to use the Print Preview, then save that to a .pdf.

That’s:

  • File --> Print Preview
  • “Print” this preview in your browser, but really save it to a .pdf

Plots, images, everything, should be in there no problem

Another note: an additional benefit of this method is that is automatically handles text wrapping. The .pdf from latex does not do text wrapping in the code cells


Updated to prefer Print Preview rather than saving to .html. The latter may have issues embedding external images, but not the former

I fixed it by converting: <img src="loss.png" />

to: ![Loss](loss.png)

All the comments by @mpacer and @kangwonlee are spot on and were very helpful. I’m just adding this example because I missed those on my first pass. I’m hoping this will help somebody in the future if they are scanning this issue.

I think the issue here is that our PDF conversion produces latex in a temporary directory, and then runs xelatex on it there. It extracts images embedded in the notebook to a location where it can refer to them, but it doesn’t copy images referenced from Markdown cells to a matching location in the temporary directory, so the references to them in the generated Latex break.

See also #52.

This is still a problem in version 6.0.7. The workaround of converting to latex then building it myself is working fine though.

Could you try converting first to .tex (with the --to latex exporter invocation) and then manually converting to pdf using xelatex?

Do you run into the same error?

We don’t support pdf export from general HTML markup in markdown cells. I’m not sure if this is within or outside the purview of our intended coverage area.

nbconvert --to latex even does not detect figure place if it uses html syntax in markdown cell:

<img src='images/denoising.png' /> 

For anyone experiencing this issue I found what seems to be a workaround after none of the above did.

Download the notebook as an HTML file.

Move the HTML file to a directory where ALL of your pictures are and make sure you are just using either ./ or just the image name. Then open the HTML doc

Press Ctrl+p and save to PDF

I am facing the same issue anyone has a solution or workaround?

In my case I needed to use the html img tag so that I could set the size of the image using the width attribute, which can’t be done with the pure markdown syntax. However, I found a work-around that allowed the image size to be set and the image would be included in the output pdf. This is based on the solution by @tashrifbillah https://github.com/jupyter/nbconvert/issues/1079#issuecomment-1012601618 and this Stack Overflow answer:

  • display your image in its own code cell with an ipython Image object (which allows width and height to be set):
from IPython.display import display, Image
display(Image(filename="image_directory/my_image.png", width=700))
  • edit that cell’s metadata to add a tag indicating removing the cell’s input (so the above code does not get displayed in the final pdf, but the “output” image does):
    • in the notebook click “View” -> “Cell toolbar” -> “Edit Metadata” (this should now show an “Edit Metadata” button above each cell in the notebook)
    • Click the “Edit Metadata” button above the required cell.
    • In the “Edit Cell Metadata” window that pops up, add the JSON:
{
  "tags": [
    "remove_input"
  ]
}
  • convert the notebook to pdf on the command line using preprocessor tags:
jupyter nbconvert --to pdf MyNotebook.ipynb --TagRemovePreprocessor.remove_input_tags remove_input

The pdf should now contain the image!

@SebastianSager re 1 we got image export working using matplotlib image

Hi!

I am also using

jupyter nbconvert --to latex $name.ipynb

and “lose” the markup included images, e.g.,

<img width="75%" src="png/rosenbrock2.png"></img>

is not included at all in the tex document. What I did not get from the current discussion:

  1. Is there another way to include an image such that there would be an automatic conversion to LaTeX?
  2. Do you plan to include this in the future or not?
  3. If the answer to both questions is no: is there anything that would not display in markdown, but would still be exported to LaTex? html comments <!-- ... --> do not work, unfortunately, as they are also not exported.

I use the export on a large number of lecture scripts and have some sed-based postprocessing of the LaTeX files, so I would really appreciate some way to export the file information into the tex file (even if it were commented out, then I could include it myself via sed).

Thanks a lot, best regards

Edit: I found a way around the issue. Ugly, but at least it works. I add

$$ %imagePostProcess png/rosenbrock2.png $$

to my markup text and replace it then afterwards using sed:

sed -r -i 's/\\\[ %imagePostProcess (.+) \\\]/\n\n\\begin\{center\} \\adjustimage\{max size=\{0.9\\linewidth\}\{0.9\\paperheight\}\}\{..\/\1\} \\end\{center\} \{ \\hspace*\{\\fill\} \\\\\}/' $name.tex