Tuesday, January 31, 2012

RTF to PDF conversion - a legacy facelift

My latest bit of programmatic prestidigitation involved creating a sort of "modernizing" layer for a legacy report delivering system. Here's the scenario we had in place:

Numerous COBOL programs on a Unix server would produce text reports for clients in rich text (*.RTF) format. At the end of a business cycle (week, semi-period, or month), a number of these reports would be emailed to various client contacts by another COBOL program. This would result in thousands of emails going out, because the legacy process could only send one file to one address per email. This usually resulted in an end-of-period process that took several hours of churning-out emails before it was finished. On top of that, the attachments were UUEncoded, so several clients would constantly have trouble opening the reports.

We had wanted to replace or update this process for a long time, but didn't quite know where to start or what would be feasible. That's when we got the bright idea to leverage the mail-sending capabilities of SQL server, our web portal's list of email contacts, and a little bit of CLR "elven magic" to convert all of the old RTF files into more modern and portable Adobe Acrobat (*.PDF) files!

I won't bog things down with tons of lines of source code, partially because it might be an eyesore, but also because it surprisingly didn't take hundreds of lines of code. Thanks to a couple of free and open source libraries, we were able to easily take an RTF source and convert it to plain text and then dump that into a PDF.

You can see some great examples of the Itenso RTF Parsing library over at Codeplex (pay particular attention to the "Text converter" section). For the PDF portion of the solution, I decided to leverage iTextSharp - a .NET version of the Java iText library. We also had to made a small addition to our existing CDOsys-based mailing procedure on SQL, so that it could handle sending attachments. This thread over at SqlTeam.com was particularly helpful for me.

With all of those pieces in place, I merely had to tie it all together with a tiny VB.Net class library that I compiled and imported as a CLR assembly into our SQL Server instance. I was then able to create a stored procedure based upon that library that would take a path (local or UNC) to an RTF file and an output folder name as its parameters, and it would convert the RTF over to a PDF file. From there, I had everything in place we needed to create a simple job that would check for files that needed to be sent, convert them to PDF, add them to a single email, and deliver that single multi-attachment email to any number of recipients in one fell swoop.

The new process not only runs quicker and sends out less emails overall, but it also sends attachments that are smaller and able to be viewed on many more systems and platforms than the old UUEncoded RTF's were. Our first major trial of this new piece is coming tomorrow when we have an end-of-month and end-of-semi cycle, so here's hoping all goes well! I may post code details in a later blog if anyone's really interested in seeing how the nuts and bolts of this piece work (or if I get wild notion and just decide to post it for posting's sake).

And remember, just because it's legacy doesn't mean it has suck forever ;)

Monday, January 30, 2012

Response.Write ("Hello World!")

Just like the ubiquitous "Hello World" coding example, this post is my simple way of saying "Greetings!" and welcoming you in the spirit of my little blog's intent. This is probably the 4th or 5th blog I've ever started in my lifetime, and this one makes it over 3 posts, I think it will break my personal record. As such, I wanted to keep the focus of this one concise, yet hopefully useful for anyone that may happen by my corner of the blogging world.

"Musing in code" is where I intend to share nuggets, ideas, and "Aha!" moments I have during the course of my work as a software developer. Expect to see a heaping helping of content geared to web application solutions in VB.Net, T-SQL, and Javascript as well as some occasional links to other sites and articles that will hopefully be as much of a boon to you as they have been to me. Again, thanks for visiting, leave a comment if you feel inclined, and enjoy!