Generating PDFs With Rails

On this week's episode, Chris takes us through everything we need to work with PDFs in our Rails apps: the easiest way to generate them, how to properly serve them as responses in our controllers, and even how to test them.


This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/generating-pdfs-with-rails

Really great start point for dealing with PDF in Rails applications.
One thing that might help is using aws to store to pdf, that way we can have an historical, and a place where users can download, all the PDF the need.
Maybe using sidekiq for the generation purpose, to release the request time while generating the PDF.

But again great resource.

Thanks Chris.

Hi @GustavoCaso, you’re totally right. I considered including a summary of caching the generated PDF on AWS, but chose against for time and complexity reasons. That said, I would definitely set up AWS for storing the generated files on anything other than small hobby projects.

This is awesome. I needed to create feature this week where it creates pdf receipts for donors and donations after they give through stripe. Thank you!

@christoomey I imaging is hard to include everything in a 30 min video. But either way is a great resource for learning how to create PDF in a rails project. Thanks for the video.

@christoomey I saw you doing a tmux-like zoom but with vim splits. How did you make that happen?

@bzf I’m using a window command in Vim that maximizes the current Vim window in both dimensions. My specific mapping is:

nnoremap <leader>- :wincmd _<cr>:wincmd \|<cr>

I cover this in (oddly enough) the tmux course. You can check out the video and notes in the Layout Balancing section of the Vim Integration video.

2 Likes

@christoomey Thanks a lot! :smiley:

I already used WickedPDF for this, which is more or less the same approach as PDFkit. En eye opener for me was to create a HTML version in development mode. Till now I was tweaking styles and generating PDF’s all the time, which is wasting a lot of time and is dreadful, so thanks for that!

Great episode, good to see the pdf-reader gem. I’d not come across that before.

Excellent episode! I’m really glad you covered the topic all the way on through deployment. Thanks!

Great episode! But shouldn’t the pdf be generated in the background? because it can take quite some time.
It would be great to explain how to generate it in the background then serve it to the user.
Thanks.

Hey @benoitwerner, you’re definitely right about that. I should have clarified this in the video, but in the real world implementation I do create the PDF outside of the request, saving to an external onject store. I omitted this from the video for the sake of brevity, but I should have at least mentioned the concept. Thanks for keeping me honest! :slight_smile:

Thanks @christoomey for this episode. I’d like to ask about the difference between wkhtmltopdf-binary vs wkhtmltopdf-heroku. Could one be switched for the other?

@pedropaf thanks for the question. I assume they’d both serve similar purposes, but I reach for the Heroku specific version as that my need.

wkhtmltopdf-heroku provides only the binary for an x64 architecture on Linux, whereas wkhtmltopdf-binary also has the x86 Mac and x86 Linux binaries.

Each binary is quite big (~40MB) so using the Heroku-only version should result in a smaller slug.

@christoomey This is a bit of a tangent but I’m interested in some of the commands you’re using like stat and next-commit, and couldn’t find them in your dotfiles.

@christoomey how would you test this if you need mora capybara like testing on pdf content ?
Would you include ability to render html in testing environment as you did in development env, and test on html ?

Hey @tsigo, tangents fully welcome! Here are the configurations for stat and next-commit. Both are defined as git aliases. We have a section in Mastering Git on git aliases if you want any more detail on them, but for the specifics of the two commands, they are implemented as:

  next-commit = !git checkout $(git log --reverse --ancestry-path --pretty=%H HEAD..master | head -1)
  stat = show --stat

@dixpac I believe there are a number of tools that can go further in extracting content from a PDF, but I’m not terribly familiar with them. My first tact would be to make sure I’ve extracted as much of the logic about what is rendered into the PDF into an object and test that thoroughly. Things like calculations, translations, etc. From there, I would use the minimal text matching testing I showed in the video. In practice, I’ve not found a need to go further, but it certainly is possible.