AlivePDF Flex/AIR Example

It took me a couple minutes to figure out how to write a PDF to disk using AlivePDF, hopefully this short example will save you those few minutes.

public class ProjectPDFExporter
{
private var filename:String = “test.pdf”;
private var pdf:PDF;

public function exportPdf(filename:String) : void
{
this.filename = filename;

pdf = new PDF();

pdf.addEventListener(Event.COMPLETE, onComplete);

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



pdf.addPage();
pdf.setFont( FontFamily.ARIAL );
pdf.addText(”My Teax”,1,10);
pdf.setFont( FontFamily.ARIAL , “”, 32);
pdf.addText(”Some more text”,10,30);

pdf.finish();
}

protected function onComplete(event:Event)
{
var f:FileStream = new FileStream();
var file:File =
File.applicationStorageDirectory.resolve( filename );

f.open( file, FileMode.WRITE);
var bytes:ByteArray = pdf.getPDF()
f.writeBytes(bytes);
f.close();
}
}

Also, it looks like the first version of AlivePDF.swc is missing at least one class (FontFamily), so you’re probably safer using the source instead of the swc for now.

The more I look into it, the more I’m impressed by this library. I’m really surprised Adobe had never made something similar before.

10 Responses to “AlivePDF Flex/AIR Example”


  1. 1 Anonymous

    Hi Marc,
    Ur AlivePDF code was quite helpful but i’m stuck at a place where i wanna display gradient effect to image in PDF.
    Can u help me out….

    Thanks,
    Nams!!
    (nams_30jan@yahoo.com)

  2. 2 jms-123

    Hi Marc,

    My requirement is to generate a PDF doc using the data present in the datagrid.

    I tried incorporating AlivePDF, unable to proceed b’coz mine is a pure FLEX applciation and not AIR [Cant use FileStream etc].

    Is AlivePDF compatible with Flex application’s?

    If yes, how to do away with FileStream class’s.

  3. 3 Marc

    To do that you need to create the PDF and send it to the server. Then the server can “bounce” it back to the client and let the browser save it somewhere. I’ve never done that, but there’s examples and a server side script to do it in the AlivePDF documentation.

  4. 4 Kumbhi

    Hi jms-123,
    As Marc said, if you are using a Flex Web application you need to set the method to Method.REMOTE in the save method. Also, you will need to put the create.php (provided with AlivePDF) and point the URL to the create.php. When you call the save method, the Flex client will generate the PDF and then POST it to create.php. The create.php will send the PDF back to the client.

    This is done to overcome the sandbox restriction of the Flash player. Version 10.0 of the Flash player has some method built into it, which allows you to save PDF from flash client. Till then, it’s an up and down journey for the PDF.

  5. 5 HaoLiang

    Hi, Marc

    AlovePDF could be applied to flex project. Not used AIR Project.

  6. 6 HaoLiang

    Hi, Marc

    I hope you could help me! thanks!
    I want to embed pdf in flash by byteArray. Do you think that is feasible?
    if you thinks is ok, please guide for me!
    thanks very much.

  7. 7 Mohammad

    The flex 3 dose not have FileStream Class..
    Is my flex compiler is missing this class or the flex3 already dose not contain this class

  8. 8 teodora

    Hello,

    I would like to know if there are benchmarks concerning the pdf generation. In my project I have to generate a pdf from a table with 10000 lines and 10 columns. I am afraid that the pdf generation could freeze the client computer.

    thanks in advance

    Teo

  9. 9 Chad Upton

    Flex 3 SDK does have FileStream, but it can only be used if you’re deploying as an AIR app. If your project is setup to deploy as a web application then you won’t have access to it (to stay compliant with the Flash Player security model).

  10. 10 vamsi

    Flex 3.3 SDK has FileStream. U can use the latest version for pure flex based application.

Leave a Reply