Prawn/Labels takes the guess work out of generating labels using Prawn
Using RubyGems
$ gem install prawn-labels
Using Bundler
gem "prawn-labels"
We've tried to make generating labels as simple as possible with Prawn::Labels.
If you have an object which responds to each
, then you're in business.
require 'prawn/labels'
names = ["Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg"]
Prawn::Labels.generate("names.pdf", names, :type => "Avery5160") do |pdf, name|
pdf.text name
end
This creates a document with a name from the names array in each label. The labels will be formatted for Avery 5160 labels. Formats are defined in the prawn/labels/types.yaml
file, or by loading a custom hash or yaml file
For a full list of examples, take a look in the examples
folder.
class LabelsController < ApplicationController
def fancy_labels
names = ["Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg"]
labels = Prawn::Labels.render(names, :type => "Avery5160") do |pdf, name|
pdf.text name
end
send_data labels, :filename => "names.pdf", :type => "application/pdf"
end
end
Notice here we use the render
method rather than generate
.
Prawn::Labels.generate( "names.pdf", names, :type => "Avery5160",
:shrink_to_fit => true) do |pdf, name|
pdf.text name
end
If the label type you need to use isn't defined in prawn/labels/types.yaml
file, you can define and load your own.
Prawn::Labels.types = '/path/to/custom/types.yaml'
Prawn::Labels.generate("names.pdf", names, :type => "Custom123") do |pdf, name|
pdf.text name
end
or using a hash:
Prawn::Labels.types = {
"QuarterSheet" => {
"paper_size" => "A4",
"columns" => 2,
"rows" => 2
}}
Prawn::Labels.generate("names.pdf", names, :type => "QuarterSheet") do |pdf, name|
pdf.text name
end
Prawn::Labels allows passing a hash of document options all the way through to Prawn.
require 'prawn/labels'
names = ["Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg"]
Prawn::Labels.generate("names.pdf", names, type: "Avery5160", document: { page_layout: :landscape }) do |pdf, name|
pdf.text name
end
Other document properties that can be set via this hash can be found in the Prawn Documentation
Developers, developers, developers!!
Contributors retain copyright to their work but must agree to release their contributions under the same terms as this project. For details, please see the LICENSE file.
If you would like to help with developing Prawn/Labels, please get in touch! Contact Jordan through GitHub (@jordanbyron), Twitter (@jordan_byron) or open up a ticket.
bundle install
in the root directory to install all of the
dependencies.prawn-2-support
).ruby test/suite.rb
. If your changes are not covered, go back to step 6.README
, please update it.Note about our test suite: It kinda stinks :poop: At the moment the tests just do a quick "Does this code run without raising any errors and at the end does it generate the PDF we expect it to generate" check. Eventually I'd like to add more robust tests, but at this point they are better than no tests! So please keep that in mind when you add your tests or run the whole suite.
To see what the test PDFs look like you can append DEBUG=true
to your test run
and all generated PDFs will be opened for visual debugging :thumbsup: