Generate PDF's from Flex/AIR – Code Sample using AlivePDF ActionScript 3.0 Library

January 26, 2010By 22 Comments

Awhile back I posted about an API called AlivePDF written by Thibault Imbert. I created a sample with it for Tour de Flex today to show how it can be used. The sample shows a use case illustrating how to add an image, a static text field, multi-line text and a text note to your PDF. AlivePDF is super easy to use and works great. There are different ways to save the PDF depending on the target environment. The sample contains code to save a PDF from a Flex application assuming a Flash Player minimum version of 10. If assuming a target Flash Player minimum less than version 10, you will need to call a server-side script to generate the PDF content. A sample of what this would look like is included in the sample code comments, as well as below. The Flash Player 10 release included a FileReference object that can now be used to avoid that server-side script and is the suggested use. The code to be used for saving a PDF from an AIR application is also included in the Tour de Flex sample and below.

Here are the code snippets for the three save options:

// HERE IS HOW TO SAVE FROM FLASH PLAYER 10 OR ABOVE
var bytes:ByteArray = claimPDF.save(Method.LOCAL);
var f:FileReference = new FileReference();
f.save(bytes,"tourdeflex-pdf-sample.pdf");
// HERE IS HOW TO SAVE FROM A FLEX APP RUNNING FLASH PLAYER < 10
 claimPDF.save( ethod.REMOTE, "http://alivepdf.bytearray.org/wp-content/demos/create.php", "tourdeflex-pdf-sample.pdf");
// HERE IS HOW TO SAVE FROM AN AIR APP
var fs:FileStream = new FileStream();
var file: File = File.desktopDirectory.resolvePath("tourdeflex-pdf-sample.pdf");
fs.open(file, FileMode.WRITE);
var pdfBytes:ByteArray = claimPDF.savePDF(Method.LOCAL);
fs.writeBytes(pdfBytes);
fs.close();

More information on server-side scripts for those running < version 10 on Flash Player can be found in the links below:

More AlivePDF sample code and general information can be found at these links:

Filed in: Flex/AIR

About the Author ()

Comments (22)

Trackback URL | Comments RSS Feed

  1. Ben says:

    I’ve used this library in a couple of projects. Besides a few issues with font embedding it works pretty well for the most part.

    However I really wish Adobe would include some native PDF wrappers to Actionscript. That way we could have really fine control over the pdf’s like we do when using extendscript in the Adobe Creative suite.

    I’d love to able to control overprint, opi, font embedding, image compression settings, pdf version, security settings etc. all from actionscript.

    Considering Adobe own both the flash player and pdf/postscript they are well within their power do this and I dare say it would give their development platform somewhat of a competitive advantage.

    cheers
    Ben

  2. Can we create a flex application (Not AIR app) which will take a pdf from server side and display in flex application for user interaction?

    If yes,
    1)Can we modify the pdf and store on the server side.

  3. JTtheGeek says:

    Hey I love your Blog and articles, keep writing! Just curious regarding Alive PDF vs. PurePDF (http://code.google.com/p/purepdf/) being maintained by Alessandro Crugnola (http://wwwsephiroth.it) I haven’t gone over everything in full detail yet, but am about to start implementing PDF integration into our app and wondering which way to go. AlivePDF looks good, but it appears that PurePDF may be a much fuller implementation. Either way its great for us that we are getting multiple options for working with PDF’s. Thanks again!.

    • devgirl says:

      Thanks for the nice remarks about the blog JT, I appreciate it :)!!

      I had not seen this library yet, thanks for the heads-up! I will check it out for sure and see how it compares. It would be another good one to add to Tour de Flex as well. It really is great to see all of these options available now. Let us know your experience with it if you decide to use it, I’m sure many of us could benefit from it!!

      Thanks much and come back again soon!!
      Holly

      • Darryl says:

        Have you got any further with PurePDF? I am looking to integrate it into a project in place of AlivePDF as it seems to handle transparency. A Tour de Flex example would be excellent!

    • JasonMichael says:

      Thank you SOOOOOO much for this response. I completely forgot about purePDF. Everywhere I go, everyeon’s talking about AlivePDF, but I get a feeling it still has a long way to go.

      PurePDF is a port from iText – a java PDF class which is popular and has been around for years. PurePDF looks GREAT, and if it is a very good port, I am sure this will be a pefect solution for someone who needs a fast, reliable PDF generation solution.

  4. Tahir Alvi says:

    Thanks Jani

    I really need this one

  5. Eric says:

    Hi Holly,

    Is Air 2 going to have the capability to print a pdf? If so do you have an example of how to do this?

  6. ren says:

    I am trying to add page Numbers or text in the PDF for all the pages that are generated. I dont know if there is any other method , but i am writing a listner when a new page is created. But this is not working.
    Can someone tell me or send me correct code

    pdf.addEventListener(PageEvent.ADDED,onPageAdded,false,0,true);

    public function onPageAdded():void{
    pdf.addText(“In Header”,10,10);
    }

    My requirement is to put headers and page numbers in all the pdf pages created.Can anyone please send me the correct code.even this code is not working properly

    override protected function header():void {
    pdf.setFont(newFont, 12);
    pdf.textStyle( new RGBColor (0×000000) );
    pdf.addText(“In Header”, 30,15);

    • Chris says:

      I achieved this by looping through each page once my PDF was created, and adding the page numbers in.

      var i:int;

      for (i = 1; i < (myTotalPageCoutn – 1); i++)
      {
      myString = "page" + String(i) + " of " + String(myTotalPageCount);
      myPDF.gotoPage(i);
      myPDF.addText(mystring, 10, 290);
      }

  7. Adam says:

    Alternatively you can you another free PDF library called FxPDF
    Project home web site is : fxpdf.com
    code is published under Apache 2.0 licence in google code code.google.com/p/fxpdf

    Usage example is under the link
    http://www.fxpdf.com/joomla/FxPDFExamples/FxPDFExamples.html

  8. Chris says:

    I have been using AlivePDF successfully with Flash Player 10+ to generate PDFs locally – however, is there a way to check when the PDF has been saved so that I can provide feedback to the user?

  9. CyberDude says:

    one can also print directly to BMP / GIF stuff…… and PDF (OSX -> directly WINDOWS -> Win2PDF-File-Printer)

    FlexPrintJob

    http://livedocs.adobe.com/flex/3/html/help.html?content=printing_3.html

  10. Tahir Alvi says:

    Hi,

    can we preview the pdf before saving it? if yes then how?

    Thanks

  11. Wally Kolcz says:

    Any way to send the pdf to FlexPrintJob so you can directly print the document instead of generating a downloadable PDF?

  12. Gilbert says:

    Holly,

    The link to the sample you created for Tour de flex is broken.

  13. Elena Tsvetkova says:

    How I can send the created PDF to the URL to allow users to preview it and then, at that point, print or save it?

Leave a Reply