【问题标题】:What is the correct way to render_to_string in wicked pdf?在 wicked pdf 中 render_to_string 的正确方法是什么?
【发布时间】:2013-04-05 00:42:52
【问题描述】:

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

【问题讨论】:

  • 有没有机会在这里选择正确的答案?

标签: ruby-on-rails-3 wicked-pdf


【解决方案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 

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

【讨论】:

    【解决方案2】:

    不要使用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
    

    【讨论】:

      【解决方案3】:

      如果您使用 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/

      【讨论】:

      • Rails 5 有哪些模块?
      猜你喜欢
      • 2014-03-22
      • 1970-01-01
      • 2011-02-05
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多