在wicked pdf中render_to_string的正确方法是什么?

use*_*136 3 ruby-on-rails-3 wicked-pdf

这就是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'})}
)   
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*ter 9

不要使用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
Run Code Online (Sandbox Code Playgroud)


Dav*_*vid 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 
Run Code Online (Sandbox Code Playgroud)

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