【问题标题】:PDFKit not generating correct PDFPDFKit 没有生成正确的 PDF
【发布时间】:2011-03-28 00:47:17
【问题描述】:

我安装了 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。

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

我不知道这里出了什么问题。

谢谢。

【问题讨论】:

  • 也许我弄错了!您是否尝试使用 pdfkit 从 HTML 源生成 pdf 而不使用 wkhtmltopdf?

标签: ruby-on-rails ruby pdfkit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2013-12-21
    • 2018-06-18
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多