加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – wkhtmltopdf尝试从http而不是文件加载

发布时间:2020-12-17 04:37:44 所属栏目:百科 来源:网络整理
导读:这是一个奇怪的小问题,导致我在SO上发布我的第一个问题.我使用wkhtmltopdf将 HTML文档转换为PDF作为Rails应用程序的一部分.为此,我将Rails网页呈现为临时目录中的静态HTML文件,将静态页眉,页脚和图像复制到同一个临时目录,然后使用“system”执行wkhtmltopdf
这是一个奇怪的小问题,导致我在SO上发布我的第一个问题.我使用wkhtmltopdf将 HTML文档转换为PDF作为Rails应用程序的一部分.为此,我将Rails网页呈现为临时目录中的静态HTML文件,将静态页眉,页脚和图像复制到同一个临时目录,然后使用“system”执行wkhtmltopdf.

这在开发和测试环境中非常有效.在我的Staging环境中,它没有.我首先怀疑权限,但该进程的前几部分(创建HTML静态文件并将它们复制到目录中)正在运行.我可以从该临时目录中的命令行运行wkhtmltopdf并获得预期的结果.最后,我通过“系统”和反引号在staging环境中通过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的选项,以确保它从本地文件加载?我很困惑.谢谢!

解决方法

看看wicked_pdf gem.
您可以添加PDF mime类型,然后添加您想要pdf的任何页面,只需将.pdf添加到URL即可.

我在生产中使用它,它运作良好.无需直接调用wkhtmltopdf.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读