Rails
render pdf:, send_pdf, previews like ActionMailer's, a font generator — and no Rails dependency in the gem.
Controllers and configuration#
From the README.
send_pdf#
Without the renderer.
Stationery::Rails#send_pdf(document, filename:, disposition: "inline", type: "application/pdf") is what render pdf: calls. Outside the Railtie (or with config.stationery.renderer = false, keeping wicked_pdf's render pdf:), include it yourself:
class InvoicesController < ApplicationController
include Stationery::Rails
def show = send_pdf(InvoicePdf.new(@invoice), filename: "invoice-#{@invoice.number}.pdf")
endPreviews#
One public method per sample document.
Font generator#
stationery:fonts.
bin/rails generate stationery:fonts noto_sans liberation_serifInstalls the packs into vendor/fonts/<pack>/ — the directory the Railtie adds to Stationery.font_paths — so font_family "Noto Sans" needs no paths. Commit the files; nothing is downloaded at runtime. See Fonts.
Mailers, jobs and ActiveStorage#
to_pdf returns bytes — use them anywhere.
to_pdf returns the PDF as a binary String, so the same document class serves mailers, background jobs and storage:
class InvoiceMailer < ApplicationMailer
def issued(invoice)
attachments["invoice-#{invoice.number}.pdf"] = InvoicePdf.new(invoice).to_pdf
mail to: invoice.customer.email, subject: "Invoice #{invoice.number}"
end
end
class ArchiveInvoiceJob < ApplicationJob
def perform(invoice)
invoice.pdf.attach(io: StringIO.new(InvoicePdf.new(invoice).to_pdf),
filename: "invoice-#{invoice.number}.pdf", content_type: "application/pdf")
end
endPass strict: true in jobs that must never store a PDF with layout problems.