Skip to content

Commit

Permalink
barcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Nichols committed Nov 22, 2012
1 parent f93540a commit 943dc48
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .gitignore
@@ -1 +1,7 @@
.DS_Store
.DS_Store

.idea

spec/coverage

spec/test_output.pdf
8 changes: 8 additions & 0 deletions README.rdoc
@@ -1,3 +1,8 @@
== WHY FORKED?

forked from jaywhy/pdf-stamper to add barcode generation facilities


= pdf/stamper - PDF Templates
http://github.com/jaywhy/pdf-stamper/
by Jason Yates
Expand All @@ -23,6 +28,9 @@ or Acrobat Professional to create the templates.
pdf.rectangle(140, 380, 50, 13)
pdf.circle(140, 380)

# *** NEW *** !!!! barcodes...
pdf.barcode("PDF417", "2d_barcode", "Barcode data...", aspect_ratio: 0.5)

pdf.save_as "my_output.pdf"

== INSTALL:
Expand Down
24 changes: 24 additions & 0 deletions lib/pdf/stamper.rb
Expand Up @@ -5,6 +5,7 @@
require 'rbconfig'
require 'fileutils'
require 'tmpdir'
require 'active_support/inflector/methods'

include FileUtils

Expand Down Expand Up @@ -72,6 +73,29 @@ def ellipse(x, y, width, height)
def rectangle(x, y, width, height)
@canvas.rectangle(x, y, width, height)
end

# Example
# barcode("PDF417", "2d_barcode", "Barcode data...", aspect_ratio: 0.5)
def barcode(format, key, value, opts = {})
bar = create_barcode(format)
bar.setText(value)
opts.each do |name, opt|
bar.send("set#{name.to_s.camelize}", opt)
end

coords = @form.getFieldPositions(key.to_s)
rect = Rectangle.new(coords[1], coords[2], coords[3], coords[4])

barcode_img = bar.getImage
barcode_img.scalePercent(100, 100 * bar.getYHeight())
barcode_img.setAbsolutePosition(
coords[1] + (rect.width - barcode_img.scaledWidth) / 2,
coords[2] + (rect.height - barcode_img.scaledHeight) / 2
)

cb = @stamp.getOverContent(coords[0].to_i)
cb.addImage(barcode_img)
end

# Saves the PDF into a file defined by path given.
def save_as(file)
Expand Down
23 changes: 13 additions & 10 deletions lib/pdf/stamper/jruby.rb
Expand Up @@ -6,16 +6,15 @@
require 'java'
require 'iText-4.2.0.jar'

include_class 'java.io.FileOutputStream'
include_class 'java.io.ByteArrayOutputStream'
include_class 'com.lowagie.text.pdf.AcroFields'
include_class 'com.lowagie.text.pdf.PdfReader'
include_class 'com.lowagie.text.pdf.PdfStamper'
include_class 'com.lowagie.text.Image'
include_class 'com.lowagie.text.Rectangle'
include_class 'com.lowagie.text.pdf.GrayColor'
java_import 'java.io.FileOutputStream'
java_import 'java.io.ByteArrayOutputStream'
java_import 'com.lowagie.text.Image'
java_import 'com.lowagie.text.Rectangle'
java_import 'com.lowagie.text.pdf.GrayColor'

module PDF
include_package 'com.lowagie.text.pdf'

class Stamper
def initialize(pdf = nil)
template(pdf) if ! pdf.nil?
Expand All @@ -25,9 +24,9 @@ def template(template)
# NOTE I'd rather use a ByteArrayOutputStream. However I
# couldn't get it working. Patches welcome.
#@tmp_path = File.join(Dir::tmpdir, 'pdf-stamper-' + rand(10000).to_s + '.pdf')
reader = PdfReader.new(template)
reader = PDF::PdfReader.new(template)
@baos = ByteArrayOutputStream.new
@stamp = PdfStamper.new(reader, @baos)#FileOutputStream.new(@tmp_path))
@stamp = PDF::PdfStamper.new(reader, @baos)#FileOutputStream.new(@tmp_path))
@form = @stamp.getAcroFields()
@black = GrayColor.new(0.0)
@canvas = @stamp.getOverContent(1)
Expand All @@ -50,6 +49,10 @@ def image(key, image_path)
cb = @stamp.getOverContent(img_field[0].to_i)
cb.addImage(img)
end

def create_barcode(format)
PDF.const_get("Barcode#{format}").new
end

# Takes the PDF output and sends as a string. Basically it's sole
# purpose is to be used with send_data in rails.
Expand Down
4 changes: 4 additions & 0 deletions lib/pdf/stamper/rjb.rb
Expand Up @@ -122,5 +122,9 @@ def add_image_on_page(page, x, y, url)
img.scaleToFit(200,70)
over.addImage(img)
end

def create_barcode(format)
Rjb::import("com.lowagie.text.pdf.Barcode#{format}").new
end
end
end

0 comments on commit 943dc48

Please sign in to comment.