这就是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。我在应用程序控制器中使用gen_pdf方法,在views / application文件夹中使用等效的pdf.html.erb。我错过了什么。
答案 0 :(得分:7)
template
。我遇到了和你一样的问题。当您在Controller操作中执行此操作时,您必须以完全不同的方式呈现PDF,而不是使用Mailer或其他内容。
首先,使用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
答案 1 :(得分:2)
如果您正在使用wicked_pdf并尝试在控制器之外生成pdf(即在cron作业中),您可以这样做(raild 4.1 +):
tmp
并调用此方法:
# app/renderers/pdf_renderer.rb
class PdfRenderer < ActionController::Metal
include ActionView::ViewPaths
include AbstractController::Rendering
include AbstractController::Helpers
include ActionController::Helpers
include ActionView::Rendering
include ActionView::Layouts
include ActionController::Rendering
include ActionController::Renderers
include ActionController::Renderers::All
append_view_path "app/views"
View = Struct.new(:layout, :template, :locals)
def render_pdf(view)
wicked = WickedPdf.new
pdf_string = render_to_string(template: view.template, layout: view.layout, locals: view.locals)
# Make a PDF in memory
pdf_file = wicked.pdf_from_string(pdf_string, pdf: "pdf_name",
page_size: 'A4',
wkhtmltopdf: ENV['WKHTMLTOPDF_EXECUTABLE_PATH'],
margin: { top: '0.5in', bottom: '1in', left: '0.5in', right: '0.5in'}
)
# Write it to tempfile
tempfile = Tempfile.new(['document', '.pdf'], Rails.root.join('tmp'))
tempfile.binmode
tempfile.write pdf_file
tempfile.close
tempfile
end
end
现在您可以使用 renderer = PdfRenderer.new
pdf_view = PdfRenderer::View.new.tap do |v|
v.template = "postal/#{template_file}.pdf.erb"
v.layout = 'layouts/layout.pdf.erb'
v.locals = {:user=> @user, :other_stuff => @details}
end
temp_pdf = renderer.render_pdf pdf_view
对此文件执行任何操作(即发送电子邮件)
您可以在此处详细了解此方式:http://blog.arkency.com/2014/03/pdf-in-rails-without-controllers/
答案 2 :(得分:1)
您可以在邮件中找到类似的内容:
class ReportMailer < ActionMailer::Base
default :from => DEFAULT_FROM
def report_pdf(user, bookings)
@bookings = booking
@user = user
mail(:subject => 'Overtime', :to => user.email) do |format|
format.text # renders overtime_pdf.text.erb for body of email
format.pdf do
attachments['monthly_report.pdf'] = WickedPdf.new.pdf_from_string(
render_to_string(:pdf => 'monthly_report', :template =>
'hospital_bookings/index.pdf.erb', :layouts => 'pdf.html')
)
end
end
end
end
希望这会有所帮助。此外,如果您需要进一步的帮助,最好发布一些代码,以便其他人可以更好地了解您所做的事情以及您希望实现的目标。