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.

Advertisements

I’m just come back from 3GSM World Congress

Here we are!
Yesterday I came back from Barcelona (what a wonderful city!), I was there for 3GSM World Congress invited from Adobe to show our latest Flash Lite contents made in collaboration with Mutado, an Italian creative and interactive company.
It was amazing for us and I’d like to thank you Adobe people to give us this opportunity, thank you guys!

Adobe stand was amazing I love it so much and also Adobe announced lots of new stuff from new Flash Lite player delivery to new challenge that allow developers to win $30.000!

Flash Lite challengers is a contest that will finish at the end of May and we’ll know winners in the middle of June.
Main prize is $30.000 for the best Flash Lite application, but there are also categories prizes from games to applications, it’s a great opportunity to show your mobile content and win!
More information about this challenge on Adobe site

Another great news is that now, we have a new way to distribute our Flash Lite contents, in fact if a device goes trough a Flash Lite content on the web browser or download a Flash Lite application, if this device doesn’t have installed Flash Lite 3.1, it will be downloaded from web and installed on the device.
More information about this new way to deliver your content on Labs site.  

Also Adobe and Nokia give us a new opportunity to invest in mobile projects with Open Screen Project fund ($10 million), in fact this fund is created to help companies and developers to join in mobile Flash world!
More information about it in open screen project site

Finally I put a review on mobile.actionscript.it about new Flash Lite devices like Palm Pre or Nokia N97, so feel free to comment it (and take a look about our new mobile website).

 If you want to see a quick tour about 3GSM feel free to take a look on my Flickr account.

3GSM World Congress 2009

Hi All,

it’s long time that I don’t write down a post.
In last months I changed lots of things, new fellows, new tutorials, new customers and big projects to develop with AIR and Flex.
So lots of things to do!

This year I’ll be at 3GSM World Congress for the second time, so if you will be there, please feel free to tell me, maybe you can drop me an email or add a comment under this post!
I’ll arrive at 15th and I’ll come back 20th February, so 5 amazing days in Barcelona.
If you don’t come, don’t worry I’ll post about this great event when I’ll be there and… there is a special surprise for anybody that follow this blog, so stay tuned and take a look at this blog in next few days.

Adobe MAX: day 1

Here we are, it has just started Adobe MAX 2008, I’m very excited and in this day I see a lot of cool and interesting things!

In General Session this morning we see new name of Thermo project, now we must start to call it Flash Catalyst, too long but not bad.
There are new interesting things on mobile side, Adobe is working in a new product that package Flash Lite contents in .sis or .cab file and delivery it over-the-air, maybe an important step trough a new Adobe mobile era.

Another interesting project that is in BETA now, it’s also my favorite one, is CoCoMo, I meet Nigel Pegg, amazing guy, that coordinate the project with Dr. Fang (:D).
Now CoCoMo is in Labs, you can download it and start to work with those components that allow you to implement your Flex applications with Connect Now features, so you can share your desktop, make a conference call with video, chat and so on.

There are other 2 minor project that Adobe releases today: Wave and Tour de Flex.

The first one is a social networking aggregator made with AIR that allow you to manage your social networking like twitter, flickr and so on in a all in one application.
Tour de Flex is another AIR application that is focused for developers and designers that approach Flex for the first time, I copy the purpose of the application from own site:

  • Provide non-Flex developers with a good overview of what is possible in Flex in a “look and see” environment
  • Provide Flex developers with an illustrated reference tool
  • Provide commercial and non-commercial Flex developers a place to showcase their work

You can find those projects directly from Labs too.
I meet also a lot of developers and designers that are involved in very cool projects, but I’ve also the honor to meet Simone Legno aka Tokidoki! It’s one of my favorite designers! Max respect for him.

But I must write also that there is ONLY 1 WI-FI CONNECTION here for more then 5.000 people… it’s unbelievable… 

See you tomorrow for resume of second day.

Adobe MAX SF… I’m coming

Yes, also this year I’ll go to Adobe MAX, the biggest Adobe event with more 4600 attendees that are ready to see the future of Adobe softwares.

We will see Thermo (2 sessions during the event), the new Adobe IDE for interaction designers and multimedia designers that allow to simplify Flex Interface customizations.

But also, I’m sure to find a lots of old and new friends there like Mobile guys (Biskero, Scott, Mark, Dale, Bill…) and girls (Mariam…) and have lots of fun with them.

I’ll have a speech about AIR and SQLite during 360Max the event in  event that is totally free for all people that are at MAX.
During my session I’ll talk about:

  • introduction to SQLite
  • how to connect AIR to a db
  • create, modify, delete and search into records
  • transactions and prevent SQLinjections with parameters
  • AIR data encryption and others technique to encrypt data
  • SQLSchema and others utilities to work with SQLite
  • embed a db in an AIR application
  • best practices & limitations

everyday I’ll put my impressions, photos and news about this great event, so if you are interesting to follow MAX, feel free to come there and read it!

See you soon… or maybe at Adobe MAX.

Next events until to the end of years

When September starts, there are so many events until to the end of years, it’s a magic period for me to find new friends, talking with developers and find “old friend”!
This year I put in my agenda a tons of them.
In the end of this week I’ll go to Brighton at Flash on the beach 08, the most inspire event for flash developers, this year I’ll find there a lots of friends like Piergiorgio, ZoharPeter, Franto, Marco, Scott and I’m sure to find my belgium and australian friends that I met last year at FotB very cool time spent with them.

Then I’ll have SMAU, the only IT event in Italy, where we are there to show Let’s course, our traning project with Thomas and Tiziano for all event days.
There we’ll make 2 sessions per day, totally free in our stand.
We’ll show interesting tips & tricks of Photoshop, Illustrator, PHP, Flex, AIR and… a very very big argument… but I can’t say it now.

In november there is Flex Camp in Italy made by actionscript.it Italian Adobe User Group, this year there are a lots to talk about Flex and AIR and I’m sure to find very cool italian flex developers.
If you are interested to take part of this event, it’s totally free, you can register yourself at Flex Camp form.

The least but not the last… ADOBE MAX in SAN FRANCISCO

I think that I must not talk about this event… this year in San Francisco, my first time in California, and I’m sure to find tons of developers and people that love this job like me.
I bought a pre-event session on Blaze DS and Livecycle, I’ve not worked yet with them so I think is a good opportunity to start learn them in depth. 

Probably I’ll go to Adobe MAX in Milan also, but only for a day… there are too many events in few time… but I must meet Matt and other guys that come here from all Europe!

So, you know, too many events, too many opportunity to share skills and find new friends.
If you find me in one of those events, please feel free to stop me!
See you there guys!

Road to Scotch on the Rocks

Yes guys, 3 weeks and it starts! I booked my hotel room and my flight a month ago and now I’m making material for my session.

I’ll talk the last day (Friday at 10.30 AM) about “Manage Flash Lite Data with Flex and Air”, when I propose this session I think that it could be very interesting to view the power of Flash Platform, in fact I’ll show a real case history that I’m working on now and explain how to create a little CMS with Flex and AIR for Flash Lite contents.
Flash Platform is too amazing, you can develop from mobile to desktop and not different application but a unique application that collaborate together.
You can find great speakers there like Ben Forta, Peter Elst, Neil Webb, Charlie Arehart, Mark DrewAndrew Shorten so there are all elements to make a great event!  

I’m sure to meet a lots of new strong developers and designers and I’ll spend a fantastic time in Edinburgh.
For more information about the event or to take part, please see Scotch on the Rocks web site. 

See you there guys!