Skip to content

Render PDFs from Rails controllers and .pdf.rb templates - #106

Merged
czj merged 2 commits into
mainfrom
claude/confident-pasteur-jz0fj6
Nov 13, 2026
Merged

czj merged 2 commits into
mainfrom
claude/confident-pasteur-jz0fj6

Conversation

@czj

@czj czj commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Most Gamba users are Rails applications, and each one writes the same glue: send_data with the right type and disposition, a call to Gamba.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.

  • Controller rendering: render pdf: takes a Gamba or Prawn document, an object answering to_pdf (an application's document class), or a PDF's bytes. The response is application/pdf, inline unless disposition: :attachment, with filename encoded by Rails for any language.
  • PDF templates: a show.pdf.rb template draws on document, a Gamba::Document, with the view's helpers and translations at hand; Prawn's API is at document.prawn. A template that ends with its own document renders that one. The response loses the charset Rails adds to text.
  • Boot hooks: Gamba.eager_load! runs before Rails eager loads, so a forking server's workers share what Gamba loads.
  • Active Storage: image and pdf_page read anything that answers download, so attachments and blobs need no temporary file.
  • Rails kept apart: without Rails, require "gamba" defines neither Rails nor ActiveSupport. 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.rb boots an application and requests it through Rack. It checks the type, inline and attachment dispositions, Facture été 2026.pdf encoded as filename*=UTF-8''Facture%20%C3%A9t%C3%A9%202026.pdf, and the text of Gamba, Prawn and to_pdf documents read back with mutool. A template prints t("greeting") and number_to_currency in the PDF; another ends with its own Prawn document. A subprocess shows the autoloads resolved after an eager-loading boot, and another shows that plain require "gamba" defines no Rails constant. An object answering download works as an image and as a PDF page. rake test passes 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.rb now 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

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]
@czj
czj merged commit 5c01814 into main Nov 13, 2026
11 checks passed
@czj
czj deleted the claude/confident-pasteur-jz0fj6 branch November 13, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rails applications wire PDF rendering by hand

2 participants