Simplyprofound.com
Flash, Flex, AIR, ASP.NET, PHP, AJAX, whatever it takes...

Generate PDFs with Actionscript using AlivePDF

May 8, 2008 18:12 by dave

I recently had a Flash / AIR project that required the ability to render PDFs. Evidently, this hasn't been a hot issue in the past for Flash applications, however I get the feeling that AIR may change that. While a web application can utilize any of many server side solutions to render a PDF to the browser, it's not the same for AIR.

In looking for a good solution, I quickly had to reset my expectations to find ANY solution. The only option that fit the bill was AlivePDF, an open source Actionscript 3 port of a PHP library. AlivePDF is maintained by Thibault Imbert.

AlivePDF comes with a SWC that you can include in your Flex application, or an Actionscript package you can import to your AS code. Here's a brief, condensed example of how I used AlivePDF in my Flash AIR project.

import org.alivepdf.*;

// setup pdf
var myPDF : PDF = new PDF ( Orientation.PORTRAIT, Unit.MM, Size.LETTER );

myPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );

myPDF.addPage();

// add a background image
myPDF.addImage (myBackgroundMovieClip, 1 , null, null, false, ImageFormat.JPG, 100, 0, 0, 0, 0);

// add headline
myPDF.textStyle ( new RGBColor ( 41, 58, 140 ) );
myPDF.setFont( FontFamily.HELVETICA, Style.BOLD );
myPDF.setFontSize ( 18 );
myPDF.setXY( 10, 40 );
myPDF.addMultiCell ( 300, 1, "This is my PDF Headline" );

// add text message
myPDF.textStyle ( new RGBColor ( 0, 0, 0 ) );
myPDF.setFont( FontFamily.HELVETICA, Style.BOLD );
myPDF.setFontSize ( 14 );
myPDF.setXY( 10, 50 );
myPDF.addMultiCell ( 300, 4, "This is my text....lots of text..." );

// save PDF to the desktop
var f : FileStream = new FileStream();
var file : File = File.desktopDirectory.resolvePath("MyPDF.pdf");
f.open( file, FileMode.WRITE);
var bytes : ByteArray = myPDF.savePDF(Method.LOCAL);
f.writeBytes(bytes);
f.close();     

Notice I'm using the method addMultiCell to add text. This may seem odd, but what this allows me to do is add a block of text that wordwraps. If I use addText then I only get a single line. 

AlivePDF is definitely still young and I've run into a few oddities (read bugs). I logged a bug on the Google project site and within no time at all received a note saying the issue had been resolved and would be released in the next build. Nice!

The Google project site provides a few examples in addition to the samples provided in the download. And the documentation is fairly helpful.

Note: Thibault just released 0.1.4.1, a minor revision the codebase I've been using at 0.1.4.0.  Hopefully this is a sign that the project is picking up some momentum! 

If you need to generate PDFs from your Flash, Flex, AIR applications using only AS 3.0, then this is your best (only) solution. 


Tags: , ,
Categories: AIR | Flash
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Adobe AIR Publishing Annoyances in Flash CS3

May 8, 2008 18:00 by dave

I've run into some minor annoyances in publishing AIR applications from Flash CS3 that I really hope Adobe fixes with an update.

Annoyance #1: Not all files being published with the AIR file

hen you install the AIR update for Flash CS3 it adds 2 commands to the Commands (duh) menu: AIR - Application and Installer Settings, and AIR - Create AIR File. The first menu item has all the settings in addition to a publish button. The latter just pumps out the AIR file.

The problem I've found is that when I have added directories and files to the "Included files" area, they are only being included when I publish from the AIR - Application form and NOT from the AIR - Create AIR File command option.

Annoyance #2: Digital Signatures files are not application specific 

I have multiple AIR projects going and each has a different certificate to sign the application. Unfortunately, the certificate used for an application is not stored with the settings for that application. So, if I build application A with a development junk signature, and then immediate build application B with a Thawte digital signature, I must reconfigure that setting alone (and dig up my ridiculously secure password that I cannot remember). 

Annoyance #3: No option to publish AND create an AIR file all at once

I kept wondering why my AIR files weren't showing my most recent changes. Turns out, the commands mentioned above will only use the most recently published SWF to build the AIR file. Fine, I understand, whatever. But I'd at least like the option to tell Flash CS3 to build the SWF and then the AIR file all in one motion, or the option to use the latest build. 


Tags: ,
Categories: AIR
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Superscript Registered Trademark in Flash

May 2, 2008 03:06 by dave

Some of the most elementary things can be so completely frustrating in Flash. In a recent project I needed to superscript a registered trademark® in both static and dynamic textboxes.

It is well know that Flash only recognizes a limited list of markup for html, and <sup></sup> is not among them. Various CSS solutions also do not work because the necessary CSS is also not recognized.

The solution? A custom embedded font.

I tried a free font, CG Superscript, which worked find for regular text, but not ®. 

The one that worked for me was Superscript, which rt now goes for $9.90. Originally, I dismissed this solution thinkin it was the same thing as CG Superscript. It didn't list ® on the site.

The instructions are simple and right on the homepage. They also offer a subscript font, same cost.

Much thanks to the illustrious Mike Creighton for getting me to take a second look at that font. 


Tags: ,
Categories: Flash
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Read More Code

April 30, 2008 15:34 by dave

Not as cool as "Eat More Chick'n", and certainly not an advocation of Dickensian code (that'd be longwinded), this is my new code exercise mantra.

As any good code citizen who has dogged eared a copies of The Pragmattic Programmer and Practices of an Agile Developer knows, if you're not learning you're not growing.

Tip #8: "Invest regularly in your knowledge portfolio. Make learning a habit."
- The Pragmatic Programmer

And it's not just about learning syntax, but about patterns and practices that will continue to apply long after the flavor of the month becomes the punchline of a joke.

As a freelance developer, I primarily code alone. So, I love it when I have the opportunity to be exposed to someone elses code and learn how other people are tackling similar tasks and challenges. This is a practice that Scott Hanselman has been promoting in his blog as he examines and discusses code from various projects weekly.

Learning topics such as how to code Actionscript 3.0 or fundamentals of AJAX are certainly essentially. But the next iteration of those technologies may well wash away a good portion of that knowledge as it becomes obsolete. It's so crucial not to forsake strengthening knowledge of the overarching principles that make us good at what we do for years to come. Scott H and X struck this chord with me this week.

Books: We need more So What, Now What and What For? and less just What - Hanselman
Code Leader book - Paul Cauldwell

As I'm reading code, I'm looking to learn not only how something works, but why it's being done that way and to what end. I can't download enough code these days.

Such as:

http://brianchau.wordpress.com/2008/04/30/learning-actionscript-3-eseminar-tutorial-files/ - Actionscript 3 from super basic to simple MVC
http://alivepdf.bytearray.org/ - Generate PDFs from Actionscript 3
http://casaframework.org/ - agile framework for Actionscript 2
http://www.asual.com/swfaddress/ - deep linking for Flash
http://www.davidtucker.net/category/cairngorm/ - David Tucker's Cairngorm Tutorials (MVC for Flex)