1

So I have some Rails 3.2.x code that takes an HTML view page and outputs it to PDF using the docraptor print service. The only issue is that it doesn't shrink the contents to fit on the PDF page, so there's overrun.

I initially tried to do a media query to inject some css for printing, but that didn't seem to do anything.

What are my options to try and make this work?

1) I thought about making a separate print style sheet (instead of media query) but I'm not sure docraptor is treating the HTML document in "print mode" when I send it to docraptor via a POST request.

2) I could also make a separate Rails view to print out with a narrower width (say 500 px), to ensure all the contents fits on the PDF page when outputted. This is more work, so I'm trying to make something like #1 work but I haven't had any luck.

class PdfMaker
  include HTTParty
  def make_pdf
     options = {
          :document_url => url,
          :document_type => "pdf",
          :name => "mydocument.pdf",
          :test => false,
          :async => false,
          :prince_options => {:baseurl => 'http://www.example.com'}
      }

      response = post("/docs", :body => {:doc => options}, :basic_auth =>{:username => "secure_stringXXXX"})
  end
end
Nona
  • 5,302
  • 7
  • 41
  • 79

1 Answers1

2

1) Should work fine. DocRaptor supports media queries and, by default, uses the print stylesheets. The only hitch is that it does not (currently) support conditional expressions like:

@media print and (min-width:700px) { ... }

All conditional expressions will return as false, so you'll just want to use:

@media print { ... }

2) This will also work, but should be entirely unnecessary.

If you're trying to control the page size, you'll want to use @page rules. One of the big advantages of DocRaptor is that all of this can be styled through pure CSS without messing with code configuration. Details on page size are available here: http://www.princexml.com/doc/page-size/

Lastly, the DocRaptor support service can assist with debugging HTML to PDF conversions. Just go to the Docs Log and click "Request Help" on a particular document.

jamespaden
  • 448
  • 2
  • 10