Thursday, January 28, 2016

Hackable PDF Typesetting in Ruby with Prawn

Installing Prawn

If you're running a Rails operation, LaTeX isn't the most convenient approach for generating documents. There are some gems available, but it's really nice when the logic and the presentation are in the same language. That's what Prawn supplies.

First, we need to install the Prawn gem:

gem install prawn

We can verify that Prawn is working with the following test:

require "prawn"

Prawn::Document.generate("hello.pdf") do
  text "Hello World!"
end

Making Text

Most of Prawn's commands are fairly minimalistic and what you would expect:

require "prawn"

Prawn::Document.generate("styling_text.pdf") do
  text "Default text styling"
  text "Blue 16pt Helvetica", size: 16, font: "Helvetica", color: "0000FF"
  text "Aligned Center", align: :center

  font_size 12
  font "Courier" do
    text "Size 12 Courier"
    font_size 10 do
      text "Slightly smaller Courier"
    end
  end
  text "Default font size 12"

  font "Helvetica"
  3.times do |i|
    text "Helvetica with leading 10 line #{i}", leading: 10
  end
end

Continue reading %Hackable PDF Typesetting in Ruby with Prawn%


by Robert Qualls via SitePoint

No comments:

Post a Comment