title: Render a HTML template during a PDF request in Ruby on Rails
body: <p>I am trying to pin point how to use <code>render_to_string</code> and get the HTML before converting it to PDF.</p>
  <p>Right now, I am using <code>PDFKit</code> to re-request the HTML version of the same URL, but it is plain ugly:</p>
  <pre class="lang-rb prettyprint-override"><code>def my_report
    # do some smart logic first
  
    respond_to do |format|
      format.html  # this responder renders HTML normally
  
      format.pdf do
        # recreate the request URL with the .html extension instead of .pdf
        url = my_report_url(format: :html, params: params)
  
        # PDFKit fetches HTML from the URL
        kit = PDFKit.new(url, page_size: 'A4', orientation: 'Portrait')
  
        # PDFKit converts the HTML to PDF
        send_data(kit.to_pdf, filename: 'my_report.pdf', type: 'application/pdf; charset=utf-8', disposition: 'inline')
      end
    end
  end
  </code></pre>
  <p>The solution seems to be to use <code>render_to_string</code> to render HTML within the <code>format.pdf</code> block, something like this:</p>
  <pre class="lang-rb prettyprint-override"><code>format.pdf do
    # render the HTML in memory - fails with ActionView::MissingTemplate
    html = render_to_string :my_report, formats: [:html]
  
    # PDFKit uses the in-memory HTML (saves a roundtrip)
    kit = PDFKit.new(html, page_size: 'A4', orientation: 'Portrait')
  
    # PDFKit converts the HTML to PDF
    send_data(kit.to_pdf, filename: 'my_report.pdf', type: 'application/pdf; charset=utf-8', disposition: 'inline')
  end
  </code></pre>
  <p>This always fails with the <code>ActionView::MissingTemplate</code> error. Any ideas?</p>