2012-11-13 39 views
1

我試圖生成用下面的代碼的PDF:如果我運行一個控制器的行動在這個代碼wicked_pdf不加載頁眉或頁腳中的ActionMailer

archivo = render_to_string(
     :pdf => "formulario_de_registro.pdf", 
     :template => 'pdf/profile_download.pdf.haml', 
     :layout => 'pdf/body.pdf.haml', 
     :margin => { :bottom => 40, :top => 30 }, 
     :header => { 
     :html => { :template => 'layouts/pdf/header.pdf.haml'}, 
     :font_size => 13}, 
     :footer => { 
     :html => { :template => 'layouts/pdf/footer.pdf.haml'}, 
     :line => true} 
    ) 
    # from here is just for debugging purposes 
    save_path = Rails.root.join('tmp','prueba.pdf') 
    File.open(save_path, 'wb') do |file| 
     file << archivo 
    end 

,工程巨大,但相同的代碼我的郵件類只是呈現HTML,而不是PDF。然後,如果我叫WickedPdf.new.pdf_from_string(archivo)生成PDF,但沒有頁眉或頁腳,這是正確的,因爲在生成的HTML不包括頁眉或頁腳。有什麼我失蹤了嗎?請幫忙。 在任何情況下,我使用的是:

  • wkhtmltopdf 0.11.0 RC1
  • 軌(3.2.3)
  • wicked_pdf(0.7.9)

謝謝!

回答

2

WickedPdf不alias_method_chain:在的ActionMailer render_to_string像它的ActionController的。

首先,更新wicked_pdf到0.8.0版本或git的主人。這將允許你把它列入上的ActionMailer類:

然後你解決這個問題通過手動包括PdfHelper模塊,只是調用該方法直接像這樣:

# Mailer 
class Notifications < ActionMailer::Base 
    include PdfHelper 

    def send_email 
    archivo = render_to_string_with_wicked_pdf(
     :pdf => "formulario_de_registro.pdf", 
     :footer => { :html => { :template => 'layouts/pdf/footer.pdf.haml' } } 
     # etc... 
    ) 
    attachments['formulario_de_registro.pdf'] = archivo 
    mail :to => '[email protected]' 
    end 
end 
+0

看來'未定義的方法「render_to_string_with_wicked_pdf」爲#' –

+0

@AlterLagos我忘了提及你必須包括PdfHelper,你甚至不能做到這一點在0.7.9適當的,並且必須使用git主,直到一個新的寶石版本出版。我已經證實這是有效的。 – Unixmonkey

+0

現在正與'修訂:488e473b5c6c9af7870d793da5769af49ef1b500'。非常感謝:) –