我正在使用Terrapin从Rails中的命令行执行wkhtmltopdf.

Terrapin::CommandLine.path = "/usr/bin/wkhtmltopdf"
pdfCommand = Terrapin::CommandLine.new("xvfb-run wkhtmltopdf :html_source :pdf_target")
tempHTML = Tempfile.new(["pattern", ".html"])
tempHTML << html
tempPDF = Tempfile.new(["pattern",".pdf"])
pdfCommand.run(html_source: tempHTML.path, pdf_target: tempPDF.path)

它运行,但返回:

QPainter::begin(): Returned false
Error: Unable to write to destination
Exit with code 1, due to unknown error.

我已经确定wkhtmltopdf拥有完全权限,我不确定TerRapin是否与此有关?

编辑:在控制台中运行xvfb-run wkhtmltopdf http://www.example.com test.pdf符合预期.

推荐答案

为了使Terrapin处理内插,它们需要在第二个参数中为new.即.它期望您正在运行的命令与其中包含内插的字符串分开:

Terrapin::CommandLine.new("xvfb-run", "wkhtmltopdf :html_source :pdf_target")

如果你在你的脚本上加Terrapin::CommandLine.logger = Logger.new(STDOUT),你会看到水龟试图逃跑,字面意思是xvfb-run wkhtmltopdf :html_source :pdf_target.

Ruby-on-rails相关问答推荐

Rails:嵌套命名空间产生一个奇怪的部分路径,导致MissingTemplate错误

Rails:has_many 通过不返回结果

Sweet Alert 2 在 Rails 7 应用程序中无法正常工作

从 rails 5 升级到 6 和 zeitwerk

如何在 Ruby on Rails 中正确存储 BigDecimal

别名可能与不同模型的相同别名有许多关联?

URL 中的 Rails slugs - 使用 Active Record Model Post 的 Title 属性而不是 ID

设计 token_authenticable 已弃用,有什么替代方案?

如何启动 rails 控制台并专门使用测试数据库?

rails s和bundle exec rails s有什么区别?

有没有办法在 Rails 3.1 中检测用户代理

在 Rails 的 ActiveRecord 中,touch 有什么用?

Rails 4,使用 ActiveRecord 的原始查询

在 Rails 中,如何处理多个选中的复选框,只需拆分 , 或?

如何将 yaml 文件解析为 ruby​​ 哈希和/或数组?

.increment vs += 1

为什么 respond_with 被从 rails 4.2 移除到它自己的 gem 中?

Rails:将所有未知路由重定向到 root_url

ActiveRecord 查找以

如何删除删除选项表单 activeAdmin?