Custom Printing with Flex, Part 2: Generating PDFs with AlivePDF

Posted in ActionScript, Coldfusion, Flex, How-To on March 1st, 2009 by Kalen Gibbons

In the first article of this series, Overriding Flash’s Built-In Print Option, I discussed how to prevent users from printing directly from Flash, by replacing the default context menu. I demonstrated how poorly Flash handles printing directly, and now we will start discussing what alternatives are available to developers.

In this article I will demonstrate how to print and save any part of your application by utilizing the AlivePDF library. AlivePDF is an ActionScript 3 open-source PDF library that easily creates PDF documents from your Flex application. The library is pretty robust and offers much more functionality than I will be able to cover here. In this article I want to demonstrate how simple it is to dynamically generate PDF documents using ActionScript and AlivePDF.

To give you a better understanding of what can be done with AlivePDF, let’s revisit one of the printing examples that was used in part 1 of the series. The two images below are printouts of the The Flex Store – one printed with Flash’s default print option and the other with AlivePDF.

Flex Store printed using Flash’s default print option
Flex Store Printed from Flash
Flex Store printed using AlivePDF
Flex Store PDF created with AlivePDF

In addition, here is a sample application for you to play with some of AlivePDF’s options. You can view the source here.

Get Adobe Flash player

So now that you’ve had a taste of what AlivePDF can do, lets look at how it works:

Adding AlivePDF to your project

First, download the library from www.AlivePDF.org, and then add it to your project in one of two ways. The easiest way is to copy the AlivePDF.swc into your project’s lib folder. Flex Builder should automatically detect the swc and add it to the compile settings. The second approach is to add it manually in Flex Builder, by clicking Project >> Properties >> Flex Build Path >> Library Path >> Add SWC… and selecting the swc from the file system.

Displaying PDFs in the Browser

After the swc has been added to your project you can begin working with the AlivePDF library. The sample code below could be used to generate a PDF document from any UI component in your application. We simply turn it into an image (think of it as a screenshot) and publish it to a PDF for the user to save or print.

The code is fairly simple, we start by creating a new instance of the PDF class and adding a page to it. Then we add our UIComponent to the document as an image. Finally, we tell AlivePDF how we want to generate the PDF with the save() method. In this example, the third argument of the method, Download.INLINE, specifies that we want the PDF to be displayed in the browser instead of prompting the user to save (that will be covered in the next section).

import mx.core.UIComponent;

 

import org.alivepdf.display.Display;
import org.alivepdf.images.ResizeMode;
import org.alivepdf.layout.Layout;
import org.alivepdf.layout.Orientation;
import org.alivepdf.layout.Size;
import org.alivepdf.layout.Unit;
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Download;

import org.alivepdf.saving.Method;

 

private function doPrint(whatToPrint:UIComponent):void{

var printPDF:PDF = new PDF( Orientation.LANDSCAPE, Unit.MM, Size.A4 );
printPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );
printPDF.addPage();
printPDF.addImage( whatToPrint, 0, 0, 0, 0, ‘PNG’, 100, 1, ResizeMode.FIT_TO_PAGE );
printPDF.save( Method.REMOTE, "/includes/pdfCreator.cfm", Download.INLINE, "test.pdf" );

}

Prompting Users to Save

Now that works great and all… but what if you want to prompt the user to save or open in Adobe Reader? Fortunately, AlivePDF makes that easy; all you have have to do is change Download.INLINE to Download.ATTACHMENT.

printPDF.save( Method.REMOTE, "/includes/pdfCreator.cfm", Download.ATTACHMENT, "test.pdf" );

Using AlivePDF with ColdFusion

A PHP script is provided with the download of the AlivePDF library, but if you don’t like PHP you can use any other server-side language that can generate PDF content. For those of you who are ColdFusion fans, like myself, here is a basic example of a ColdFusion script that can be used.

<!––– establish parameters –––>
<cfparam name="URL.method" default="" />
<cfparam name=
"URL.name" default="" />

 

<––– get the content from the http data –––>
<cfset httpContent = GetHttpRequestData()>

<!––– make sure content was passed in –––>
<cfif len(httpContent.content) gt 0>

<!––– write the content to local pdf –––>
<cfheader name="Content-Disposition" value="#URL.method#; filename=#URL.name#" />
<cfcontent type="application/pdf" variable="#httpContent.content#">

<cfelse>

<!––– redirect to home page –––>
<cflocation url="/" />

</cfif>

AlivePDF has a wide range of capabilities and we’ve barely scratched the surface here. I encourage you to take a good look at the library and utilize it to its full potential.

In the next article in this serious we will discuss how to print data from Flex. See you soon!


Tags: , ,