This blog isn't maintained anymore. Check out my current project, an agile/scrum management tool.

Friday, August 24, 2007

AlivePDF Flex/AIR Example

It took me a couple minutes to figure out how to write a PDF to disk using AlivePDF, hopefully this short example will save you those few minutes.

public class ProjectPDFExporter

{
private var filename:String = "test.pdf";
private var pdf:PDF;

public function exportPdf(filename:String) : void
{
this.filename = filename;

pdf = new PDF();

pdf.addEventListener(Event.COMPLETE, onComplete);

pdf.setDisplayMode (Display.FULL_PAGE,
Layout.SINGLE_PAGE);



pdf.addPage();
pdf.setFont( FontFamily.ARIAL );
pdf.addText("My Teax",1,10);
pdf.setFont( FontFamily.ARIAL , "", 32);
pdf.addText("Some more text",10,30);

pdf.finish();
}

protected function onComplete(event:Event)
{
var f:FileStream = new FileStream();
var file:File =
File.applicationStorageDirectory.resolve( filename );

f.open( file, FileMode.WRITE);
var bytes:ByteArray = pdf.getPDF()
f.writeBytes(bytes);
f.close();
}
}

Also, it looks like the first version of AlivePDF.swc is missing at least one class (FontFamily), so you're probably safer using the source instead of the swc for now.

The more I look into it, the more I'm impressed by this library. I'm really surprised Adobe had never made something similar before.

Labels:

4 Comments:

  • Hi Marc,
    Ur AlivePDF code was quite helpful but i'm stuck at a place where i wanna display gradient effect to image in PDF.
    Can u help me out....


    Thanks,
    Nams!!
    (nams_30jan@yahoo.com)

    By Anonymous Anonymous, At 9/19/2007 11:10 PM  

  • Hi Marc,

    My requirement is to generate a PDF doc using the data present in the datagrid.

    I tried incorporating AlivePDF, unable to proceed b'coz mine is a pure FLEX applciation and not AIR [Cant use FileStream etc].

    Is AlivePDF compatible with Flex application's?

    If yes, how to do away with FileStream class's.

    By Anonymous Anonymous, At 12/12/2007 1:06 AM  

  • To do that you need to create the PDF and send it to the server. Then the server can "bounce" it back to the client and let the browser save it somewhere. I've never done that, but there's examples and a server side script to do it in the AlivePDF documentation.

    By Blogger Marc, At 12/12/2007 4:08 AM  

  • This comment has been removed by a blog administrator.

    By Blogger lala, At 1/03/2008 10:35 PM  

Post a Comment

Subscribe to Post Comments [Atom]



<< Home