PrawnVectorImport icon indicating copy to clipboard operation
PrawnVectorImport copied to clipboard

Reads an EPS/Illustrator/PDF file and generates code for rendering in Prawn the vector content of that file

= PrawnVectorImport: import Adobe Illustrator / EPS / PDF Vector Shapes for Use with Prawn

PrawnVectorImport can import vector shapes, lines, stroke and fill color, and dash settings from PDF documents and PDF compatible Illustrator files.

= Motivation

I needed to include logos into PDF documents generated by Prawn. You could also use this to embed other pre-rendered vector graphics.

= Usage

PrawnVectorImport requires the PDF::Reader gem:

$ gem install pdf-reader

First, generate your Adobe Illustrator / PDF file. If using Illustrator, when you save, save as PDF compatible, uncompressed. NOTE: if the graphic includes text that you want imported, convert that text to outlines (Type > Create Outlines) before importing it.

Use PrawnVectorImport from the command line to generate a ruby file:

$ cd PrawnVectorImport/ $ ruby bin/import_vectors.rb path_to_ai_or_pdf_file method_name > method_name.rb

The resulting ruby file is an extension to the Prawn::Graphics module. It contains a method named <method_name> (the second argument passed to bin/import_vectors.rb) that takes three optional parameters. Include the file in your project and call the method on a Prawn document object.

If you have Prawn installed in the same directory as PrawnVectorImport, then here is an easy way to test the results:

$ cd PrawnVectorImport/ $ ruby bin/import_vectors.rb ./examples/vector_import_test.ai my_vector_graphics > my_vector_graphics.rb $ cp my_vector_graphics.rb ../prawn/examples/ $ cp examples/prawn_vector_import.rb ../prawn/examples/ $ cd ../prawn $ ruby examples/prawn_vector_import.rb

See the ./examples/prawn_vector_import.rb file for an example of how the generated method is called.

= Hints

  • Make certain that all of the artwork you want to appear is contained within the artboard.

  • Positioning is based on the lower left corner of the Illustrator document and from the lower left corner of the Prawn bounds.

  • Use Prawn::Graphics::Transformation#translate and Prawn::Graphics::Transformation#scale to adjust the position and scale of "imported" graphics within your document

  • Delete hidden layers. Artwork that is present, but not visible, may be drawn, creating a larger ruby file and final PDF unnecessarily.

  • Again, convert type to outlines prior to importing if you want the text to appear.

= Limitations

Doesn't yet handle transparency.

Not intended to incorporate text or rasterized images (see hints section, above)

= Changelog

2009-12-14

  • CMYK fixed (Michaël Witrant)
  • Command line now takes name of method to create (Michaël Witrant)

2010-03-08

  • use transformation_matrix code added to Prawn by Michaël Witrant