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.
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.
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.
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…
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
thanks..
Iam using cfml for generating reports.can u help me in writing the resultset to pdf file.software is oracle9i and coldfusionmx.
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.
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.
Can iText convert HTML to PDF? I couldn’t find any info on this?
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
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.
Just put the Jar file in “CfusionMX\runtime\jre\lib” and also put an entry into ColdFusion Admin Classpath.
Will this work with ColdFusion 5?
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.
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?
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.
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
Anyone have any luck with refactoring iText for CFMX7?
Their cfdocument tag is terrible….
Check this out CFMX 7 users
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();
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
how to get setDpi() in reports after each row
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
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.
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/
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
never mind… I just needed a full path apparently.
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
Has anyone tried using iText to silently print pdfs? I have a number of PDFs that the user can select using checkboxes. Upon clicking a button I want to send those PDFs to the users default local printer without any further dialogue boxes. There is code out there that pertains to this, but I’ve not been able to get anything working.
Thanks
Please i’m just a newbee in java ,in my project i have one problem …
After the execution of my program ,this is located in a given location ,but my wish is to open it automatically …
PLZ how can i do it ?
thank a lot for your support .
Os :fedora 8,netbeans 6.5
Sorry i wanted to say that my pdf file is located in a given location ,but i dont want to browser there and open it manually ….
I investigated using iText with ColdFusion and have had great success serving PDFs from my CFM that I generated myself with iText. Not only does it work, but it’s ridiculously easy. I’m new to ColdFusion and Java and was pleasantly surprised to see how easily and well the two played together.
However, in order to create headers and footers on each page, I found I have to extend the PdfPageEventHelper class and add an override to the method OnPageEnd( ), and attach that helper to my instance of the PDFWriter. This is fine, except it’s not obvious how to do this in CF. Putting a class declaration inside the where I do everything else made CF choke (unsurprisingly).
This is roughly what I’m trying to do in Java:
class HeaderFooterPageEventHandler extends pdfPageEventHelper {
public void onEndPage( PdfWriter writer, Document document ) {
header = Phrase.Init( “This is the header of the document.” );
footer = Phrase.Init( “This is the footer of the document.” );
center = ( document.right( ) – document.left( ) ) / 2;
ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, header,
center + document.leftMargin( ), document.top( ) + 10, 0 );
ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, footer,
center + document.leftMargin( ), document.bottom( ) – 10, 0 );
}
}
I’m looking into using to implement this class so I can use it in my CFM. Is this the right way to do it? It is even possible? I’d hate to have to make my own jar just to do this simple thing, but I imagine that wouldn’t be too bad.
I’m still a little hazy about the relationship between ColdFusion components and the Java objects I know they are underneath, and how interchangeable they are with non-CF Java objects. The idea of extending CF with Java is very exciting and I imagine will be very handy for me in the future.
Many thanks,
Rick
I want the code to read the pdf file and store it into text document( .txt file) using iText … Can you help me for this…?
Its urgent…
@Iain B – it’s been a long time since your post, but I’ve been having the same problem. After some searching on the interwebs, the best answer I have been able to find is Paolo’s statement “That method was always public. Whatever is happening it’s not iTexts fault.”
This appears to be the problem for me. If I define writer as
oWriter=CreateObject(“java”,”com.lowagie.text.pdf.PdfWriter”);
oWriter.getInstance(oDocument,oOutput);
then I get the error.
However, if I define oWriter as
oWriter=CreateObject(“java”,”com.lowagie.text.pdf.PdfWriter”).getInstance(oDocument,oOutput);
Then I don’t get the error. Both approaches work for everything else (and I have seen the first approach used a lot in Coldfusion examples online), but I think in the first case you are getting a reference to the static class, and in the second case you are getting a reference to a PdfWriter object.