title: Generating a PDF as attachment in a mail in Rails 4
body: <p><strong>The problem</strong></p>
  
  <p>I want to send emails with an invoice PDF attached to it.</p>
  
  <p>I already have a invoice show view, with styling and javascript.</p>
  
  <p>The javascript is making calculations for the total field, formatting the currency and dates and splitting at page breaks. </p>
  
  <p>The show view is presenting the invoice just like it should be on the pdf.</p>
  
  <p><strong>Attempts</strong> </p>
  
  <ol>
  <li><p>I have tried <a href="https://github.com/mileszs/wicked_pdf" rel="nofollow">wicked_pdf</a> and <a href="https://github.com/pdfkit/pdfkit" rel="nofollow">pdfkit</a>. The problem is that
  when doing this..</p>
  
  <pre><code>html = render_to_string(:action =&gt; "../invoices/show_pdf.html.erb") 
  mail(:to =&gt; @registration.billing_email, :subject =&gt; "Faktura") do |format|
    format.pdf do
      attachments['faktura.pdf'] = PDFKit.new(html).to_pdf
    end
  </code></pre>
  
  <p>..I don't have a browser to interpreter the javascript. So all
  calculations and formats is gone. When I open the pdf in the browser it's correct.</p></li>
  <li>I also tried <a href="https://github.com/adeven/shrimp" rel="nofollow">shrimp</a> which uses phantomjs to generate the
  PDF's. The problem with this solution is that I am using devise and
  the phantomjs browser has to be signed in in some way.</li>
  </ol>
  
  <p>I need some suggestions on a elegant solution for this problem.</p>
  
  <p>Either </p>
  
  <p>Some way to render the view with all the javascript before I generate the PDF with pdfkit or wicked_pdf </p>
  
  <p>or </p>
  
  <p>How to assign a session cookie to phantomjs?</p>
  
  <p>I am aware that this is two questions, but as it solves one problem I hope it is ok. </p>