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

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.

New Flex resource: flexets forum

Today is my last at work, from tomorrow to 23 August I’m on holiday.

But before to go on holiday, I want to share with you a new resource made from Franto that is a new Flex forum where you can talk about Flex, AIR and actionscript 3, share your knowledge with other developers and so on.

It started a couple of days ago so there are few people now, but I’m sure that could become a great resource for all Flash Platform developers.

Flexest launches also a service called Coaching forum, where new developers has a traning coach on Flex, AIR or Actionscript that helps to learn this stuff.
If you have few minutes during this holiday period, take a look and register in this community to start share your Flex exeprience! see you there guys!

My 2 cents about Let’s tour

Yesterday Thomas, Tiziano, Samuele and I finished Let’s tour.
Let’s tour was a tour around 5 Italian cities (Turin, Florence, Milan, Padua, Verona) where we showed what a team do when work together with new technologies in the same project.

In fact we made this session during this tour:

9.30AM – PHP & Framework how to make a web form with a MVC framework

10.00AM – Flash Lite: create a mobile experience to register yourself for an event

10.30AM – Illustrator: how to create dynamic content with Illustrator

11.00AM – AIR & Flex: working with web data and export them for Illustrator and Photoshop

11.30AM – Photoshop: working with external data

12.00AM – PHP: how to create dynamic PDF for mobile & print

We met a lots of people, developers, designers and account too, and we understand that in Italy we MUST talk more about those technologies from university to big company, because there are so many people that are interesting to grow up with those technologies!
I was so surprised when I asked in Florence, at Master of Multimedia, how many people knew Flex or AIR… I think that 80% of them knew those technologies and it’s GREAT!
They appreciate a lots this kind of event in particular they love the informal but technical way to show stuff during our sessions.
We are talking to organize another one maybe and we are working to make a big international event that it takes part in Italy next year.
I must to say THANK YOU to all people that came during this tour, that help us to organize meeting room in cities, and I hope to make it again.
A big thank you to Let’s course guys that follow me in this crazy idea! I think we spent great time together.

AIR tour, last date in Milan

AIR tour is finished! Last date was in Milan and obviusly was very cool!

Location was pretty nice, great organization, cool food and a lots of stuff (T-shirt is amazing!).
A very friendly atmosphere, Adobe guys made a great overview about AIR from the beginning to final deployment.
I appreciate a lots Serge Jesper and Andrew Shorten‘s session, they talked about how to deploy and sign an AIR application and a great overview about LCDS and Blaze DS.
But my favourite one was Brimelow‘s session, he showed a lots of cool samples and in a very cool and funny way.

This weekend I’ll publish all photos made during the event in my Flickr account

 

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!

 

Flex Builder compile Flash Lite projects, is it a dream?

Today was last day at 360Flex Europe, I spent great time there!

Talking with 2 Flex team guys, they said to us that if you’d like some new features in Flex 4, only make a feature request to Adobe jira system.
So I said: “One of the most cool feature that you could implement in next version of Flex Builder, it could be to compile Flash Lite projects and maybe Flex Builder could interact with Device Central.” They said me: “It’s first time that ask me this implementation“.

So I make a feature request for next version of Flex Builder, I think it could be so interesting working with Flex IDE and when I compile a Flash Lite project open directly into Device Central.
What do you think about it? If you’d like this feature too, vote please!

Make comments and we could create a great discussion about it.

second day at 360Flex Europe

That’s it! Second day is finished, but it was very interesting! I know better a lots of speakers and attendees, I love share experience and points of view with them.
I go to Piergiorgio session on Papervision and he was amazing! Very cool session man!
So I go to Giorgio session on Accessibility with Flex and he found a great solution to interact with screen readers, that was cool!
Finally, I can’t miss it, I see an introduction on Cairngorm by Neil Webb (you rock man!) very cool session and so funny preso, I love it!
Sorry Zohar if I can’t come in your session but I hear that you made a very cool stuff!
Also, if you’d like to take a look at some cool video about the conference, please view Zohar mix!

Tomorrow is the final day, I spent great time here, I hope to keep in touch with a lots of attendees and speakers that I met here.

In this weekend I’ll also put in my Flickr account some photos.

first day at 360Flex Europe..my impressions…

It’s just finished this long day, I slept 2 hours to finish my preso and samples, I hope that people enjoy my Flex BitmapData session!
I’ll put preso and samples in the next few days in this blog, so take a look everyday guys!

There weren’t a lots of people at 360Flex, but I met a lots of strong developers for all over the world, there were great share moments and all speakers help toghether to make a better session ( I love so much this atmosphere).
I’m so glad that a lots of italian developers and companies are lost this cool appointment to know better Flex and AIR technologies!

In fact you could talk with Adobe people (Evangelist, marketing people and so on) and with cool speakers that help you to understand better the power of flash platform! I hope tomorrow I can see more people here!
This evening all speakers go out with Adobe people to have dinner and to know us better and talk about Adobe softwares and strategies, that’s cool!

I’ll put also new event photos in my Flickr account, so please take a look sometimes!

UPDATE

You can download my preso and my samples NOW!
Enjoy

Road to 360 Flex Europe

1 month more or less and in Milan I’ll partecipate at 360|Flex the most important Flex event in Italy (maybe in Europe?).
In the last schedule I saw my seminar is in the first day (7 April), now I’m waiting the final one.
I’ll talk about BitmapData class, I’m making 3 samples and a real case history made with Flex and AIR to manipulate images.

There will be lots of important and great developer that are coming from all over the world, I love this event, it will be amazing!
You can find a lots of Adobe employees, Adobe partners, Adobe UG and so on, an event that you can’t lost.

So see you at 360 Flex guys!