Class: PDF::Stamper
- Inherits:
-
Object
- Object
- PDF::Stamper
- Defined in:
- lib/pdf/stamper/rjb.rb,
lib/pdf/stamper.rb,
lib/pdf/stamper/jruby.rb,
lib/pdf/stamper/version.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.9"
Instance Method Summary collapse
- #add_image_on_page(page, x, y, url) ⇒ Object
- #add_images(images) ⇒ Object
-
#checkbox(key) ⇒ Object
Set a checkbox to checked.
- #circle(x, y, r, page = 1) ⇒ Object
- #ellipse(x, y, width, height, page = 1) ⇒ Object
-
#get_checkbox_values(key) ⇒ Object
Get checkbox values.
-
#image(key, image_path) ⇒ Object
Set a button field defined by key and replaces with an image.
-
#initialize(pdf = nil) ⇒ Stamper
constructor
A new instance of Stamper.
- #is_checkbox(field_type) ⇒ Object
- #rectangle(x, y, width, height, page = 1) ⇒ Object
-
#save_as(file, flatten_form = true) ⇒ Object
Saves the PDF into a file defined by path given.
- #template(template) ⇒ Object
-
#text(key, value) ⇒ Object
Set a textfield defined by key and text to value.
-
#to_s(flatten_form = true) ⇒ Object
Takes the PDF output and sends as a string.
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, = {}) @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
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pdf/stamper.rb', line 43 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
62 63 64 65 |
# File 'lib/pdf/stamper.rb', line 62 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
67 68 69 70 |
# File 'lib/pdf/stamper.rb', line 67 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
55 56 57 58 59 60 |
# File 'lib/pdf/stamper.rb', line 55 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
72 73 74 75 |
# File 'lib/pdf/stamper.rb', line 72 def rectangle(x, y, width, height, page=1) update_canvas_list(page) @canvas_list[page].rectangle(x, y, width, height) end |
#save_as(file, flatten_form = true) ⇒ Object
Saves the PDF into a file defined by path given.
78 79 80 |
# File 'lib/pdf/stamper.rb', line 78 def save_as(file, flatten_form=true) File.open(file, "wb") { |f| f.write to_s(flatten_form) } 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.
38 39 40 |
# File 'lib/pdf/stamper.rb', line 38 def text(key, value) @form.setField(key.to_s, value.to_s) # Value must be a string or itext will error. end |
#to_s(flatten_form = true) ⇒ Object
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 |