5

这是 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。我错过了什么。

4

3 回答 3

11

不要使用template.

我遇到了和你一样的问题。当您在控制器操作中而不是在邮件程序或其他东西中执行它时,您必须以完全不同的方式呈现您的 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
于 2015-08-05T20:45:56.873 回答
4

如果您使用 wicked_pdf 并尝试在控制器之外生成 pdf(即在 cron 作业中),您可以这样做(raild 4.1 +):

# 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

现在你可以用temp_pdf.path这个文件做任何你想做的事情(即发送电子邮件)

你可以在这里阅读更多关于这种方式的信息:http: //blog.arkency.com/2014/03/pdf-in-rails-without-controllers/

于 2017-04-13T08:44:32.617 回答
2

你可以在你的邮件里有这样的东西:

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 

希望这可以帮助。此外,如果您需要进一步的帮助,最好发布您的一些代码,以便其他人可以更好地了解您所做的事情以及您想要实现的目标。

于 2013-04-12T17:37:56.537 回答