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.

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.”

Touch screen and cd-rom with Multidmedia Zinc

One recent project that I produce is a touch screen software, obviusly I made it with Zinc.

So I’d like to share my experience with my blog readers.

First of all, I must say that Zinc has a too bad documentation, a lots of examples don’t work and there are some big bugs.

ZINC and FTP CONNECTION
In my last project I add a routine to download new files from touch screens.
Zinc FTP API works only in Windows, but for this project, all touch screens had OS Win, so no problem.
In this case, I found 3 problems, first of all I can’t download all files togheter because Zinc had some issues to do this operation, so when I download a file, when onComplete event triggered I launch another one.
Probably Zinc didn’t copy all bytes from FTP server to client and it crashed.
So I put a setTimeOut function to call a new file after 1 second and it works!
Finally, using close method of FTP class, FTP session is still opened…so I delete FTP istance and it works! So crazy!

ZINC and Flex
Recently I produce 3 projects with Flex and Zinc…I found a lots of interesting issues.
When you work with Zinc in Flex, you must add SWC library to use Zinc API, so you bring it from 2 folders(one .swc in for Flash and one for Flex, but you could use both in Flex) and there is different way to implements same API with .swc for Flash or for Flex, so take a look of which .swc you could use to work.
Another problem that I found is when you create a transparent application and you have mx:TextInput, if you have focus in textinput component and you try to use keyboard arrows to move cursor, you’ll receive an unknown error!
In Multidmedia forum Peter suggests to me to use callLater function, but it doesn’t work.

So those are some problems that I would share with you, I hope that they are useful for everyone.