gpt4 book ai didi

ruby-on-rails-3 - 在 wicked pdf 中 render_to_string 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-01 07:19:49 25 4
gpt4 key购买 nike

这是 wicked pdf 的文档指定的内容:

WickedPdf.new.pdf_from_string(
render_to_string(:pdf => "pdf_file.pdf", :template => 'templates/pdf.html.erb', :layout => 'pdfs/layout_pdf'),
:footer => {:content => render_to_string({:template => 'templates/pdf_footer.html.erb', :layout => 'pdfs/layout_pdf'})}
)

我得到的是 ActionView::MissingTemplate
即使我在目录中有 pdf.html.erb 。我在应用程序 Controller 中使用了 gen_pdf 方法,在 View /应用程序文件夹中使用了等效的 pdf.html.erb。我错过了什么。

最佳答案

请勿使用 template .

我遇到了和你一样的问题。当您在 Controller 操作而不是 Mailer 或其他操作中执行此操作时,您必须以完全不同的方式呈现您的 PDF。

首先,使用 template不会工作。还有一些其他细微差别,但这里是我的实现,你可以建立:

notification_mailer.rb

def export

header_html = render_to_string( partial: 'exports/header.pdf.erb',
locals: { report_title: 'Emissions Export' } )

body_html = render_to_string( partial: "exports/show.pdf.erb" )

footer_html = render_to_string( partial: 'exports/footer.pdf.erb' )

@pdf = WickedPdf.new.pdf_from_string( body_html,
orientation: 'Landscape',
margin: { bottom: 20, top: 30 },
header: { content: header_html },
footer: { content: footer_html } )

# Attach to email as attachment.
attachments["Export.pdf"] = @pdf

# Send email. Attachment assigned above will automatically be included.
mail( { subject: 'Export PDF', to: 'elon@musk.com' } )

end

关于ruby-on-rails-3 - 在 wicked pdf 中 render_to_string 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15972189/

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