← Back to Upcase

Generating PDFs With Rails

(Elmer A. Sia) #21

Following this tutorial, I was able to generate PDF’s on development mode. My issue is on Heroku as I’ve tried uploading the repo and but can’t get the PDF generation working.

Is this because Heroku doesn’t allow serving of PDF files, just like images when using Paperclip? BTW, I didn’t upload the exact repo of the tutorial, I just wrote mine from scratch and followed along. Did I perhaps missed some gems?

It’s working locally but the PDF generation on Heroku does not.

(Elmer A. Sia) #22

@christoomey, I worked on this tutorial and pushed a repo to Heroku. Only the PDF generation part fails but in development mode locally it was fine at all.

Does this mean that Heroku can’t serve the PDF generated files just like other static assets?

(Chris Toomey) #23

Hey @panoysia, Heroku will happily serve the PDF, although you have to make sure you have the needed wkhtmltopdf binary available. There is a section in the video that covers PDF generation on Heroku which should have everything you need. Are you still having issues after following those notes?

(Elmer A. Sia) #24

The PDF generation in Heroku is working fine now @christoomey, and the issue was that the <%= Rails.application.assets.find_asset(‘invoice_pdf’).to_s %> heroku line of code in the invoice_pdf.html.erb layout file was causing the error.

I think this is because when assets are precompiled, Rails adds a hash to the invoice_pdf.html.erb file which in turn makes the call to** <%= Rails.application.assets.find_asset('invoice_pdf').to_s %> invalid.

(Pedro Moreira) #25

Sorry to bring up an old topic, but I thought it would be worth adding this can now be done in Rails 5 without the render_anywhere gem: you can use ApplicationController.renderer (method signature is identical). Just keep in mind that you need to set your asset host correctly in all envs so Rails picks ups on things (I think Suspenders does this out of the box, which is probably why @christoomey did not run into it during this vid). Hope this is useful to somebody!

(Dimitry Nazarov) #26

Error in the docs I think. Step describing ‘inlining’ css with a Rails.application.assets call has the filename as invoice.pdf whereas it should be invoice_pdf since the full filename is invoice_pdf.scss

(Haroon) #27

@pedrosmmoreira How did you get this working without render_anywhere gem? Can you show me a sample, I am using rails 5.1.0 (beta). Reducing a dependency if it’s not needed would be great for me. Thanks.

(Pedro Moreira) #28

Hi, let me create a sample repo and I’ll get back to you