Repository navigation
Render PDFs from Rails controllers and .pdf.rb templates - #106
Merged
Merged
Conversation
With Rails in the bundle, a Railtie adds render pdf: (a Gamba or Prawn document, an object answering to_pdf, or bytes; inline unless told, the filename encoded by Rails), registers .pdf.rb templates that draw on a Gamba::Document with the view's helpers, and runs Gamba.eager_load! before Rails eager loads. image and pdf_page read anything answering download, as Active Storage attachments do. Without Rails, nothing of it is defined. The Rails tests run in a process of their own, on Rails 8.1 and 8.0 in CI. script/highlight_docs.rb reads the pages as UTF-8 whatever the locale. Assisted-by: Claude [Claude Code]
Under gemfiles/rails_8_0.gemfile, CI installs only that bundle, and the oracle ran Gamba with the main Gemfile, whose gems were missing: every example failed with "gamba cannot run". Assisted-by: Claude [Claude Code]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Most Gamba users are Rails applications, and each one writes the same glue:
send_datawith the right type and disposition, a call toGamba.eager_load!in the boot hook, a temporary file for each Active Storage image. Nothing in the gem knew about Rails. Closes #75.With Rails in the bundle, a Railtie wires this in, with no configuration and no runtime dependency on any Rails gem.
render pdf:takes a Gamba or Prawn document, an object answeringto_pdf(an application's document class), or a PDF's bytes. The response isapplication/pdf, inline unlessdisposition: :attachment, withfilenameencoded by Rails for any language.show.pdf.rbtemplate draws ondocument, aGamba::Document, with the view's helpers and translations at hand; Prawn's API is atdocument.prawn. A template that ends with its own document renders that one. The response loses the charset Rails adds to text.Gamba.eager_load!runs before Rails eager loads, so a forking server's workers share what Gamba loads.imageandpdf_pageread anything that answersdownload, so attachments and blobs need no temporary file.require "gamba"defines neitherRailsnorActiveSupport. The Rails tests run in a process of their own, so its core extensions do not change the Ruby the other tests run on. CI adds Rails 8.1 and 8.0 to the test matrix, as the sibling gems do.Proof
test/rails/rails_test.rbboots an application and requests it through Rack. It checks the type, inline and attachment dispositions,Facture été 2026.pdfencoded asfilename*=UTF-8''Facture%20%C3%A9t%C3%A9%202026.pdf, and the text of Gamba, Prawn andto_pdfdocuments read back with mutool. A template printst("greeting")andnumber_to_currencyin the PDF; another ends with its own Prawn document. A subprocess shows the autoloads resolved after an eager-loading boot, and another shows that plainrequire "gamba"defines no Rails constant. An object answeringdownloadworks as an image and as a PDF page.rake testpasses on Rails 8.1 and on 8.0 (gemfiles/rails_8_0.gemfile): 7 Rails tests and 1369 others, 0 failures; RuboCop is clean.script/highlight_docs.rbnow reads pages as UTF-8 whatever the locale: it failed under a US-ASCII one, as the tests did before #100.Generated by Claude Code