sermoa / pdf_label_maker

Generates PDFs that can print to Avery labels.

This URL has Read+Write access

pdf_label_maker / README
100644 101 lines (69 sloc) 2.679 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
PdfLabelMaker
=============
 
Generates PDFs that can print to Avery labels.
 
Requires pdf-writer (gem install pdf-writer).
 
 
Example
=======
 
Get a collection of contacts and send them to the label maker, with the Avery code of the size labels. For example:
 
  class ContactsController < ApplicationController
 
    def index
      @contacts = Contact.all.ordered
      respond_to do |format|
        format.pdf do
          pdf = PdfLabelMaker.avery_labels(@contacts)
          send_data pdf.render, :filename => 'labels.pdf', :type => 'application/pdf'
        end
        format.html
      end
    end
    
  end
 
This would be found at /contacts.pdf
 
Your model needs to implement a 'label_lines' method which returns an array - one element for each line to appear on the label.
 
  class Contact < ActiveRecord::Base
    
    def label_lines
      returning Array.new do |arr|
        arr << full_name
        address.split("\r\n").each{|line| arr << line}
      end
    end
 
  end
 
 
Options
=======
 
Send the label code that you want printed. Default is 'L7162' which is 16 labels per page.
 
  pdf = PdfLabelMaker.avery_labels(@contacts, 'J8651')
 
This would print the J8651 format, which is 65 little labels per page.
 
Not all label formats are supported yet, but you could send your own if necessary. Just send a bunch of options, like this:
 
  class ContactsController < ApplicationController
 
    require 'pdf/writer'
    include PDF
      
    def index
      @contacts = Contact.all.ordered
      respond_to do |format|
        format.pdf do
          options = {
            :columns => 3,
            :labels_per_page => 30,
            :left_margin => Writer.mm2pts(4),
            :bottom_margin => Writer.mm2pts(25),
            :label_width => Writer.mm2pts(67),
            :label_height => Writer.mm2pts(26.3),
            :label_padding_x => Writer.mm2pts(4),
            :label_padding_y => Writer.mm2pts(3),
            :gap_between_labels_x => Writer.mm2pts(4),
            :font_size => 9,
            :line_height => 10
          }
          pdf = PdfLabelMaker.avery_labels(@contacts, 'user-defined', options)
          send_data pdf.render, :filename => 'labels.pdf', :type => 'application/pdf'
        end
        format.html
      end
    end
    
  end
 
 
You can also override any of the settings, just like this:
 
  pdf = PdfLabelMaker.avery_labels(@contacts, 'L7162', {:font_size => 20, :font => 'Times-Roman'})
 
 
Credit
======
 
Thanks to Nick Sieger for the example, and the code that got me started.
http://blog.nicksieger.com/articles/2006/05/26/rails-is-simpler-than-office
 
 
Copyright (c) 2008 Aimee Daniells, released under the MIT license