Skip to content

Instantly share code, notes, and snippets.

@ahpex
Created February 5, 2012 18:22
Show Gist options
  • Save ahpex/1747015 to your computer and use it in GitHub Desktop.
Save ahpex/1747015 to your computer and use it in GitHub Desktop.
JRuby IText Example Snippet
require "java"
Dir["/home/michael/Downloads/\*.jar"].each { |jar| require jar }
import com.itextpdf.text.BaseColor
import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.Phrase
import com.itextpdf.text.pdf.PdfPCell
import com.itextpdf.text.pdf.PdfPTable
import java.io.FileOutputStream
pdf = Document.new
para = Paragraph.new "Brought to you by JRuby"
file = FileOutputStream.new "pdf_demo.pdf"
table = PdfPTable.new 3
table.setWidthPercentage 100
table.setHeaderRows 2
table.setFooterRows 1
cell = PdfPCell.new Phrase.new "Cell with colspan = 3"
cell.setColspan 3
cell.setBackgroundColor BaseColor::CYAN
table.addCell cell
(1..300).each do |i|
cell = PdfPCell.new Phrase.new "cell#{i}"
cell.setBackgroundColor BaseColor::ORANGE
table.addCell cell
end
com.itextpdf.text.pdf.PdfWriter.get_instance pdf, file
pdf.open
pdf.add para
pdf.add table
pdf.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment