Pandoc 转换为 pdf 不适用于 heroku

Pandoc conversion to pdf not working on heroku

我在 rails 应用程序上有一个 ruby,它使用 pandoc-ruby 将 markdown 文件转换为 pdf。 pandoc-ruby 需要安装 pandoc。要成功转换为 pdf,还需要存在 pdflatex。在本地(在 Mac 和 Ubuntu 18.04 上测试)如果安装了 pandoctexlive-latex-recommendedtexlive-fonts-recommended 包,一切正常。部署到 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:自定义构建包

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

~ $ 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.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 包的 Heroku TeX Live buildpack 后 (one example of such buildpack),转换错误为 ! LaTeX Error: File 'xcolor.sty' not found.

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

texlive.packages 文件添加到应用程序的根目录即可。它被 buildpack 识别并使用 tlmgr.

为您安装所有指定的包
# texlive.packages

collection-fontsrecommended
collection-latexrecommended