Aaron Johnson Now with 50% less caffeine!

Posted
14 March 2004 @ 8pm

Tagged
ColdFusion, J2EE, Software Development

Using iText PDF & ColdFusion

Mike Steele sent me an email in reference to an article I wrote for the ColdFusion Developer’s Journal a year or so ago. In the email, he mentions that he is trying to use the iText Java-PDF library with ColdFusion MX:

… The getInstance method is static and according to your July 2003 CFDJ article, you can’t instantiate an object in CF this way.

In the article I said this:

… using the CreateObject() function does not get you access to an instance of an object. In order to access a Java object, you must either a) first call the CreateObject() method and then the init() method, which in the above example, maps to the default constructor in Java, or b) call any nonstatic method on the object, which causes ColdFusion to then instantiate the object for you.

I guess this statement needs to be amended to include a third possible, but not always valid solution: call a static method on the class which returns an instance of the object in question. In this case the API designer included a static method ‘getInstance()’ on the PDFWriter class. Given that news, you can take the quick example that the author of the iText library gives here to create a PDF in a snap using ColdFusion:

<cfscript>
// create a 'Document' object
document = CreateObject("java", "com.lowagie.text.Document");
document.init();
// get an outputstream for the PDF Writer
fileIO = CreateObject("java", "java.io.FileOutputStream");
// call the constructor, pass the location where you want
// the pdf to be created
fileIO.init("C:\myhost.com\somedir\test.pdf");
// get a PDF Writer var
writer = CreateObject("java", "com.lowagie.text.pdf.PdfWriter");
// call the static 'getInstance' factory method
writer.getInstance(document, fileIO);
// open the document
document.open();
// create a new paragraph
paragraph = CreateObject("java", "com.lowagie.text.Paragraph");
paragraph.init("Hello World!");
// add the paragraph
document.add(paragraph);
// close the document (PDF Writer is listening and will automatically
// create the PDF for us
document.close();
</cfscript>

Copy that code into a cfml page and make sure you’ve downloaded the iText jar to the /lib/ directory of your ColdFusion server and you should be able to create PDF’s in a jiffy!

Full source code available here.


27 Comments

Posted by
Vui Lo
14 March 2004 @ 10pm

Good work, Mr. Johnson. I wonder if you’d come up a sample for a related library: JasperReport with CFMX? I’ve tried but with no success. Thank you.


Posted by
Kurt Wiersma
15 March 2004 @ 3pm

I too I have tried to get JasperReports working with CF and have found that it doesn’t work due to problems with commons-logging version in campatiablities.

Our work around was to make a standard Java web app and deploy that war file to the JRun server. Fortunatly we have CF Enterprise so we can do this since we have a full version of JRun.


Posted by
Spectrum
18 March 2004 @ 1pm

hi

please do you can explain WHERE i need to install iText files? All files? In CFMX directory? Please, i’m a newbie in Java development and i’ll appreciate to know iText, that powerfull tool. Thanx in advance and congratulations for all resources…


Posted by
AJ
21 March 2004 @ 4pm

hi Spectrum,

The iText.jar file needs to be copied to the {cfusion}/WEB-INF/lib/ directory… restart ColdFusion and you should be able to use it right away!

AJ


Posted by
sridhar
31 March 2004 @ 3am

thanks..
Iam using cfml for generating reports.can u help me in writing the resultset to pdf file.software is oracle9i and coldfusionmx.


Posted by
Ben
20 July 2004 @ 9am

I have gotten and used jasperreports with CFMX. The key is setting your jave parameters to headless.
I design with iReport and created a tag that executes the report and saves it to a pdf.

I need to make this tag and stuff more stable, but if I get enough request, I might be motivated to get a doc written up and the tag done quicker.


Posted by
Ben
27 July 2004 @ 3pm

I have added a tag that takes a jasperreports xml file and turns it into a pdf file. You can find it on the Macromedia Exchange.. called Pyro_JasperReports.


Posted by
nravo
26 November 2004 @ 8am

Can iText convert HTML to PDF? I couldn’t find any info on this?


Posted by
Paul
13 December 2004 @ 8am

I copied all the *.java files into the {cfusionmx}/wwwroot/WEB-INF/lib/ directory and restarted the server.. However, I get the following error:
Object Instantiation Exception.
Class not found: com.lowagie.text.Document

Anyone any idea what might be causing this???

Paul - Java Newbie


Posted by
James Holmes
15 December 2004 @ 8am

Don’t put the classes in the web-inf/lib/ dir - just put the whole jar file in there and the axample will work as written.


Posted by
Minhaj
10 January 2005 @ 1pm

Just put the Jar file in “CfusionMX\runtime\jre\lib” and also put an entry into ColdFusion Admin Classpath.


Posted by
Raja
15 February 2005 @ 3pm

Will this work with ColdFusion 5?


Posted by
Giampaolo Bellavite
18 June 2005 @ 5am

Note that iText jars with ColdFusion 7 don’t work due to compatibility issues with the iText version already installed and used by the tag. You have to refactor iText with another name to use it on CFMX7.


Posted by
cMiller
2 August 2005 @ 6am

How do you refactor itext as you described? I stopped CF server, removed the old version, and replaced it with the new version and it seemed to work. MM said absolutely not to do this but I don’t use their CFDocument tag or reporting tags, so I don’t think it will be an issue? Any ideas, should I do this refactoring you mentioned?


Posted by
Chris Miller
2 August 2005 @ 6am

My latest troubles involve merging pdf form fields from within cold fusion, can someone paste the code that makes this possible? I’ve expermineted with tons of variations, but would like someone elses fresh mind helping.


Posted by
Rick Root
5 August 2005 @ 11am

Hi,

I wanted to post a quick response to something I saw in the comments.

jasperReports DOES work with Coldfusion MX 6.1 - we use it in my department here at Duke, and I wrote a Blog entry about it here:

http://www.rickroot.com/blog/1/2004/05/Using-JasperReports-with-Coldfusion-MX-61.cfm


Posted by
Jeffrey Roberson
22 September 2005 @ 11am

Anyone have any luck with refactoring iText for CFMX7?

Their cfdocument tag is terrible….


Posted by
Jeffrey Roberson
22 September 2005 @ 11am

Check this out CFMX 7 users


Posted by
Iain B
5 October 2005 @ 10am

Thanks for the script - it took me a long way towards my own project, which i’ve included the code for.

Have you got any ideas on how to use the table.writeSelectedRows(0, -1, 50, pos, writer.getDirectContent()); as I get an error

Class coldfusion.runtime.java.JavaProxy can not access a member of class com.lowagie.text.pdf.PdfWriter with modifiers “protected”

// create a ‘Document’ object
rectangle = CreateObject(”java”, “com.lowagie.text.Rectangle”);
pageSize = Rectangle.init(240, 155);

Chunk = CreateObject(”java”, “com.lowagie.text.Chunk”);
Element = CreateObject(”java”, “com.lowagie.text.Element”);

CMYKcolor = CreateObject(”java”, “com.lowagie.text.pdf.CMYKColor”);
document = CreateObject(”java”, “com.lowagie.text.Document”).init(pageSize, 0, 0, 0, 0);
Image = CreateObject(”java”, “com.lowagie.text.Image”);

BaseFont = CreateObject(”java”, “com.lowagie.text.pdf.BaseFont”);
Font = CreateObject(”java”, “com.lowagie.text.Font”);
arial = BaseFont.createFont(#expandpath(”TGL.TTF”)#, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
arial = Font.init(arial, 12);

PdfPCell = CreateObject(”java”, “com.lowagie.text.pdf.PdfPCell”);
PdfPTable = CreateObject(”java”, “com.lowagie.text.pdf.PdfPTable”);

fileIO = CreateObject(”java”, “java.io.FileOutputStream”);
// call the constructor, pass the location where you want
// the pdf to be created
fileIO.init(”#expandpath(”test2.pdf”)#”);
// get a PDF Writer var
writer = CreateObject(”java”, “com.lowagie.text.pdf.PdfWriter”);
// call the static ‘getInstance’ factory method
writer.getInstance(document, fileIO);

// open the document
document.open();
// create a new paragraph
paragraph = CreateObject(”java”, “com.lowagie.text.Paragraph”);
phrase = CreateObject(”java”, “com.lowagie.text.Phrase”);

phrase0 = paragraph.init(” “);

phrase = paragraph.init(”#url.name#”, arial);
phrase.setAlignment(Element.ALIGN_LEFT);

phrase2 = paragraph.init(”#url.jobtitle#”, arial);
phrase2.setAlignment(Element.ALIGN_LEFT);

phrase3 = paragraph.init(”#url.clubname#”, arial);
phrase3.setAlignment(Element.ALIGN_LEFT);

phrase4 = paragraph.init(”#url.address1#”, arial);
phrase4.setAlignment(Element.ALIGN_LEFT);

phrase5 = paragraph.init(”#url.address2#”, arial);
phrase5.setAlignment(Element.ALIGN_LEFT);

phrase6 = paragraph.init(”#url.postcode#”, arial);
phrase6.setAlignment(Element.ALIGN_LEFT);

phrase7 = paragraph.init(”T : #url.telephone#”, arial);
phrase7.setAlignment(Element.ALIGN_LEFT);

phrase8 = paragraph.init(”F : #url.fax#”, arial);
phrase8.setAlignment(Element.ALIGN_LEFT);

phrase9 = paragraph.init(”E : #url.email#”, arial);
phrase9.setAlignment(Element.ALIGN_LEFT);

phrase10 = paragraph.init(”www.holmesplace.com”, arial);
phrase10.setAlignment(Element.ALIGN_LEFT);

//add the image
png = Image.getInstance(”#expandpath(”businesscardside1.tif”)#”);
png.setAbsolutePosition(0, 0);
png.scaleToFit(240, 156);
png.setDpi(300,300);
png.setAlignment(Image.UNDERLYING);

document.add(png);

table = PdfPTable.init(1);
cell = PdfPCell.init(Paragraph.init(”", arial));
cell.setColspan(1);
cell.setBorderWidth( 0.0 );
table.getDefaultCell().setBorderWidth( 0.0 );
table.getDefaultCell().setMinimumHeight(2);
table.addCell(cell);
table.addCell(” “);
table.addCell(” “);
cell.setFixedHeight(1);
table.addCell(phrase);
cell.setFixedHeight(2);
table.addCell(phrase2);
table.addCell(” “);
cell.setFixedHeight(1);
table.addCell(phrase3);
table.addCell(phrase4);
table.addCell(phrase5);
table.addCell(phrase6);
table.addCell(phrase7);
table.addCell(phrase8);
table.addCell(phrase9);
table.addCell(phrase10);

cell = PdfPCell.init(Paragraph.init(”", arial));
cell.setBorderWidth( 0.0 );
table.addCell(cell);
table.setWidthPercentage(50);

document.add(table);

document.newPage();
png1 = Image.getInstance(”#expandpath(”businesscardside2.tif”)#”);
png1.setDpi(300,300);

png1.setAlignment(Image.UNDERLYING);
png1.setAbsolutePosition(0, 0);
png1.scaleToFit(240, 156);
document.add(png1);

// close the document (PDF Writer is listening and will automatically
// create the PDF for us
document.close();


Posted by
Jeffrey Roberson
7 November 2005 @ 7am

For Coldfusion MX 7 I had to recompile iText. I couldn’t get it to work any other way. The version of iText they use in MX7 throws all sorts of errors.

Anyway I blogged about it.

http://blog.internetdatabases.com/?p=7


Posted by
ruban
8 November 2005 @ 9am

how to get setDpi() in reports after each row


Posted by
dipu
22 March 2006 @ 4am

i also get the

Object Instantiation Exception.
Class not found: com.javaimage.ImageObj

i have all the files in the root.should i changed the folder structure…
pls make a reply


Posted by
Zack
25 March 2006 @ 10pm

You should be able to run both the CFMX 7 cfdocument tag as well as the iText.jar by placing the itext-1.4.jar in a “higher” lib folder (e.g. C:\JRun4\jre\lib). In fact, I’m running Fop, cfdocument, and now iText all on the same CFMX 7 server.


Posted by
alan
8 May 2006 @ 10pm

A PDF file cannot contain a virus, The layout of a PDF file is certified, A PDF file is compressed (low size) et can be opened on all computers (PC, MAC, Linux…) with the free and well-known Adobe Reader, No one can modify the content of your PDF files.

http://www.yaodownload.com/utilites/file-compression/easy-pdf-converter/


Posted by
mark Kruger
11 May 2006 @ 12pm

Aaron, great post. Do you have an example of READING a pdf using the PdfReader? I’m trying something simple like:

createObject(”java”,”com.lowagie.text.pdf.PdfReader”).init(”mypdf.pdf”);

But I’m having no success


Posted by
mark Kruger
11 May 2006 @ 12pm

never mind… I just needed a full path apparently.


Posted by
Emmanuel Ruiz
20 February 2007 @ 10am

Has anyone experienced issues with generating a pdf file with itext that is bigger than exactly 14 megs. It seems to work fine until it reaches that 14 meg threshold.

Thanks


Leave a Comment

Cool URIs don’t change… Scripting in ASP with Java