I want to create a PDF file contaning a title, a local picture and a small description.
I'm going to use AlivePDF (http://alivepdf.bytearray.org), Gumbo, Spark and a little bit of Halo to create a simple UI, and File, FileStream to save the file.
First I'm going to design a simple User Interface :
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/halo"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="init(event)">
<fx:Script source="com/palleas/controller.as" />
<s:states>
<s:State name="default" />
<s:State name="pictureSelected" />
</s:states>
<mx:Form>
<mx:FormItem label="Title">
<s:TextInput id="pdfTitle" />
</mx:FormItem>
<mx:FormItem label="Picture">
<s:Button label="Browse" label.pictureSelected="Change picture" id="browseButton" />
</mx:FormItem>
<mx:FormItem label="Description">
<s:TextArea id="pdfDescription" />
</mx:FormItem>
<mx:FormItem>
<s:Button id="createBtn" label="Create and download PDF" enabled="false" enabled.pictureSelected="true" />
</mx:FormItem>
</mx:Form>
</s:WindowedApplication>
And then some actionscript to create the pdf :
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import mx.controls.Alert;
import mx.events.FlexEvent;
import org.alivepdf.display.Display;
import org.alivepdf.fonts.FontFamily;
import org.alivepdf.layout.Orientation;
import org.alivepdf.layout.Unit;
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Method;
// will contains the chosen picture
protected var pictureBytes:ByteArray;
// init the application
public function init(e:FlexEvent):void {
browseButton.addEventListener(MouseEvent.CLICK,onBrowse);
createBtn.addEventListener(MouseEvent.CLICK,onCreate);
}
// called when the user clicks on "chose a picture"
// open a browse window
protected function onBrowse(e:MouseEvent):void {
var file:File = new File();
file.addEventListener(Event.SELECT,onFileSelected);
file.browseForOpen("Chosse a picture",[new FileFilter("PictureOnly (*.jpg,*.png,*.gif)","*.jpg;*.png;*.gif;*.jpeg")]);
}
// called when the user has selected a picture
protected function onFileSelected(e:Event):void {
pictureBytes = new ByteArray();
var selectedFile:File = (e.currentTarget) as File;
var stream:FileStream = new FileStream();
stream.open(selectedFile,FileMode.READ);
currentState = "pictureSelected";
stream.readBytes(pictureBytes);
}
// called when the user clicks on "create pdf"
protected function onCreate(e:MouseEvent):void {
var file:File = new File();
file.addEventListener(Event.SELECT,onSaveFileSelected);
file.browseForSave("Save the pdf file");
}
// called when the user has selected the path he want
// to save his pdf to
protected function onSaveFileSelected(e:Event):void {
// create the PDF
var pdf:PDF = new PDF(Orientation.PORTRAIT,Unit.MM);
pdf.setDisplayMode(Display.REAL);
pdf.addPage();
pdf.setFont(FontFamily.ARIAL,"",20);
pdf.writeText(1,pdfTitle.text);
pdf.addImageStream(pictureBytes,0,25);
pdf.addText(pdfDescription.text,0,130);
// open the target file
var f:File = e.currentTarget as File;
var s:FileStream = new FileStream();
s.open(f,FileMode.WRITE);
s.writeBytes(pdf.save(Method.LOCAL));
s.close();
Alert.show("PDF file has been saved to "+f.nativePath+" !");
}