Create PDF in runtime with Actionscript 3 (AlivePDF, Zinc or AIR, Flex or Flash)

This morning I’ve a new target, create PDF in runtime with Actionscript 3.
Very cool project to accomplished this mission is AlivePDF, an opensource AS3 library that you can download from Google Code.
AlivePDF allow you to generate PDF in runtime with Actionscript 3 and you can add pages, draw in each pages or add images, it’s very powerful library.

In this sample I use Actionscript 3 (with FDT) and Multidmedia Zinc 3, but you can use Flex or Flash and AIR to make this sample.
So first of all I create a simple class that allow you to create a PDF file with multiple pages and to add content in each pages.
This is the code:

package org.mart3.pdfGeneration {
    import flash.events.Event;    
    import flash.utils.ByteArray;    
    
    import org.alivepdf.images.ImageFormat;    
    import org.alivepdf.saving.Method;    
    
    import flash.display.Loader;    
    import flash.events.IEventDispatcher;    
    import flash.events.EventDispatcher;
    
    import org.alivepdf.pdf.PDF;
    import org.alivepdf.layout.Orientation;
    import org.alivepdf.layout.Size;
    import org.alivepdf.layout.Unit;
    import org.alivepdf.display.Display;
    
    /**
     * @author lm
     */
    public class CreatePDF extends EventDispatcher {
        
        private var pdf:PDF;
        public var pdfBA:ByteArray;
        
        public function CreatePDF(target : IEventDispatcher = null) {
            super(target);
            
            pdf = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.A4);
            pdf.setDisplayMode(Display.FULL_PAGE);
        }
        
        public function set totalPages(num:int):void{
                
            for(var i:int = 0; i < num; i++){
            
                pdf.addPage();    
                
            }
    
        }
         public function setData(_l : Loader, _numPage:int) : void {
            
            pdf.gotoPage(_numPage);
            pdf.addImage(_l, 15, 15, 0, 0, ImageFormat.JPG, 100);
        }
        
        public function savePDF():void{
            pdfBA = new ByteArray();
            pdfBA = pdf.save(Method.LOCAL);
            
            var evt : Event = new Event("baReadyEvent");
            dispatchEvent(evt);
        }
    }
}

Obviously if you want, you can create a custom event that pass to the document class the ByteArray but this is a quick sample to show how you can create PDF in runtime!

One of the amazing things that you should do with AlivePDF, it’s that you can decide to save PDF locally or on web! Read documentation because it’s very interesting what you can do with this library!

Ok now, go to document class where we use MDM swc that you can find when install Zinc on your computer (you can find 2 differents SWC, one for Flash and the other one for Flex. Remeber also that Flash SWC works with Flash CS4 also, not only with Flash CS3!).
In this class we do those simple steps:

  • create a PDF object using CreatePDF object
  • set our PDF document
  • pass an external image loaded with Loader object
  • save PDF bytearray with Zinc FileSystem class
package org.mart3.pdfGeneration {

    import flash.display.MovieClip;    
    import flash.net.URLRequest;    
    import flash.display.Loader;    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    import mdm.*;

    import org.mart3.pdfGeneration.*;

    /**
     * @author lm
     */
    public class Main extends Sprite {
    
        private var pdfObj:CreatePDF;
        private var l : Loader;
    
        public function Main() {
            
            mdm.Application.init(this);            
            
            pdfObj = new CreatePDF();
            pdfObj.totalPages = 2;
            pdfObj.addEventListener("baReadyEvent", saveLocalPDF);
            
            l =  new Loader();
            l.name = "myImg";
            
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, showImage);
            l.load(new URLRequest(mdm.Application.path+"assets/bg.jpg"));
        }
        
        private function saveLocalPDF(e:Event) : void {
        
            mdm.FileSystem.BinaryFile.setDataBA(pdfObj.pdfBA);
            mdm.FileSystem.BinaryFile.writeDataBA(mdm.System.Paths.desktop+"generate.pdf");
    
        }

        private function showImage(event : Event) : void {
            
            l.scaleX = l.scaleY = .4;
            var mc : MovieClip = new MovieClip();
            mc.buttonMode = true;
            mc.addEventListener(MouseEvent.CLICK, savePDF);
            mc.addChild(l);
            this.addChild(mc);
        }
        
        private function savePDF(event : MouseEvent) : void {
            event.currentTarget.alpha = .5;
            pdfObj.setData(l, 1);
            pdfObj.savePDF();

            
        }
    }
}

You can also download source files from their hosting service and test it on your computer.
Feel free to give me any comments about AlivePDF, it’s very interesting to know what you think about this AS3 library.

Advertisement

Zinc 3 is out!

I’ve just seen new MDM site where you can find the new release of Zinc!

I download it and I want to try in next few weeks, I hope to great new features and new performance.

You can find more information in Zinc page, take a look and stay tuned to read new Zinc 3 experiences!

First images of Zinc 3.0

Zinc 3.0 codename Pandora will be release at Q1 of 2008, but now you can see first images for Macintosh and Windows directly from Jaspal Sohal blog.  You can also try to enter in Candidate BETA program if you are registered in multidmedia.comIn fact they said:“The Public Beta will be a Release Candidate Build with all functionality that will be in the final release. Registered Users will have an opportunity to download the Public Beta and test it over the Holiday period. The Zinc™ 3.0 Public Beta will be available for both Windows and Mac OSX.”

Flash Lite 3 & Zinc 3.0, new softwares released this weekend

This weekend Adobe announced that is ready new flash lite version developer edition, dowloadable for free from labs.adobe.com.
Remember that you can develop flash lite 3 contents only if you upgrade Flash CS3 and Device Central.
More information about features and files to download you can see at Bill Perry’s blog.
If you test this developer release in your device, remember to write in Flash Lite 3 Forum which device you are using!

You can also see Flash Lite 3 application in action from Biskero’s blog, very cool video!

Finally multidmedia announce Pandora project that is new Zinc version with new big features and optimization.
New features are (from Pandora’s devlab):

. Mac OSX Output will now be in Native Universal Binary and optimized for Mac 10.5 “Leopard”
. Windows Projectors will have been specifically optimized for Vista
. Windows & Mac OSX Projectors almost 500% faster than Zinc 2.5 Projectors
. The entire {mdm}Script API will now be 100% Modularized
. Dynamic Form Creation
. An extensive Custom Command SDK will be available for both Windows and Mac OSX Developers
. {mdm}Script 3.0 – Completely re-written from scratch and now Unicode (UTF8/UTF16) Compliant
. {mdm}Script 3.0 – Greater Similarity between Windows & Mac OSX Classes
. Completely New GUI on Windows & Mac OSX – Screenshots available in December 2007!
. Several other “Industry Firsts” to be revealed in December 2007!

Probably Zinc 3.0 will go out in January 2008 but a public BETA will be release in December.
So take a look at Multidmedia site!