将 Jupyter Notebook 转换为 PDF 时如何渲染图像?
我使用“nbconvert”和“notebook-as-pdf”模块将我的 Jupyter 笔记本转换为 PDF 文件,并在命令行中使用以下命令:
jupyter nbconvert --to PDFviaHTML <mynotebook.ipynb>
笔记本中显示的 Markdown 单元格中引用的图像(png 文件)不会在生成的 PDF 文件中呈现。 我使用以下方式引用 MD 单元格中的图像:

或
作为解决方法,我可以在“下载为...”菜单中将笔记本下载为 HTML。图像在生成的 HTML 文件中正确呈现(假定图像在正确的位置提供,此处为笔记本目录的“relativepath”子文件夹)。
有谁知道可行的解决方案? (我主要尝试使用 PDF via LaTex 转换解决方案,不幸的是在我的 M1 Mac 笔记本电脑上遇到了问题,这就是我首先使用 PDFviaHTML 解决方案的原因)。
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:

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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说开箱即用
使用 https://pypi.org/project/notebook- as-pdf/
安装使用 chromium,显然这是一个巧妙的技巧,可以确保 html 渲染用作 pdf 输入,而不是通过 Latex 进行更复杂的路线。我们的方法是“所见即所得”,这正是我们所需要的,我想这也是您提出问题的动机。
另请参阅 https://github 上关于如何将其合并到 ploomber 管道中的冗长讨论。 com/ploomber/ploomber/issues/658 和相应的问题 https://github.com/ploomber/ploomber/issues/675
可能只是大写/小写的方式
不起作用或者有类似的明显/简单的问题?
有关我们环境的更多详细信息:
计算机是用于测试的 Mac OS 机器和用于生产的 Ubuntu 20.04 LTS 或 miniconda docker 映像。
works out of the box for me with
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
does not work or something similar obvious/simple problem is in the way?
more details on our environment:
The computers are a Mac OS box for testing and Ubuntu 20.04 LTS or miniconda docker image for production.
我不完全确定你的意思
您可以尝试将 LaTeX 直接嵌入到 Markdown 单元中。
请注意,这不会显示在 markdown 单元格中(在笔记本中),并且可能会突出显示为错误(因为 markdown 不理解这一点),但是当您将文件直接导出为 PDF(而不是 HTML)时,图像将会呈现),假设您已经设置了 LaTeX。
您可以在此 文档。这是我经常使用的示例,
它将图像宽度设置为水平填充。
I am not entirely sure what you mean by
You could try embedding LaTeX directly into your markdown cell.
Please note that this will not show up within the markdown cell (in the notebook) and may be highlighted as an error (as markdown does not understand this), but the image will be rendered when you export the file to PDF directly (not HTML), given that you have LaTeX set up.
You can have a look at all the
<options>
available forincludegraphics
in this documentation. Here is an example of something I use quite often,which sets the image width to fill horizontally.