gpt4 book ai didi

ruby-on-rails - Rails 4 wicked pdf 问题

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:37 25 4
gpt4 key购买 nike

我正在尝试使用 wicked pdf 将 html 页面呈现为 pdf。

mime_types.rb

Mime::Type.register "application/pdf", :pdf

Controller 方法

def invoice    
respond_to do |format|
format.html
format.pdf do
render pdf: "invoice"
end
end
end

wicked_pdf.rb

:exe_path => '/usr/local/bin/wkhtmltopdf'

发票.pdf.erb

<div id="test_pdf">
test data
</div>

我已经添加了上述代码并将以下 gem 添加到我的项目中。

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

当我尝试呈现页面时,出现Template is missing 错误。如果我将 invoice.pdf.erb 重命名为 invoice.html.erb 我可以绕过错误,但我将得到一个 html 页面而不是 pdf。

我错过了什么吗?

最佳答案

wicked_pdf的文档中所述,你可以像这样使用它。它不言自明。不要忘记创建“pdfs”目录

# or from your controller, using views & templates and all wicked_pdf options as normal  
pdf = render_to_string pdf: "some_file_name", template: "templates/pdf.html.erb", encoding: "UTF-8"

# then save to a file
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end

send_file save_path, :type=>'text/pdf

关于ruby-on-rails - Rails 4 wicked pdf 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29484006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com