AlivePDF: AS3 PDF library for Flex PDF creating
AlivePDF is a very useful ActionScript3 PDF library , Here is a class and sample code that create PDF file with a list of images, this class creates a new URL request for each image and when the request is completed it adds a new page with the image.
class:
package { import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.ByteArray; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import org.alivepdf.pdf.PDF; import org.alivepdf.display.Display; import org.alivepdf.images.ResizeMode; import org.alivepdf.layout.Layout; import org.alivepdf.layout.Size; import org.alivepdf.layout.Orientation; import org.alivepdf.layout.Unit; import org.alivepdf.saving.Method; public class PdfGenerator extends EventDispatcher { private var pdf:PDF; private var images:Array; private var outFilePath:String; private var completedPages:int; public function PdfGenerator(images:Array, outFilePath:String) { this.outFilePath = outFilePath; this.images = images; } public function generate():void { this.completedPages = 0; this.pdf = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.A4); this.nextPage(); } private function nextPage():void { this.completedPages < this.images.length ? this.loadImage(this.images[this.completedPages++]) : this.save(); } private function loadImage(path:String):void { var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.BINARY; urlLoader.addEventListener(Event.COMPLETE, this.onImageLoadComplete); urlLoader.load(new URLRequest(path)); } private function onImageLoadComplete(event:Event):void { this.pdf.addPage(); this.pdf.addImageStream(ByteArray(event.target.data), 0, 0, 0, 0, 1, ResizeMode.FIT_TO_PAGE); this.dispatchEvent(new PdfGeneratorEvent(PdfGeneratorEvent.PDF_GENERATOR_PAGE_COMPLETE, false, false, this.images.length, this.completedPages)); this.nextPage(); } private function save():void { this.pdf.end(); this.writeOutFile(); this.dispatchEvent(new PdfGeneratorEvent(PdfGeneratorEvent.PDF_GENERATOR_COMPLETE)); } private function writeOutFile():void { var fileStream:FileStream = new FileStream(); fileStream.open(new File(this.outFilePath), FileMode.WRITE); fileStream.writeBytes(this.pdf.save(Method.LOCAL)); fileStream.close(); } } }
package { import flash.events.Event; public class PdfGeneratorEvent extends Event { public static const PDF_GENERATOR_PAGE_COMPLETE:String = "PDF_GENERATOR_PAGE_COMPLETE"; public static const PDF_GENERATOR_COMPLETE:String = "PDF_GENERATOR_COMPLETE"; public var totalPages:int; public var completedPages:int; public function PdfGeneratorEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false, totalPages:int=0, completedPages:int=0) { super(type, bubbles, cancelable); this.totalPages = totalPages; this.completedPages = completedPages; } } }
main file:
public function createPdf(images:Array):void
{
var generator:PdfGenerator = new PdfGenerator(images, "D:/test.pdf");
generator.addEventListener(PdfGeneratorEvent.PDF_GENERATOR_COMPLETE, this.onPdfComplete);
generator.addEventListener(PdfGeneratorEvent.PDF_GENERATOR_PAGE_COMPLETE, this.onPdfPageComplete);
generator.generate();
}
public function onPdfPageComplete(event:PdfGeneratorEvent):void
{
progressBar.setProgress(event.completedPages, event.totalPages);
progressBar.label = event.completedPages + " of " + event.totalPages;
}
public function onPdfComplete(event:PdfGeneratorEvent):void
{
trace("completed")
}





Previous
Next
Tags: