Class: PDF::Stamper
- Inherits:
-
Object
- Object
- PDF::Stamper
- 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.0"
Instance Method Summary collapse
- #add_image_on_page(page, x, y, url) ⇒ Object
- #add_images(images) ⇒ Object
-
#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.
-
#save_as(file) ⇒ 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 ⇒ 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 |
# 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') template(pdf) if ! pdf.nil? end |
Instance Method Details
#add_image_on_page(page, x, y, url) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/pdf/stamper/rjb.rb', line 110 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
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 102 103 104 105 106 107 108 |
# File 'lib/pdf/stamper/rjb.rb', line 71 def add_images(images) basefont = @basefont_class.createFont(@basefont_class.HELVETICA, @basefont_class.CP1252, @basefont_class.NOT_EMBEDDED) image_size = [] image_size[0] = @pagesize.width() / 2 image_size[1] = (@pagesize.height() / 2) - 25 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], (image_size[1] + 25)) case n when 0 img.setAbsolutePosition(0, (image_size[1] + 25)) when 1 img.setAbsolutePosition(image_size[0], (image_size[1] + 25)) when 2 img.setAbsolutePosition(0, 25) when 3 img.setAbsolutePosition(image_size[0], 25) 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] / 2)), (img.getAbsoluteY() - 20), 0) over.endText() end end end end end |
#image(key, image_path) ⇒ Object
Set a button field defined by key and replaces with an image.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/pdf/stamper/rjb.rb', line 46 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 |
#save_as(file) ⇒ Object
Saves the PDF into a file defined by path given.
45 46 47 |
# File 'lib/pdf/stamper.rb', line 45 def save_as(file) File.open(file, "wb") { |f| f.write to_s } end |
#template(template) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/pdf/stamper/rjb.rb', line 36 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() 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_s ⇒ Object
Takes the PDF output and sends as a string. Basically it’s sole purpose is to be used with send_data in rails.
66 67 68 69 |
# File 'lib/pdf/stamper/rjb.rb', line 66 def to_s fill @baos.toByteArray end |