Create better printouts of a view/data

I’ve got an application that collects and displays information about accident reports, and the client wants to be able to:

  1. Create a PDF of the data in the view (probably with a “Create PDF” button) and have it come out nicely formatted without all the stuff that’s normally on the page. What’s the “best” way to accomplish this: Prawn? wkhtmltopdf? which gems?. Seems like Prawn and prawn_to used to be all the rage, but have fallen by the wayside of late.
  2. Just provide css print option that gets rid of much of the clutter.Know of any good tutorials on how to accomplish this? I’ve tried some “media=print” options but they all seem to wind up fouling the output in display mode.

Thanks…jon

  1. I think Prawn is still the option that allows you to customize things the most. There is also wicked_pdf, which uses wkhtmltopdf. (GitHub - mileszs/wicked_pdf: PDF generator (from HTML) plugin for Ruby on Rails).

  2. Your print stylesheets are probably being included in your views, maybe due to being picked up by the asset pipeline and compiled into the css. I’m not sure about this, but hope it gives you a clue for debugging the problem.

2 Likes

Thanks, @pedromoreira; much appreciated. I’ll look into your suggestion about the asset pipeline.

No problem :slight_smile: Glad to help!

I recently had to deal with this and came to the conclusion that if you’re looking to use something in your app (and not something like a heroku addon, docraptor, et al) that you’re better off writing a class on your own to act as a proxy for something like wkhtmltopdf.

I tried out PDFKit and it worked out fine if you’re using a multithreaded server, but that ended up being just annoying enough to not continue down that path.

If you have a collection of predefined pages you know you’ll need PDF’s for I might also suggest writing a rake task to generate them using wkhtmltopdf, uploading to S3, and either saving that against a model or just coming up with a common naming convention making it easier to link to.

There are options for wkhtmltopdf, by the way, that allow you to generate PDF’s with or without the print-stylesheet: https://github.com/antialize/wkhtmltopdf/blob/master/README_WKHTMLTOPDF#L199

Good luck @JESii!

1 Like

OK; I decided to start with wicked_pdf and wrote a feature test to get started; things are not working as I expected, so I have a couple of questions (see this pastie):

  1. I don’t think I’m writing a good test for this… any suggestions? Certainly the description is misleading, as I’m trying to create the report rather than just looking for a button; even so, I’m not convinced this was the right way to tackle the problem. [I can repost to the TDD section if that makes sense]
  2. Running the spec, all I get is a blank page (second save_and_open_page call). However, if I actually type in the url (http://localhost:4000/accident_reports/7.pdf), then I get an “ActionView::MissingTemplate” error, which is what I thought I would get from my spec so that I could continue specing out the code.

Thanks…jon

Got wicked_pdf (mostly) working.

The one remaining major issue I have is that I cannot get textarea fields to resize as I want them to. In spite of the fact that I use jquery.autosize.js to automatically resize them on the screen, they aren’t resized in the PDF but instead take a huge amount of vertical space… looks like it’s hard-coded to 1000px or something like that.

Any suggestions for how to deal with this? If not, then I’ll have to dump this and try something like prawn or pdfkit.

FYI, it’s all working now. The thing I hadn’t quite understood was that ANY Javascript or CSS that will be used on the page must be embedded in the page through use of the various wicked_pdf helpers, such as wicked_pdf_javascript_include_tag, etc. as that provides an absolute URL. CDN’s also work, so that’s what I did to include jQuery.

Thanks for all the help…