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.
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.
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.
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! 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!
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?
@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.