开发者

PDFKit not generating correct PDF

开发者 https://www.devze.com 2023-02-19 23:02 出处:网络
I installed PDFKit and wkhtmltopdf. In one action I render a simple html like: <html><body><h1>Hello Internet!</h1></body></html>

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 wron开发者_高级运维g here.

Thanks.


The solution is pretty simple and is object of a pull request by bxu689 at PDFKits Github.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号