1. Products
  2.   PDF
  3.   Ruby
  4.   Prawn



Speedy PDF Generation via Open Source Ruby Library

Free Ruby PDF API that allows programmers to create & modify PDF files; insert, edit graphics, and draw text on a PDF page inside Ruby Apps.

The Portable Document Format (PDF) is a very popular reliable and secure file format for presenting, sharing, and printing files across the world. The open source Ruby library Prawn has provided complete functionality for working with PDF documents using Ruby library. The library is very easy to use and well documented making the developer’s job easy. It helps developers to create a highly flexible PDF document generation system with ease. 

The Prawn library has encompassed several important features that enable software developers to handle their PDF documents with ease, such as PDF creation from scratch, PNG and JPG image embedding,  add vector drawing  to PDF,  using built-in fonts and TrueType fonts, encryption support,  secure PDF document using a password, text rendering support, UTF-8 based fonts support, right-to-left text rendering, outlines support and many more.

Previous Next

Getting Started with Prawn

To install the Prawn on your system, please run the following command, 

Install Prawn via RubyGems

gem install prawn 

Create PDF Files via Ruby Library

The Open source Ruby library Prawn enables software developers to programmatically create PDF documents using a couple of lines of Ruby code. The library has provided 3 different ways for PDF document creation with default settings and fonts. These are through assignment, implicit block, or explicit block.  Once created you can easily access and modify each part of your PDF document. You can easily add a new page, set page size, and margins, change font styles, apply formatting, and much more.

Create & Render PDF File via Ruby

  # Using explicit block form and rendering to a file
  content = "Hello World"
  Prawn::Document.generate "example.pdf" do |pdf|
  # self here is left alone
  pdf.font "Times-Roman"
  pdf.draw_text content, :at => [200,720], :size => 32

Insert and Edit Graphic inside PDFs

The Prawn library gives software developers the power to add Vector drawings into their PDF documents using Ruby code. Developers can easily draw lines, polygons, curves, circles, etc. to any place of their choice in a PDF page.  The library has included several important functions for working with graphics such as fill colors, apply dash styles and pattern, set line thickness and apply color to it, use blended mode, apply transformation as well as transparency, and so on.

Using Images in PDF Documents

The Open source Ruby library Prawn has provided support for inserting images into your PDF files using Ruby commands. Currently, the library has included support for JPEG and PNG images. There are several important features provided for managing your images inside a PDF file such as adjusting position, image scaling support, setting image width and height separately, fitting image proportionally, and so on.

Generate Image & Scale to Fit in PDF via Ruby

  Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do
   pigs = "#{Prawn::DATADIR}/images/pigs.jpg"
   image pigs, :at => [50,450], :width => 450

   dice = "#{Prawn::DATADIR}/images/dice.png"
   image dice, :at => [50, 450], :scale => 0.75

Drawing Text on PDF Page

The Prawn library enables software professionals to draw text on a PDF page inside their Ruby application with ease.  You can easily start drawing text on a specified position of a PDF page. You can easily adjust text position, rotate text according to your needs, apply font size, single line and multiple lines support, and much more.

Draw Text on PDF Page via Ruby Library

  def draw_text!(text, options)
   unless font.unicode? || font.class.hide_m17n_warning || text.ascii_only?
    warn "PDF's built-in fonts have very limited support for " \
      "internationalized text.\nIf you need full UTF-8 support, " \
      "consider using an external font instead.\n\nTo disable this " \
      "warning, add the following line to your code:\n" \
      "Prawn::Fonts::AFM.hide_m17n_warning = true\n"

    font.class.hide_m17n_warning = true

   x, y = map_to_absolute(options[:at])
   add_text_content(text, x, y, options)