{"id":577,"date":"2004-03-14T20:58:21","date_gmt":"2004-03-15T00:58:21","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=577"},"modified":"2004-03-14T20:58:21","modified_gmt":"2004-03-15T00:58:21","slug":"using-itext-pdf-coldfusion","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/03\/14\/using-itext-pdf-coldfusion\/","title":{"rendered":"Using iText PDF &amp; ColdFusion"},"content":{"rendered":"<p>Mike Steele sent me an email in reference to an <a href=\"http:\/\/www.sys-con.com\/coldfusion\/articleprint.cfm?id=629\">article I wrote<\/a> for the <a href=\"http:\/\/www.sys-con.com\/coldfusion\/\">ColdFusion Developer&#8217;s Journal<\/a> a year or so ago. In the email, he mentions that he is trying to use the <a href=\"http:\/\/www.lowagie.com\/iText\/\">iText Java-PDF library<\/a> with ColdFusion MX:<\/p>\n<blockquote><p>\n&#8230; The getInstance method is static and according to your July 2003 CFDJ article, you can&#8217;t instantiate an object in CF this way.\n<\/p><\/blockquote>\n<p>In the article I said this:<\/p>\n<blockquote><p>\n&#8230; 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.\n<\/p><\/blockquote>\n<p>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 &#8216;getInstance()&#8217;  on  the PDFWriter class. Given that news, you can take the quick example that the author of the iText library gives <a href=\"http:\/\/www.lowagie.com\/iText\/tutorial\/ch01.html\">here<\/a> to create a PDF in a snap using ColdFusion:<br \/>\n<code><br \/>\n&lt;cfscript&gt;<br \/>\n\/\/ create a 'Document' object<br \/>\ndocument = CreateObject(\"java\", \"com.lowagie.text.Document\");<br \/>\ndocument.init();<br \/>\n\/\/ get an outputstream for the PDF Writer<br \/>\nfileIO = CreateObject(\"java\", \"java.io.FileOutputStream\");<br \/>\n\/\/ call the constructor, pass the location where you want<br \/>\n\/\/ the pdf to be created<br \/>\nfileIO.init(\"C:\\myhost.com\\somedir\\test.pdf\");<br \/>\n\/\/ get a PDF Writer var<br \/>\nwriter = CreateObject(\"java\", \"com.lowagie.text.pdf.PdfWriter\");<br \/>\n\/\/ call the static 'getInstance' factory method<br \/>\nwriter.getInstance(document, fileIO);<br \/>\n\/\/ open the document<br \/>\ndocument.open();<br \/>\n\/\/ create a new paragraph<br \/>\nparagraph = CreateObject(\"java\", \"com.lowagie.text.Paragraph\");<br \/>\nparagraph.init(\"Hello World!\");<br \/>\n\/\/ add the paragraph<br \/>\ndocument.add(paragraph);<br \/>\n\/\/ close the document (PDF Writer is listening and will automatically<br \/>\n\/\/ create the PDF for us<br \/>\ndocument.close();<br \/>\n&lt;\/cfscript&gt;<br \/>\n<\/code><br \/>\nCopy that code into a cfml page and make sure you&#8217;ve downloaded the iText jar to the \/lib\/ directory of your ColdFusion server and you should be able to create PDF&#8217;s in a jiffy!<\/p>\n<p>Full source code available <a href=\"\/projects\/coldfusion_itext\/default.cfm\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mike Steele sent me an email in reference to an article I wrote for the ColdFusion Developer&#8217;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: &#8230; The getInstance method is static and according to your July 2003 CFDJ article, &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/03\/14\/using-itext-pdf-coldfusion\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using iText PDF &amp; ColdFusion<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/577"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=577"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}