Skip to content

tomascco/digital_document

Repository files navigation

DigitalDocument

Low level gem for representing and creating PDFs with Ruby

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add digital_document

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install digital_document

Usage

This gem maps PDF Objects to Ruby data types and objects, allowing the generation of PDFs with ease, without dependencies and using only Ruby!

PDF Ruby
Boolean TrueClass, FalseClass
Null NilClass
Numeric Float, Integer
String String
Name Symbol
Array Array
Dictionary Hash
Stream DigitalDocument::PDF::Stream
Reference DigitalDocument::PDF::Reference

A document can be created instantiating DigitalDocument::PDF, passing an Array to the constructor with objects to build the PDF, as in the example below:

pdf = DigitalDocument::PDF.new([
  {Type: :Catalog, Pages: DigitalDocument::PDF::Reference.new(2)},
  {Type: :Pages, Kids: [DigitalDocument::PDF::Reference.new(3)], Count: 1},
  {Type: :Page, MediaBox: [0, 0, 595.44, 841.68], Contents: DigitalDocument::PDF::Reference.new(4)},
  DigitalDocument::PDF::Stream.new("1.0 0.0 0.0 RG\n0.5 0.75 1.0 rg\n97.72 220.84 400 400 re\nB"),
])

DigitalDocument::PDF::Save.to_file("demo.pdf", pdf: pdf)

The example above produces the following PDF:

image

This lib could also be used to build higher level abstractions to create PDF. As an example, the class DigitalDocument::Simple has the capability to represent documents with more ease and DigitalDocument::Simple::ToPDF could be used to convert it into an PDF:

document = DigitalDocument::Simple.new
document.pages << DigitalDocument::Simple::Page.new("1.0 0.0 0.0 RG\n0.5 0.75 1.0 rg\n97.72 220.84 400 400 re\nB")
pdf = DigitalDocument::Simple::ToPDF.call(document)
DigitalDocument::PDF::Save.to_file("test.pdf", pdf: pdf)

This code creates the same PDF file as above.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tomascco/digital_document. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the 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 DigitalDocument project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published