PDFKit无法生成正确的PDF

时间:2011-03-28 00:47:18

标签: ruby-on-rails ruby pdfkit

我安装了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我发现调用的值是:

"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。

生成的正确pdf为here,错误为here

我不知道这里有什么不妥。

感谢。

1 个答案:

答案 0 :(得分:0)

解决方案非常简单,并且是PDFKits Github的bxu689 pull request的对象。