PDFs from Ruby
Invoices, tickets, certificates – sooner or later almost every program has to produce a PDF. Ruby has two strong gems for it: Prawn draws PDFs with a friendly DSL, and HexaPDF can also open existing PDFs and change them. Both are pure Ruby, so they run right here in your browser:
install_gem "prawn"
require "prawn"
pdf = Prawn::Document.new
pdf.text "Chunky Bacon's Breakfast Club", size: 24, style: :bold
pdf.move_down 10
pdf.text "Membership card for Kaz"
pdf.stroke_horizontal_rule
show_pdf pdfWhat happened?
Prawn::Document.newstarts an empty document with one page.textwrites a paragraph –size:andstyle:are keyword arguments. Prawn wraps long lines by itself and starts a new page when one is full.move_down 10leaves 10 points of space: PDF measures in points, 72 to the inch (an A4 page is 595 × 842).show_pdfis a helper of this site that shows the PDF below the cell. On your computer you would write it to a file:pdf.render_file "card.pdf".
Instead of putting pdf. in front of everything, you can give Prawn::Document.generate a block – at the end it writes the file:
Prawn::Document.generate("menu.pdf", page_size: "A4") do
text "Menu", size: 28, align: :center
move_down 20
{ "Bacon & eggs" => 9.5, "Pancakes" => 7.0, "Coffee" => 3.2 }.each do |dish, price|
float { text dish }
text format("$%.2f", price), align: :right
end
start_new_page
text "Page two: the fine print", size: 10
number_pages "<page> / <total>", at: [bounds.right - 50, 0]
end
show_pdf "menu.pdf"Inside the block, text works without pdf.: Prawn runs the block inside the document (lesson 121 shows how that trick works). float writes the dish and jumps back up, so the price lands on the same line, aligned right. number_pages stamps the page numbers at the end – <page> and <total> are filled in per page. The file menu.pdf appears as a download below the cell, and show_pdf takes its name too.
Now HexaPDF. It reads PDFs – ours or anybody's – and writes them back. Let's stamp every page of the menu:
install_gem "hexapdf"
require "hexapdf"
doc = HexaPDF::Document.open("menu.pdf")
doc.pages.each do |page|
canvas = page.canvas(type: :overlay)
canvas.fill_color(193, 74, 46)
canvas.font("Helvetica", size: 48)
canvas.text("CHUNKY!", at: [170, 420])
end
doc.write("menu-stamped.pdf")
show_pdf "menu-stamped.pdf"
doc.pages.countpage.canvas(type: :overlay) is a drawing surface on top of what is already on the page. Its origin [0, 0] is the bottom left corner – in PDF, y counts upwards. fill_color(193, 74, 46) is bacon red in RGB.
Merging is just as short: import every page of several documents into a new one.
merged = HexaPDF::Document.new
["menu.pdf", "menu-stamped.pdf"].each do |file|
source = HexaPDF::Document.open(file)
source.pages.each { |page| merged.pages << merged.import(page) }
end
merged.write("both.pdf")
show_pdf merged
merged.pages.countgem install prawn hexapdf, and render_file and write save real files. HexaPDF also comes with a command line tool: hexapdf info menu.pdf, hexapdf merge, hexapdf optimize.
Fonts: the 14 standard PDF fonts (Helvetica, Times, Courier …) only know Western European letters. For other scripts – Japanese, say – embed a TrueType font: pdf.font "NotoSansJP-Regular.ttf" in Prawn, canvas.font("NotoSansJP-Regular.ttf") in HexaPDF.
Licences: Prawn comes under Ruby's licence or the GPL. HexaPDF comes under the AGPL: if you pass on a program that uses it, or offer one as a web service, the AGPL generally requires you to publish that program's source under the AGPL too – unless you buy HexaPDF's commercial licence.
certificate.pdf with exactly 3 pages: Level 1 on the first, Level 2 on the second, Level 3 on the third.# certificate.pdf: 3 pages – "Level 1", "Level 2", "Level 3"