Rip's Domain

ColdFusion 8: Data corruption when writing out binary data.

Posted in ColdFusion by rip747 on March 31, 2009

Just a quick not to myself and others out there. Just today I had one of my users call me and complain that when they were trying to view a PDF from one of our applications Adobe Reader was complaining that the file was corrupt.

Now this application has been in production for 7 years and we never had a problem with it, why would all of a sudden would it start corrupting files? The application using the database to store the files (don’t ask, I didn’t write the original app) so in CF I have to write the file to disk using <cffile action=”write”> and then flush the file to the browser.

After a little detective work, it dawned on me. About 3 months ago, I finally updated the server to CF8 from CF7. The migration went really smooth and everything seemed to work fine. Something told me to get into CFAdmin and see if the blob support setting for the DSN was on. Well come to find out it wasn’t. After enabling it everything is working fine again.

So check any advanced settings that you might have had enabled on your DSNs if your did an upgrade and make sure that they are still enabled.

Advertisements

Add an image dynamically to a PDF with CF and iText

Posted in ColdFusion by rip747 on March 26, 2009

For the last couple of weeks I’ve been in a runt because I have really been doing anything challenging in CF for quite sometime. So my partner called me up and wanted to revamp the way our software generates certificates for our clients. Right now we have separate certificates for each client and the only thing that’s really different about them is the logo that we place on them and some data being inserted into some form fields. What we wanted to do was find someway to add the logos dynamically to a master PDF and use form fields to enter in the copy that needed to be inserted as well.

Now CF8 has some really nice tags for doing the form stuff, but adding the logo dynamically was not supported. I’ve used iText before back in the CF5 and CF6 days to combine and generate PDFs, but I never added an image dynamically to an existing PDF before.

This is just the challenge that I’ve been waiting for!

Suffice to say that after scouring the internet and reading a slew of Java and ColdFusion blogs, I was finally able to piece together some ideas to get this things working. Below is the commented code that I used to achieve success.

<cfscript>
// full path to PDF you want to add image to
readPDF = expandpath(“your.pdf”);
// full path to the PDF we will output. Using creatUUID() to create
// a unique file name so we can delete it afterwards
writePDF = expandpath(“#createUUID()#.pdf”);
// full path to the image you want to add
yourimage = expandpath(“dynamic_image.jpg”);

// JAVA STUFF!!!
// output buffer to write PDF
fileIO = createObject(“java”,”java.io.FileOutputStream”).init(writePDF);
// reader to read our PDF
reader = createObject(“java”,”com.lowagie.text.pdf.PdfReader”).init(readPDF);
// stamper so we can modify our existing PDF
stamper = createObject(“java”,”com.lowagie.text.pdf.PdfStamper”).init(reader, fileIO);
// get the content of our existing PDF
content = stamper.getOverContent(reader.getNumberOfPages());
// create an image object so we can add our dynamic image to our PDF
image = createobject(“java”, “com.lowagie.text.Image”);
// get the form fields
pdfForm = stamper.getAcroFields();
// setting a value to our form field
pdfForm.setField(“our_field”, “whatever you want to put here”);
// initalize our image
img = image.getInstance(yourimage);
// centering our image top center of our existing PDF with a little margin from the top
x = (reader.getPageSize(1).width() – img.scaledWidth()) – 50;
y = (reader.getPageSize(1).height() – img.scaledHeight()) / 2 ;
// now we assign the position to our image
img.setAbsolutePosition(javacast(“float”, y),javacast(“float”, x));
// add our image to the existing PDF
content.addImage(img);
// flattern our form so our values show
stamper.setFormFlattening(true);
// close the stamper and output our new PDF
stamper.close();
// close the reader
reader.close();
</cfscript>
<!— write out new PDF to the browser —>
<cfcontent type=”application/pdf” file = “#writePDF#” deleteFile = “yes”>

There you have it. The code above will insert a dynamic image into an existing PDF and fill in the form information as well.

One thing to note: In the finalized component I created to handle this, I was looping over a structure to populate the PDF form fields. Having spent about a half an hour wondering why my fields weren’t populating, it finally dawned on me: JAVA IS CASE SENSITIVE! How could I have forgotten. Anyways, in CF all the keys of a structure get converted to uppercase and the form fields in my PDF were lower case. After using wrapping the key references in lcase(), everything was working.

Friday! Jules pimping Linux

Posted in Fridays by rip747 on March 20, 2009

It’s been soooooo long since I’ve done a Friday! post. This Friday I bring Jules (from Pulp Fiction) pimping Linux curiosity of Slashdot.org.

Jules: [Jules shoots the man on the couch, who turns out to be Steve Jobs, turns to talk to Bill Gates] I’m sorry, did I break your concentration? I didn’t mean to do that. Please, continue, you were saying something about best intentions. What’s the matter? Oh, you were finished! Well, allow me to retort. What does Linus Torvalds look like?
Bill: What?
Jules: What OS do you run?
Bill: What? What? Wh – ?
Jules: “What” ain’t no OS I’ve ever heard of. They have a usable command line in What?
Bill: What?
Jules: Usable command line, mother fucker, do you have one?
Bill: Yes! Yes!
Jules: Then you know what I’m sayin’!
Bill: Yes!
Jules: Describe what Linus Torvalds looks like!
Bill: What?
Jules: Say ‘what’ again. Say ‘what’ again, I dare you, I double dare you motherfucker, say what one more Goddamn time!
[end scene, fade out with Linux, Operating System of Bad Mother Fuckers everywhere]

My new doormat

Posted in Uncategorized by rip747 on March 11, 2009

home-doormat

Any ColdFusion jobs in the South Florida area?

Posted in Uncategorized by rip747 on March 3, 2009

Some of my friends are hurting for some work down here in sunny (and freezing right now) South Florida. I’m asking if anyone in the community knows of some job opening to please pass them along this way. Anything in the following areas would do, either permanent or contract is fine:

ColdFusion
Flash
Photoshop
Illustrator
Site Layouts
Web Design

I’m not looking for any positions myself, just trying to help out my buddies as best I can. Please leave the information in the comments below so others can benefit as well.