Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pdf doesn't include images #552

Open
xNok opened this issue Mar 20, 2017 · 36 comments
Open

Pdf doesn't include images #552

xNok opened this issue Mar 20, 2017 · 36 comments
Labels
format:PDF pertains to exporting to the PDF format

Comments

@xNok
Copy link

xNok commented Mar 20, 2017

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.

@mpacer
Copy link
Member

mpacer commented Mar 20, 2017

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.

@takluyver
Copy link
Member

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.

@xNok
Copy link
Author

xNok commented Mar 22, 2017

@mpacer you are right once I get the latex file i can compile it manually with my latex compiler with no issues. I guess It confirms @takluyver explanation.

Why don't we have access to download as latex via the file menu? Is this intentional?

@mpacer
Copy link
Member

mpacer commented Mar 22, 2017 via email

@mpacer
Copy link
Member

mpacer commented Mar 22, 2017

@xNok So made a PR, but even if that is merged, I'd still recommend using the LaTeX converter from the command line because right now the download buttons aren't exactly emulating the functionality of the command line LaTeXExporter.

Specifically, the command line LaTeX exporter will create a directory of files to represent any of your outputs that need to be called from an external source (i.e., images which are included via \begin{figure}…\end{figure}). In order to do this in the browser, we need to return a zip file (not just a TeX file) that will contain both the .tex and the appropriate images. Right now that zipping utility doesn't seem to be being triggered for either the LaTeX or the Markdown exporter (didn't test others but I'm guessing its the same). There are utilities for creating that zip file, but they aren't being used right now; I'm not sure why.

This happens to be part of a more general project I'm going to be working on re: better in browser conversion capabilities. Keep an eye out for my PRs on the notebook repo in the coming weeks for updates.

edit: Corrected earlier mistakes

@mpacer
Copy link
Member

mpacer commented Mar 24, 2017

Correction — the LaTeX file will grab outputs and instead with download a zip if there are outputs. I just didn't realise that my test notebook for this didn't have any output images.

@jmarcoscosta
Copy link

What sintax should I use to include the images correctly into the notebooks? I've tried the latex suggestion, but it did not work...By the way, there was no command into the .tex file to include the image. (The images are correctly rendered if the notebook is opened on my browser)
I import images to my notebooks with this sintax:
If I use the image's URL instead of its path, the pdf output does not include the image as well...

@hagger98
Copy link

hagger98 commented Aug 9, 2017

Please see the following code of auto generated Latex file created by Jupyter Notebook (ipynb) file. You can open the .tex extension file in the editor and go to the line 32 or 33 where you will find the bold line and comment it out. Your problem will be partially solved.
Code:
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
% Set max figure width to be 80% of text width, for now hardcoded.
(%)\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=.8\maxwidth]{#1}}
After commenting it out, find the image attachment command in .tex file. When you find that lines where compiler stops usually. replace the line of:
\adjustmentbox
with:
\includegraphics[scale=0.5 or whatever you desire]{yourimage.png}

Hope it solves your problem, mine is solved and i generated pdf from latex including graphics.

@xiaoleihuang
Copy link

xiaoleihuang commented Feb 4, 2018

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.

@peteflorence
Copy link

peteflorence commented Feb 21, 2018

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

@paulperry
Copy link

@peteflorence Print Preview doesn't work for me for a full slide deck, it will only prepare a pdf of the slide being viewed. nbconvert would/should produce the full deck.

@kangwonlee
Copy link

In my case, I added a .png file using a website URL. Currently it does not work.

Hope my comment helps.

@KuangcongLiu
Copy link

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.

Thank you so much! Solved my issue by deleting "./"

@1uciusy
Copy link

1uciusy commented Mar 29, 2019

Hi, I succeed to include image when converting ipynb to pdf.

However, some funny thing showed up, the image doesn't belong where they're supposed to be.

Like, the picture I insert in the first markdown cell shows up in the next md cell or somewhere strange.

I don't quite know about latex, so I haven't consider manually edit the tex file, I wonder if there is any easy solution?

Much thanks

@t-makaro
Copy link
Contributor

t-makaro commented Apr 2, 2019

@1uciusy I think I have a solution: add

\usepackage{float}
\floatplacement{figure}{H}

somewhere to the top of the latex file, but after the \documentclass.

Ultimately, I think that issue is worth a bug report of it's own. I'll working on submitting some of my fixes some one of my other repos back to nbconvert and this will be one of them.

@t-makaro t-makaro added the format:PDF pertains to exporting to the PDF format label Aug 2, 2019
@mcclaassen
Copy link

Interestingly, if the latex file to be manually converted to a pdf is created by using the JupyterLab File>Export Notebook As... conversion. TeXworks will not find any embedded .png files.

I advise to always create the latex files in the command line as:

jupyter nbconvert --to latex notebook_name.ipynb

@nguyen14ck
Copy link

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

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

@laholmes
Copy link

laholmes commented Nov 7, 2019

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

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

see mpacer's comment on mar 20 2017 above, support for general html in markdown cells is not guaranteed

@SebastianSager
Copy link

SebastianSager commented Nov 8, 2019

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

@laholmes
Copy link

laholmes commented Nov 14, 2019

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

@bradgrimm
Copy link

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.

@MSeal
Copy link
Contributor

MSeal commented Jan 3, 2020

Some of theses issues are a real pain to fix in nbconvert. I'm planning to get some fixes in for the 6.0 release that will help with some, but no one's really spending the significant time to refactor and address some of the fundamental issues causing these (many aren't even in jupyter). If someone has some time and a will to dig into the topic I'll gladly help review / advise where I can / know what's wrong, but I and the other current maintainers are unlikely to find the time to solve all of these for some time.

@NilsNoreyson
Copy link

Hi,
I am also fighting with this issue, which, for me, roots in the relative/absolute path structures inside the provided source code. In my experience, classical file system approaches with the "file path based locating" algorithm will become more and more a problem. But alternatives a on the rise...
...Great ideas based on a somehow "contend based location system" for files, like IPFS, are a refreshing alternative to this. I would see myself interested in tackling this "Notebook - Latex - PDF - Image" issue, but I would like to know, if a IPFS based file scheme could be possibly added to Jupyter.

Since the browser support is very good for IPFS and beyond this "technical issue", thing will become much simpler to organize and allocate. I think this could be a great investment in the portability of notebooks in general, if for instance IPFS will get included.
I'm not having the full picture in mind yet, but a good gut feeling about this.

@MSeal
Copy link
Contributor

MSeal commented Feb 26, 2020

IPFS based file scheme is a pretty experimental concept, probably would be a challenge to adopt across jupyter projects as a whole. The underlying issue is with associated assets in different contexts. Filesystems have 60+ years of established precedent that we should still be trying to make usable. The main issue is with the dependent tools being so working directory constrained that they're inflexible to setting working directories / accessing relative file paths in a consistent manner. They make a lot of assumptions that don't hold up well for nbconvert's execution model.

@ghayth82
Copy link

ghayth82 commented Mar 4, 2020

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

@alelom
Copy link

alelom commented Mar 11, 2020

Same issue here. None of the solutions above works for me.

Also by doing this in a Markdown cell:

\begin{figure}[h!]
\includegraphics{myImage.png}
\end{figure}

the exported PDF does not show any image, but an empty blank space instead, by the look of it exactly of the size of the referenced image.

It seems they have an open issue for this:
#1079
open since August 2019.

@laikas123
Copy link

laikas123 commented Jan 16, 2021

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

@kjohnsen
Copy link

kjohnsen commented Feb 1, 2021

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

@acelere
Copy link

acelere commented Mar 3, 2021

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.

That did the trick for me too! Thanks for sharing!

@rosers90
Copy link

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.

This worked for me too. Thank you

@hosseink33
Copy link

hosseink33 commented Jan 22, 2022

For me it was the extra attachment it was creating even in the latex file. Try downloading the latex file. Your image latex code should look like this

\begin{figure}
	\centering
	\includegraphics{YOUR_IMAGE_NAME.jpeg}
	\caption{SOME_CAPTION.jpeg}
\end{figure}

For me it had an extra attachment: before the image name which I needed to remove to make it work!

@lcoulet
Copy link

lcoulet commented Jan 27, 2022

Hello, are there any news on this issue ?

@xingfanxia
Copy link

Any fix to this?

@Plinsboorg
Copy link

There are a few workarounds available:
#1326

TLDR: This should work when you export to pdf
![Alternative_text](folder_with_images/Picture.jpg)
*I use slash (artifacts/Picture.jpg) in my path because I use Windows. In the case of Linux backslash is needed.

And this should work with webPDF export (last comment)
#1079

@mattpitkin
Copy link

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 #1079 (comment) 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!

@normanius
Copy link

There may have been progress on that issue, see #1079. Let's hope the fix will be available in the next release of nbconvert. As of nbconvert-7.4.0, this problem was still present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format:PDF pertains to exporting to the PDF format
Projects
None yet
Development

No branches or pull requests