将 Wicked_pdf 的 PDF 视图转换为文件并将其保存到 S3
Convert a PDF view with Wicked_pdf to a file and saving it to S3
我目前有一个模型,该模型具有呈现 PDF 视图的特定控制器方法。
假设这个模型是 photographer_quote
我在 photographer_quote
控制器中有一个名为 print_quote
的方法来完成这项工作。
这是代码:
@quote= Photographer_quote.find(params[:id])
respond_to do |format|
format.pdf do
render pdf: 'quote'+@quote.id.to_s
end
一切都很好,当针对这个控制器方法的 link 被激活时,它会即时创建 PDF 并将其呈现在浏览器中。 (当然,我对所有格式都有看法...)
不过,现在我想将此 PDF 创建放入队列 (Sidekiq),并将其作为 PDF 文件保存到另一个模型(我们称之为 PDF_quotes
)中,该模型具有单个 PDF 回形针附件。
尽管我正在努力通过 Paperclip 将 PDF 保存在 S3 上。
Wicked pdf 提到了这个:
# 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", 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
我必须首先在本地创建文件吗?以及如何使用名为 quote
的回形针附件将文件写入模型 PDF_quotes
也许这会让你朝着正确的方向前进,关于:
"Do I have to create the file locally first place ? And how can I write the file onto model PDF_quotes with a Paperclip attachment named quote"
您可以在内存中创建一个 WickedPdf 并像这样直接通过管道传输到回形针模型。
型号PDF_quote有标准回形针"has_attached_file :quote"
例如在控制器中你可以这样做:
# Generate PDF
pdf = WickedPdf.new.pdf_from_string(
render_to_string('your-pdf-template-in-html.pdf') # This is a view
)
# Stream PDF from WickedPdf to Paperclip PDF_quote.quote
# Content Type is automatically read by Paperclip (at least for application/pdf in my testing)
pdf_quote = PDF_quote.new(
# Other attributes here as well.....
quote: StringIO.new(pdf) # Pipe pdf to quote
)
pdf_quote.quote_file_name = "your_quote_file_name.pdf"
pdf_quote.save
您可以添加更多选项。pdf_from_string 请参阅回形针 GitHub。布局、页脚、页眉等内容...
希望对您有所帮助!
我目前有一个模型,该模型具有呈现 PDF 视图的特定控制器方法。
假设这个模型是 photographer_quote
我在 photographer_quote
控制器中有一个名为 print_quote
的方法来完成这项工作。
这是代码:
@quote= Photographer_quote.find(params[:id])
respond_to do |format|
format.pdf do
render pdf: 'quote'+@quote.id.to_s
end
一切都很好,当针对这个控制器方法的 link 被激活时,它会即时创建 PDF 并将其呈现在浏览器中。 (当然,我对所有格式都有看法...)
不过,现在我想将此 PDF 创建放入队列 (Sidekiq),并将其作为 PDF 文件保存到另一个模型(我们称之为 PDF_quotes
)中,该模型具有单个 PDF 回形针附件。
尽管我正在努力通过 Paperclip 将 PDF 保存在 S3 上。
Wicked pdf 提到了这个:
# 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", 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
我必须首先在本地创建文件吗?以及如何使用名为 quote
PDF_quotes
也许这会让你朝着正确的方向前进,关于:
"Do I have to create the file locally first place ? And how can I write the file onto model PDF_quotes with a Paperclip attachment named quote"
您可以在内存中创建一个 WickedPdf 并像这样直接通过管道传输到回形针模型。
型号PDF_quote有标准回形针"has_attached_file :quote"
例如在控制器中你可以这样做:
# Generate PDF
pdf = WickedPdf.new.pdf_from_string(
render_to_string('your-pdf-template-in-html.pdf') # This is a view
)
# Stream PDF from WickedPdf to Paperclip PDF_quote.quote
# Content Type is automatically read by Paperclip (at least for application/pdf in my testing)
pdf_quote = PDF_quote.new(
# Other attributes here as well.....
quote: StringIO.new(pdf) # Pipe pdf to quote
)
pdf_quote.quote_file_name = "your_quote_file_name.pdf"
pdf_quote.save
您可以添加更多选项。pdf_from_string 请参阅回形针 GitHub。布局、页脚、页眉等内容...
希望对您有所帮助!