Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
namespace :pdf do task :load_stuff => :environment do require 'rubygems' require 'rjb' # Rjb::load(classpath = '.', jvmargs=[]) my_path = File.dirname(File.expand_path(__FILE__)) load_path = File.join(my_path, '..', 'iText-2.1.4.jar') puts load_path options = [] Rjb::load load_path, options # => nil FileOutputStream = Rjb::import('java.io.FileOutputStream') PdfWriter = Rjb::import('com.lowagie.text.pdf.PdfWriter') PdfReader = Rjb::import('com.lowagie.text.pdf.PdfReader') PdfCopy = Rjb::import('com.lowagie.text.pdf.PdfCopy') PdfImportedPage = Rjb::import('com.lowagie.text.pdf.PdfImportedPage') Document = Rjb::import('com.lowagie.text.Document') Paragraph = Rjb::import('com.lowagie.text.Paragraph') end desc 'Demo' task :build => :load_stuff do # Count the users and signs for the initial PDFs [ [Sign, 'signs'], [User, 'users'] ].each do |object, label| document = Document.new() PdfWriter.getInstance(document, FileOutputStream.new("#{label}.pdf")) document.open document.add(Paragraph.new("Hello - I have #{object.count} #{label}")) document.close end end desc 'Combine two PDF files' task :combine => :build do begin output_file = 'output.pdf' read_from_files = %w{signs.pdf users.pdf} document = nil document = Document.new copier = PdfCopy.new( document, FileOutputStream.new(output_file) ) read_from_files.each do |read_target| reader = PdfReader.new(read_target) document.open n_pages = reader.getNumberOfPages n_pages.times do |i| copier.addPage( copier.getImportedPage(reader, i+1) ) if copier end end rescue Exception => e puts "#{e.inspect}" ensure document.close end end end
This paste will be private.
From the Design Piracy series on my blog: