Pandoc 转换为 pdf 不适用于 heroku

如何解决Pandoc 转换为 pdf 不适用于 heroku

我有一个 ruby​​ on rails 应用程序,它使用 pandoc-ruby 将 Markdown 文件转换为 pdf。 pandoc-ruby 需要安装 pandoc。要成功转换为 pdf,还需要存在 pdflatex。如果安装了 pandoctexlive-latex-recommendedtexlive-fonts-recommended 软件包,则本地(在 Mac 和 Ubuntu 18.04 上测试)一切正常。部署到 heroku 时,事情变得有点棘手。 为了在 heroku 上安装所有软件包,我使用了 Aptfile 方法,但我无法解决这个问题。


方法一:Aptfile

我已经指定了这个 Aptfile:

# Aptfile
texlive-latex-recommended
texlive-fonts-recommended
https://github.com/jgm/pandoc/releases/download/2.11.3.2/pandoc-2.11.3.2-1-amd64.deb

我得到的错误是:

2021-01-22T21:46:03.478937+00:00 app[web.1]: [6ac45926-62f3-48bf-98b5-8f7c1022a02f] RuntimeError (warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.
2021-01-22T21:46:03.478938+00:00 app[web.1]:
2021-01-22T21:46:03.478938+00:00 app[web.1]: kpathsea: Running mktexfmt pdflatex.fmt
2021-01-22T21:46:03.478940+00:00 app[web.1]: warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.
2021-01-22T21:46:03.478940+00:00 app[web.1]: /app/.apt/usr/bin/mktexfmt: kpsewhich -var-value=TEXMFROOT Failed,aborting early.
2021-01-22T21:46:03.478942+00:00 app[web.1]: BEGIN Failed--compilation aborted at /app/.apt/usr/bin/mktexfmt line 28.
2021-01-22T21:46:03.478942+00:00 app[web.1]: warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.
2021-01-22T21:46:03.478942+00:00 app[web.1]:
2021-01-22T21:46:03.478943+00:00 app[web.1]: kpathsea: Running mktexfmt pdflatex.fmt
2021-01-22T21:46:03.478943+00:00 app[web.1]: warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.
2021-01-22T21:46:03.478944+00:00 app[web.1]: /app/.apt/usr/bin/mktexfmt: kpsewhich -var-value=TEXMFROOT Failed,aborting early.
2021-01-22T21:46:03.478944+00:00 app[web.1]: BEGIN Failed--compilation aborted at /app/.apt/usr/bin/mktexfmt line 28.
2021-01-22T21:46:03.478945+00:00 app[web.1]: Error producing PDF.
2021-01-22T21:46:03.478947+00:00 app[web.1]: This is pdfTeX,Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) (preloaded format=pdflatex)
2021-01-22T21:46:03.478947+00:00 app[web.1]: I can't find the format file `pdflatex.fmt'!
2021-01-22T21:46:03.478947+00:00 app[web.1]:
2021-01-22T21:46:03.478948+00:00 app[web.1]: ):

我尝试使用 heroku run bash 来尝试弄清楚发生了什么。好像所有的包都安装好了,但是一个简单的 pandoc 命令失败了,看下面的输出

~ $ pandoc -v
pandoc 2.11.3.2
Compiled with pandoc-types 1.22,texmath 0.12.1,skylighting 0.10.2,citeproc 0.3.0.3,ipynb 0.1.0.1
User data directory: /app/.local/share/pandoc or /app/.pandoc
copyright (C) 2006-2020 John MacFarlane. Web:  https://pandoc.org
This is free software; see the source for copying conditions. There is no
warranty,not even for merchantability or fitness for a particular purpose.
~ $ pdflatex -v
pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian)
kpathsea version 6.3.1
copyright 2019 Han The Thanh (pdfTeX) et al.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters,see the file
named copYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.37; using libpng 1.6.37
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 4.01
~ $ pandoc test.md -o test.pdf
warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.

kpathsea: Running mktexfmt pdflatex.fmt
warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.
/app/.apt/usr/bin/mktexfmt: kpsewhich -var-value=TEXMFROOT Failed,aborting early.
BEGIN Failed--compilation aborted at /app/.apt/usr/bin/mktexfmt line 28.
warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.

kpathsea: Running mktexfmt pdflatex.fmt
warning: kpathsea: configuration file texmf.cnf not found in these directories: /etc/texmf/web2c:/usr/local/share/texmf/web2c:/usr/share/texmf/web2c:/usr/share/texlive/texmf-dist/web2c:/app/.apt/share/texmf/web2c.
/app/.apt/usr/bin/mktexfmt: kpsewhich -var-value=TEXMFROOT Failed,aborting early.
BEGIN Failed--compilation aborted at /app/.apt/usr/bin/mktexfmt line 28.
Error producing PDF.
This is pdfTeX,Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) (preloaded format=pdflatex)
I can't find the format file `pdflatex.fmt'!

方法 2:自定义构建包

我还尝试了 2 个用于 TeX Live 的自定义构建包,发现 herehere。 它看起来更有希望,但在转换为 pdf 时仍然会产生错误。请参阅下面的 heroku run bash 输出

~ $ pandoc -v
pandoc 2.11.3.2
Compiled with pandoc-types 1.22,not even for merchantability or fitness for a particular purpose.
~ $ pdflatex -v
pdfTeX 3.14159265-2.6-1.40.21 (TeX Live 2020)
kpathsea version 6.3.2
copyright 2020 Han The Thanh (pdfTeX) et al.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters,see the file
named copYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.37; using libpng 1.6.37
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 4.02
~ $ pandoc test.md -o test.pdf
Error producing PDF.
! LaTeX Error: File `xcolor.sty' not found.

Type X to quit or <RETURN> to proceed,or enter new name. (Default extension: sty)

Enter file name:
! Emergency stop.
<read *>

l.36 \IfFileExists

解决方法

经过反复试验,我找到了一个有效的解决方案。

正如@mb21 所提到的,Docker 镜像可能是长期的最佳选择。 Docker images are supported on Heroku。但是,我想避免对整个应用程序进行 dockerizing 来解决这个问题。

找到支持添加自定义 TeX Live 包 (one example of such buildpack) 的 Heroku 的 TeX Live buildpack 后,转换错误为 ! LaTeX Error: File 'xcolor.sty' not found.

我使用 tlmgr 来获取有关丢失文件的一些信息。运行 tlmgr search --global --file xcolor.sty 可以解决问题并显示有一个名为 xcolor 的包。安装后,我们来到下一个错误,下一个,下一个。最后,我最终安装了 2 个对 Heroku 来说足够小的集合(注意 500MB 的 slug 大小限制)并包含成功转换所需的所有 pandoc。这两个是 collection-fontsrecommendedcollection-latexrecommended

texlive.packages 文件添加到应用程序的根目录即可解决问题。它被 buildpack 识别,并使用 tlmgr 为您安装所有指定的包。

# texlive.packages

collection-fontsrecommended
collection-latexrecommended

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?