Prawn::Markup

Build Status Maintainability Test Coverage

Adds simple HTML snippets into Prawn-generated PDFs. All elements are layouted vertically using Prawn's formatting options. A major use case for this gem is to include WYSIWYG-generated HTML parts into server-generated PDF documents.

This gem does not and will never convert entire HTML + CSS pages to PDF. Use wkhtmltopdf for that. Have a look at the details of the Supported HTML.

Installation

Add this line to your application's Gemfile:

gem 'prawn-markup'

And then execute:

$ bundle

Or install it yourself as:

$ gem install prawn-markup

Usage

In your prawn Document, add HTML like this:

doc = Prawn::Document.new
doc.markup('<p>Hello World</p><hr/><p>KTHXBYE</p>')

To customize element formatting, do:

doc = Prawn::Document.new
# set options for the entire document
doc.markup_options = {
  text: { font: 'Times' },
  table: { header: { style: :bold, background_color: 'FFFFDD' } }
}
# set additional options for each single call
doc.markup('<p>Hello World</p><hr/><p>KTHXBYE</p>', text: { align: :center })

Options may be set for text, table (cell and header) and list (content and bullet).

Text options include all keys from Prawns #text method: font, size, color, style, align, valign, leading,direction, character_spacing, indent_paragraphs, kerning, mode.

Tables and lists are rendered with prawn-table and have the following additional options: padding, borders, border_width, border_color, background_color, border_lines, rotate, overflow, min_font_size. Options from text may be overridden.

Beside these options handled by Prawn / prawn-table, the following values may be customized:

  • [:text][:preprocessor]: A proc/callable that is called each time before a chunk of text is rendered.
  • [:text][:margin_bottom]: Margin after each <p>, <ol>, <ul> or <table>. Defaults to about half a line.
  • [:heading1-6][:margin_top]: Margin before a heading. Default is 0.
  • [:heading1-6][:margin_bottom]: Margin after a heading. Default is 0.
  • [:table][:placeholder][:too_large]: If the table content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[table content too large]'.
  • [:table][:placeholder][:subtable_too_large]: If the content of a subtable cannot be fitted into the table, this text is rendered instead. Defaults to '[nested tables with automatic width are not supported]'.
  • [:list][:vertical_margin]: Margin at the top and the bottom of a list. Default is 5.
  • [:list][:bullet][:char]: The text used as bullet in unordered lists. Default is '•'.
  • [:list][:bullet][:margin]: Margin before the bullet. Default is 10.
  • [:list][:content][:margin]: Margin between the bullet and the content. Default is 10.
  • [:list][:placeholder][:too_large]: If the list content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[list content too large]'.
  • [:image][:loader]: A callable that accepts the src attribute as an argument an returns a value understood by Prawn's image method. Loads http(s) URLs and base64 encoded data URIs by default.
  • [:image][:placeholder]: If an image is not supported, this text/callable is rendered instead. Defaults to '[unsupported image]'.
  • [:iframe][:placeholder]: If the HTML contains IFrames, this text/callable is rendered instead. A callable gets the URL of the IFrame as an argument. Defaults to ignore iframes.

Supported HTML

This gem parses the given HTML and layouts the following elements in a vertical order:

  • Text blocks: p, div, ol, ul, li, hr, br
  • Text semantics: a, b, strong, i, em, u, s, del, sub, sup
  • Headings: h1, h2, h3, h4, h5, h6
  • Tables: table, tr, td, th
  • Media: img, iframe

All other elements are ignored, their content is added to the parent element. With a few exceptions, no CSS is processed. One exception is the width property of img, td and th, which may contain values in cm, mm, px, pt, % or auto.

If no explicit loader is given (see above), images are loaded from http(s) addresses or may be contained in the src attribute as base64 encoded data URIs. Prawn only supports PNG and JPG.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/puzzle/prawn-markup. For pull requests, add specs, make sure all of them pass and fix all rubocop issues.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Prawn::Markup project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.