puzzle / prawn-markup

Parse simple HTML markup to include in Prawn PDFs
MIT License
65 stars 16 forks source link

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>')

Supported HTML

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

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. Another exception is the rgb or cmyk properties of the Prawn-specific color tag.

If no explicit loader is given (see below), 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.

Example

Have a look at showcase.html, which is rendered by the corresponding spec. Uncomment the lookatit call there to directly open the generated PDF when running the spec with spec spec/prawn/markup/showcase_spec.rb.

Formatting Options

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, heading[1-6], table (subkeys cell and header) and list (subkeys content and bullet).

Text and heading 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:

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.