这是一个奇怪的小问题,导致我在 SO 上发布我的第一个问题。我正在使用 wkhtmltopdf 将 HTML 文档转换为 PDF,作为 Rails 应用程序的一部分。为此,我将 Rails 网页渲染为临时目录中的静态 HTML 文件,将静态页眉、页脚和图像复制到同一临时目录,然后使用“system”执行 wkhtmltopdf。
这在开发和测试环境中完美运行。在我的暂存环境中,它没有。我一开始怀疑权限,但该过程的前几个部分(创建 HTML 静态文件并将它们复制到目录)正在运行。我可以从该临时目录中的命令行运行 wkhtmltopdf 并获得预期结果。最后,我在临时环境中通过 Rails 控制台通过“系统”和反引号运行 wkhtmltopdf,以下是我得到的输出:
> `wkhtmltopdf --footer-html tmp/invoices/footer.html --header-html tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in tmp/invoices/test.html tmp/invoices/this.pdf`
Loading pages (1/6)
QPainter::begin(): Returned false ] 10%
Error: Unable to write to destination
Error: Failed loading page http://tmp/invoices/test.html (sometimes it will work just to ignore this error with --load-error-handling ignore) => ""
注意最后一点。我指向本地文件,但它是通过 http 寻找它们。好吧,我想,也许我需要明确地向它提供 file:// 协议,这样它就不会寻找 http。所以我尝试这个:
> system("wkhtmltopdf --footer-html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/footer.html --header-html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/this.pdf")
Loading pages (1/6)
Error: Failed loading page file://library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
=> false
请注意,此操作失败并在库上显示小写“l”。有没有搞错? (不,建议忽略该开关的错误并没有得到任何改善。)
有任何想法吗?是否存在 Rails 或 Ruby 设置会导致系统命令被重写?是否有一个选项可以添加到 wkhtmltopdf 以确保它从本地文件加载?我很困惑。谢谢!
我在使用绝对文件路径时取得了成功(请注意后面的额外斜杠file://
)
wkhtmltopdf --footer-html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/footer.html --header-html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/this.pdf
这在 Windows 上也是一样的
Unix路径
file:///absolute/path/to/file
Windows路径
file:///C:/absolute/path/to/file
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)