简体   繁体   English

PDFKit无法生成正确的PDF

[英]PDFKit not generating correct PDF

I installed PDFKit and wkhtmltopdf. 我安装了PDFKit和wkhtmltopdf。

In one action I render a simple html like: 在一个动作中,我呈现了一个简单的html,例如:

<html><body><h1>Hello Internet!</h1></body></html>

When I run my action with .pdf extension it generates a wrong pdf file. 当我使用.pdf扩展名运行我的操作时,它将生成错误的pdf文件。 Debugging PDFKit I got to these lines: 调试PDFKit我有以下几行:

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. 好的,这就是生成我的PDF的地方,它使用的命令行没有wkhtmltopdf,而我的html作为输入。

Using rdebug I found that the value for invoke is: 使用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" "-" "-"

And @source.to_s is @ source.to_s是

<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 显然在hello_internet中有@ source.to_s的内容

Running it results in the correct pdf being generated. 运行它会生成正确的pdf。

The correct pdf generated is here and the wrong is here . 正确的pdf 在这里 ,错误的在这里

I have no idea what can be wrong here. 我不知道这里有什么问题。

Thanks. 谢谢。

该解决方案非常简单,是PDFKits Github上bxu689 提出拉取请求的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM