Pandoc 转换为 pdf 不适用于 heroku

时间:2021-01-22 21:48:53

标签: heroku pandoc pdflatex buildpack tex-live

我有一个 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, 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

1 个答案:

答案 0 :(得分:1)

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

正如@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
相关问题