wicked_pdf从带有页眉和页脚的模型方法创建PDF

时间:2018-09-12 12:17:02

标签: wicked-pdf

我正在尝试找出需要将哪些选项传递给av.render来打印带有页眉和页脚的 PDF 。控制器中使用的选项不起作用。

我在wicked_pdf wiki中找到了一个Wiki页面,但是它不提供要传递的打印页眉和页脚选项。

  pdf_html = av.render(template: 'reports/pdf.pdf.haml',
                       layout: 'layouts/pdf.pdf.haml',
                       print_media_type: true)

controller中的代码可以很好地打印出页眉和页脚。

  def show
    respond_to do |format|
      format.pdf do
        render pdf: @report.pdf_file_name,
               template: 'reports/pdf',
               layout:   'pdf',
               show_as_html: params.key?('debug'),
               header:  { html: {  template:'reports/pdf_header',
                                   locals:  { setting: setting } } },
               footer:  { html: {  template:'reports/pdf_footer',
                                   locals:  { setting: setting } } }
      end
    end
  end

model中使用的代码通过后台作业打印PDF。

def save_as_pdf
  av = ActionView::Base.new()
  av.view_paths = ActionController::Base.view_paths
  av.class_eval do
    include ApplicationHelper
  end

  pdf_html = av.render(template: 'reports/pdf.pdf.haml',
                       layout: 'layouts/pdf.pdf.haml',
                       print_media_type: true)

  pdf_string = WickedPdf.new.pdf_from_string(pdf_html)

  tempfile = Tempfile.new([self.pdf_file_name, ".pdf"], Rails.root.join('tmp'))
  tempfile.binmode
  tempfile.write pdf_string
  tempfile.close

  self.pdf = tempfile
  self.save
  tempfile.unlink
end

0 个答案:

没有答案
相关问题