Class: PDF::Stamper

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/stamper/rjb.rb,
lib/pdf/stamper.rb,
lib/pdf/stamper/jruby.rb

Overview

PDF::Stamper::RJB

RJB needs the LD_LIBRARY_PATH and JAVA_HOME environment set for it to work correctly. For example on my system:

export LD_LIBRARY_PATH=/usr/java/jdk1.6.0/jre/lib/i386/:/usr/java/jdk1.6.0/jre/lib/i386/client/:./ export JAVA_HOME=/usr/java/jdk1.6.0/

Check the RJB documentation if you are having issues with this.

Constant Summary collapse

VERSION =
"0.3.6"

Instance Method Summary collapse

Constructor Details

#initialize(pdf = nil) ⇒ Stamper

Returns a new instance of Stamper.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pdf/stamper/rjb.rb', line 21

def initialize(pdf = nil, options = {})
  @bytearray    = Rjb::import('java.io.ByteArrayOutputStream')
  @filestream   = Rjb::import('java.io.FileOutputStream')
  @acrofields   = Rjb::import('com.lowagie.text.pdf.AcroFields')
  @pdfreader    = Rjb::import('com.lowagie.text.pdf.PdfReader')
  @pdfstamper   = Rjb::import('com.lowagie.text.pdf.PdfStamper')
  @pdfwriter    = Rjb::import('com.lowagie.text.pdf.PdfWriter')
  @image_class  = Rjb::import('com.lowagie.text.Image')
  @pdf_content_byte_class = Rjb::import('com.lowagie.text.pdf.PdfContentByte')
  @basefont_class = Rjb::import('com.lowagie.text.pdf.BaseFont')
  @rectangle = Rjb::import('com.lowagie.text.Rectangle')
  @gray_color = Rjb::import('com.lowagie.text.pdf.GrayColor')

  template(pdf) if ! pdf.nil?
end

Instance Method Details

#add_image_on_page(page, x, y, url) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/pdf/stamper/rjb.rb', line 122

def add_image_on_page(page, x, y, url)
  over = @stamp.getOverContent(page)
  img = @image_class.getInstance(url)
  img.setAbsolutePosition(x,y)
  img.scaleToFit(200,70)
  over.addImage(img)
end

#add_images(images) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pdf/stamper/rjb.rb', line 78

def add_images(images)
  basefont = @basefont_class.createFont(@basefont_class.HELVETICA, @basefont_class.CP1252, @basefont_class.NOT_EMBEDDED)
  image_size = []
  half_page_width = @pagesize.width() / 2
  half_page_height = @pagesize.height() / 2
  image_size[0] = half_page_width - 80
  image_size[1] = half_page_height - 80
  pages = (images.length / 4.0).ceil
  pages.times do |index|
    page_number = index + @numpages + 1
    image_index = index * 4
    @stamp.insertPage(page_number, @pagesize)
    over = @stamp.getOverContent(page_number)
    over.setFontAndSize(basefont, 12.0)
    4.times do |n|
      if pdf_image = images[image_index + n]
        if image_path = pdf_image[0]
          img = @image_class.getInstance(image_path)
          img.scaleToFit(image_size[0] + 30, (image_size[1]))
          img_x_offset = (half_page_width - image_size[0]) / 2
          img_y_offset = (half_page_height - img.getScaledHeight()) / 2
          case n
          when 0
            img.setAbsolutePosition(img_x_offset, (half_page_height + img_y_offset))
          when 1
            img.setAbsolutePosition((half_page_width + (img_x_offset - 30)), (half_page_height + img_y_offset))
          when 2
            img.setAbsolutePosition(img_x_offset, img_y_offset)
          when 3
            img.setAbsolutePosition((half_page_width + (img_x_offset - 30)), img_y_offset)
          end
          over.addImage(img)
        end
        if image_label = pdf_image[1]
          over.beginText()
          over.showTextAligned(@pdf_content_byte_class.ALIGN_CENTER, image_label, (img.getAbsoluteX() + ((image_size[0] + 30) / 2)), (img.getAbsoluteY() - 15), 0)
          over.endText()
        end
      end
    end
  end
  @stamp.setFullCompression()
end

#checkbox(key) ⇒ Object

Set a checkbox to checked



45
46
47
48
49
50
51
52
53
54
# File 'lib/pdf/stamper.rb', line 45

def checkbox(key)
  field_type = @form.getFieldType(key.to_s)
  return unless is_checkbox(field_type)

  all_states = @form.getAppearanceStates(key.to_s)
  yes_state = all_states.reject{|x| x == "Off"}
  
  
  @form.setField(key.to_s, yes_state.first) unless (yes_state.size == 0)
end

#circle(x, y, r, page = 1) ⇒ Object



64
65
66
67
# File 'lib/pdf/stamper.rb', line 64

def circle(x, y, r, page=1)
  update_canvas_list(page)
  @canvas_list[page].circle(x, y, r)
end

#ellipse(x, y, width, height, page = 1) ⇒ Object



69
70
71
72
# File 'lib/pdf/stamper.rb', line 69

def ellipse(x, y, width, height, page=1)
  update_canvas_list(page)
  @canvas_list[page].ellipse(x, y, x + width, y + height)
end

#get_checkbox_values(key) ⇒ Object

Get checkbox values



57
58
59
60
61
62
# File 'lib/pdf/stamper.rb', line 57

def get_checkbox_values(key)
  field_type = @form.getFieldType(key.to_s)
  return unless is_checkbox(field_type)

  @form.getAppearanceStates(key.to_s)
end

#image(key, image_path) ⇒ Object

Set a button field defined by key and replaces with an image.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pdf/stamper/rjb.rb', line 53

def image(key, image_path)
  # Idea from here http://itext.ugent.be/library/question.php?id=31 
  # Thanks Bruno for letting me know about it.
  img = @image_class.getInstance(image_path)
  img_field = @form.getFieldPositions(key.to_s)

  
  rect = @rectangle.new(img_field[1], img_field[2], img_field[3], img_field[4])
  img.scaleToFit(rect.width, rect.height)
  img.setAbsolutePosition(
    img_field[1] + (rect.width - img.getScaledWidth) / 2,
    img_field[2] + (rect.height - img.getScaledHeight) /2
  )

  cb = @stamp.getOverContent(img_field[0].to_i)
  cb.addImage(img)
end

#is_checkbox(field_type) ⇒ Object



37
38
39
# File 'lib/pdf/stamper/rjb.rb', line 37

def is_checkbox(field_type)
  field_type == @acrofields.FIELD_TYPE_CHECKBOX
end

#rectangle(x, y, width, height, page = 1) ⇒ Object



74
75
76
77
# File 'lib/pdf/stamper.rb', line 74

def rectangle(x, y,  width, height, page=1)
  update_canvas_list(page)
  @canvas_list[page].rectangle(x, y, width, height)
end

#save_as(file) ⇒ Object

Saves the PDF into a file defined by path given.



80
81
82
# File 'lib/pdf/stamper.rb', line 80

def save_as(file)
  File.open(file, "wb") { |f| f.write to_s }
end

#template(template) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/pdf/stamper/rjb.rb', line 41

def template(template)
  reader = @pdfreader.new(template)
  @pagesize = reader.getPageSize(1)
  @numpages = reader.getNumberOfPages()
  @baos = @bytearray.new
  @stamp = @pdfstamper.new(reader, @baos)
  @form = @stamp.getAcroFields()
  @black = @gray_color.new(0.0)
  @canvas_list = { 1 => @stamp.getOverContent(1) }
end

#text(key, value) ⇒ Object

Set a textfield defined by key and text to value.



40
41
42
# File 'lib/pdf/stamper.rb', line 40

def text(key, value)
  @form.setField(key.to_s, value.to_s) # Value must be a string or itext will error.
end

#to_sObject

Takes the PDF output and sends as a string. Basically it’s sole purpose is to be used with send_data in rails.



73
74
75
76
# File 'lib/pdf/stamper/rjb.rb', line 73

def to_s
  fill
  @baos.toByteArray
end