将 Jupyter Notebook 转换为 PDF 时如何渲染图像?

发布于 2025-01-15 12:24:36 字数 584 浏览 4 评论 0原文

我使用“nbconvert”和“notebook-as-pdf”模块将我的 Jupyter 笔记本转换为 PDF 文件,并在命令行中使用以下命令:

jupyter nbconvert --to PDFviaHTML <mynotebook.ipynb>

笔记本中显示的 Markdown 单元格中引用的图像(png 文件)不会在生成的 PDF 文件中呈现。 我使用以下方式引用 MD 单元格中的图像:

![sometext](relativepath/filename.png)

作为解决方法,我可以在“下载为...”菜单中将笔记本下载为 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:

![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).

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2025-01-22 12:24:36
jupyter nbconvert --to pdfviahtml notebook.ipynb

对我来说开箱即用

pip install notebook-as-pdf

使用 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

可能只是大写/小写的方式

PDFviaHTML

不起作用或者有类似的明显/简单的问题?

有关我们环境的更多详细信息:

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

计算机是用于测试的 Mac OS 机器和用于生产的 Ubuntu 20.04 LTS 或 miniconda docker 映像。

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.

薄凉少年不暖心 2025-01-22 12:24:36

我不完全确定你的意思

我主要尝试通过 LaTex 转换解决方案使用 PDF

您可以尝试将 LaTeX 直接嵌入到 Markdown 单元中。

$\includegraphics[<options>]{absolute/path/to/image}$

请注意,这不会显示在 markdown 单元格中(在笔记本中),并且可能会突出显示为错误(因为 markdown 不理解这一点),但是当您将文件直接导出为 PDF(而不是 HTML)时,图像将会呈现),假设您已经设置了 LaTeX。

您可以在此 文档。这是我经常使用的示例,

$\includegraphics[width=\linewidth]{absolute/path/to/image}$

它将图像宽度设置为水平填充。

I am not entirely sure what you mean by

I primarly tried to use the PDF via LaTex converting solution

You could try embedding LaTeX directly into your markdown cell.

$\includegraphics[<options>]{absolute/path/to/image}$

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 for includegraphics in this documentation. Here is an example of something I use quite often,

$\includegraphics[width=\linewidth]{absolute/path/to/image}$

which sets the image width to fill horizontally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文