2

I need to use iText with ColdFusion (CF) because CFDocument won't do everything I need it to, however I would like to return the result into a CF Variable instead of saving it to a file. It seems that every example out there saves the results to a file.

I'm using the following example code to actually generate a pdf but as I said I need it in a variable (preferably without being a file first) because that variable has to be passed on to code another group has written (and I have no control over).

<cfset var document=createObject("java", "com.lowagie.text.Document") />
<cfset var PageSize = createObject("java","com.lowagie.text.Rectangle") />
<cfset var fileIO = createObject("java","java.io.FileOutputStream") />
<cfset var writer = createObject("java","com.lowagie.text.pdf.PdfWriter") />
<cfset var paragraph = createObject("java", "com.lowagie.text.Paragraph") />
<cfset var FontFactory = createObject("java","com.lowagie.text.FontFactory") />
<cfset var Font = createObject("java", "com.lowagie.text.Font") />
<cfset var Courier = Font.init(Font.COURIER, 8.0) />
<cfset var CourierB = Font.init(Font.COURIER_BOLD, 8.0) />

<cfset PageSize.init(612, 792) />
<cfset document.init(PageSize) />
<cfset fileIO.init("C:\test.pdf") />
<cfset writer.getInstance(document, fileIO) />
<cfset document.open() />
<cfset paragraph.init("Hello world.", Courier) />
<cfset document.add(paragraph) />
<cfset document.close() />

I'm not all that great with Java, just basic knowledge, so this could actually be something simple that I'm not understanding.

Thanks

sdurette
  • 61
  • 4
  • I'm not too sure what it is you need iText to do... but did you consider using [the combine_pdf gem](https://github.com/boazsegev/combine_pdf) or the [Prawn](http://prawnpdf.org/api-docs/2.0/) library? both are pure Ruby and would allow you to output the compiled PDF as a String. – Myst May 15 '15 at 19:51
  • Not using Ruby. I'm in a ColdFusion environment (that can access Java directly) and iText comes with ColdFusion. At this time I don't have access to use other languages on the server. – sdurette May 15 '15 at 20:11
  • Exactly what kind of variable? What does the other code ultimately do with it? – Leigh May 15 '15 at 20:53
  • ColdFusion variables aren't typed. The other code does processing on the PDF in the variable. Whatever they want to do with it. Sometimes they add a watermark, sometimes they change the properties. The thing is that it must be possible to save into a variable because the cfdocument tag can do it but isn't really accurate enough for some of the things I have to place in the document. – sdurette May 16 '15 at 03:31
  • Technically, CF is loosely typed. You did not mention whether the "other code" is CF, java or some other language. Hence the question about what kind of variable. In CF, when you read a pdf into a variable it uses an object type which is a thin wrapper around the binary of a PDF. Assuming this "other code" is also CF, it sounds like it is expecting that type of object ie something that can be used with CF's pdf functions/tags.(cont'd) – Leigh May 16 '15 at 13:38
  • If so, either a) write the pdf to a binary stream instead of a file, then [load the binary into a CF pdf variable](http://stackoverflow.com/questions/10690548/coldfusion-cfpdf-reading-a-binary-database-column) (simpler in CF9+) OR b) save it to a file, read the file into a pdf variable, the delete the file. – Leigh May 16 '15 at 13:38
  • The variable gets sent on to a ColdFusion component. I'll have to look at binary stream, I think that was the part I was missing. – sdurette May 16 '15 at 14:09
  • Do not have time to write up a full answer now, but use a `ByteArrayOutputStream` instead of `FileOutputStream`. When finished, grab the binary from it using `ByteArrayOutputStream.toByteArray()`. – Leigh May 16 '15 at 14:31
  • They use of ByteArrayOutputStream was the answer. Since these are just comments, I don't know how to mark it as the answer so I will post my corrected code and mark it as the answer unless you want to do it for the points. – sdurette May 18 '15 at 11:08
  • @sdurette - That is fine, as I did not have time to write it up. Though the courtesy of asking is appreciated :) Thanks for posting the final code to help the next guy. – Leigh May 18 '15 at 15:32

1 Answers1

1

Leigh's comment about using ByteArrayOutputStream was the correct answer. Here is the updated code that works placing the generated PDF into a ColdFusion variable:

<cfset var document=createObject("java", "com.lowagie.text.Document") />
<cfset var PageSize=createObject("java","com.lowagie.text.Rectangle") />
<cfset var fileIO=createObject("java","java.io.ByteArrayOutputStream") />
<cfset var writer=createObject("java","com.lowagie.text.pdf.PdfWriter") />
<cfset var paragraph=createObject("java", "com.lowagie.text.Paragraph") />
<cfset var FontFactory=createObject("java","com.lowagie.text.FontFactory") />
<cfset var Font = createObject("java", "com.lowagie.text.Font") />
<cfset var Courier = Font.init(Font.COURIER, 8.0) />

<cfset PageSize.init(612, 792) />
<cfset document.init(PageSize) />
<cfset writer.getInstance(document, fileIO) />
<cfset document.open() />
<cfset paragraph.init("Hello world.", Courier) />
<cfset document.add(paragraph) />
<cfset document.close() />

<cfset var returnVar = fileIO.toByteArray() />
sdurette
  • 61
  • 4