PDFKit 未生成正确的 PDF
我安装了 PDFKit 和 wkhtmltopdf。
在一个操作中,我渲染了一个简单的 html,如下所示:
<html><body><h1>Hello Internet!</h1></body></html>
当我使用 .pdf 扩展名运行操作时,它会生成错误的 pdf 文件。调试 PDFKit 我得到了这些行:
result = IO.popen(invoke, "w+") do |pdf|
pdf.puts(@source.to_s) if @source.html?
pdf.close_write
pdf.gets(nil)
end
嗯,这是生成我的 PDF 的地方,它使用命令行 no wkhtmltopdf 和我的 html 作为输入。
使用 rdebug 我发现 invoke 的值是:
"c:\Program Files\wkhtmltopdf\wkhtmltopdf.exe" "--margin-right" "0.75in" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-"
并且 @source.to_s 是
<html><body><h1>Hello Internet!</h1></body></html>
好的,所以我尝试像这样手动运行命令:
"c:\Program Files\wkhtmltopdf\wkhtmltopdf.exe" "--margin-right" "0.75in" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-" < c:\hello_internet.html > correct.pdf
显然在 hello_internet 中有 @source.to_s 的内容
运行它会产生正确的 pdf正在生成。
我不知道这里出了什么问题。
谢谢。
I installed PDFKit and wkhtmltopdf.
In one action I render a simple html like:
<html><body><h1>Hello Internet!</h1></body></html>
When I run my action with .pdf extension it generates a wrong pdf file. Debugging PDFKit I got to these lines:
result = IO.popen(invoke, "w+") do |pdf|
pdf.puts(@source.to_s) if @source.html?
pdf.close_write
pdf.gets(nil)
end
Well, this is where my PDF is generated, it uses a command line no wkhtmltopdf and my html as input.
Using rdebug I found that the value for invoke is:
"c:\Program Files\wkhtmltopdf\wkhtmltopdf.exe" "--margin-right" "0.75in" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-"
And @source.to_s is
<html><body><h1>Hello Internet!</h1></body></html>
Ok, so I tried to run the command by hand like this:
"c:\Program Files\wkhtmltopdf\wkhtmltopdf.exe" "--margin-right" "0.75in" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-" < c:\hello_internet.html > correct.pdf
Obviously in hello_internet there is the content of @source.to_s
Running it results in the correct pdf being generated.
The correct pdf generated is here and the wrong is here.
I have no idea what can be wrong here.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该解决方案非常简单,是 PDFKits Github 上 bxu689 的拉取请求的对象。
The solution is pretty simple and is object of a pull request by bxu689 at PDFKits Github.