I read this post from DevGirl about AlivePDF, an AS3 API to save Flash content to PDF. The examples showed how to use a server-side script or using AIR. Hmmm... this could be a nice little introduction into the world of FP10 me thinks. I haven't spend any time looking into the FP10 features but thought this could be fairly quick and easy to do... and it was!
The example shows two textfields. Enter some text in the title and also some in the body. If you're lazy no worries I added some already for you. I used the AlivePDF (0.1.4.6) API which is dead easy to use. Here is the example and the full source. (Flash Player 10 required)
Selah.
Selah.
Example
Source
package com.wezside.alive.pdf.core
{
import org.alivepdf.colors.RGBColor;
import org.alivepdf.fonts.FontFamily;
import org.alivepdf.layout.Orientation;
import org.alivepdf.layout.Size;
import org.alivepdf.layout.Unit;
import org.alivepdf.pages.Page;
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Method;
import flash.display.Sprite;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TextEvent;
import flash.net.FileReference;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.utils.ByteArray;
/**
* @author Wesley.Swanepoel
* @version .001
*/
public class Main extends Sprite
{
private var pdf:PDF;
private var page:Page;
private var body:TextField;
private var title:TextField;
private var ba:ByteArray;
public function Main()
{
ba = new ByteArray();
title = new TextField();
title.type = TextFieldType.INPUT;
title.border = true;
title.borderColor = 0xefefef;
title.width = 500;
title.height = 50;
title.defaultTextFormat = new TextFormat( "Times", 40, 0x666666 );
title.x = 20;
title.y = 20;
title.text = "Title";
addChild( title );
body = new TextField();
body.type = TextFieldType.INPUT;
body.border = true;
body.borderColor = 0xefefef;
body.width = 500;
body.height = 200;
body.wordWrap = true;
body.multiline = true;
body.x = 20;
body.y = 75;
body.text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent tempor ultricies eros. Sed erat ipsum, ultrices fermentum, euismod ac, commodo nec, tellus. Curabitur faucibus. Proin at sapien. Aliquam ullamcorper. \n\nAliquam tincidunt. Nunc blandit, mauris eu egestas consectetuer, enim lacus ullamcorper mi, sed dignissim nisl erat sed nulla. Donec nisl. Vestibulum volutpat congue odio. Maecenas nibh sem, congue vel, scelerisque non, placerat sed, nisl. Cras congue. Nam iaculis. Fusce adipiscing ipsum vitae sem lacinia congue. Pellentesque luctus ultrices nisi. Etiam ut mi nec arcu pellentesque sagittis. Duis accumsan est a nibh. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi vulputate turpis suscipit ipsum. Nunc posuere tortor id est. ";
body.defaultTextFormat = new TextFormat( "Arial", 12, 0x666666 );
body.setTextFormat(new TextFormat( "Arial", 12, 0x666666 ));
addChild( body );
var buttonCSS:StyleSheet = new StyleSheet();
buttonCSS.setStyle("a:link", {textDecoration: "none", fontSize: 24});
buttonCSS.setStyle("a:hover", {textDecoration: "underline", fontSize: 24});
var button:TextField = new TextField();
button.autoSize = TextFieldAutoSize.LEFT;
button.width = 200;
button.addEventListener(TextEvent.LINK, generatePDF );
button.htmlText = "[ Generate PDF ]";
button.styleSheet = buttonCSS;
button.x = body.x + body.width - button.textWidth;
button.y = body.y + body.height;
addChild( button);
}
private function generatePDF(event:TextEvent):void
{
page = new Page(Orientation.PORTRAIT, Unit.MM, Size.A4 );
pdf = new PDF();
pdf.addPage( page );
pdf.textStyle(new RGBColor(0x666666));
pdf.setFont( FontFamily.TIMES, "", 40 );
pdf.addText( title.text, title.x, title.y );
pdf.setFont( FontFamily.ARIAL, "", 12 );
pdf.setAuthor( "wezside" );
pdf.setXY( body.x, 30 );
pdf.addMultiCell( 150, 5, body.text );
pdf.setTitle( title.text );
ba = pdf.save( Method.LOCAL );
var file:FileReference = new FileReference();
file.addEventListener( IOErrorEvent.IO_ERROR, error );
file.addEventListener( SecurityErrorEvent.SECURITY_ERROR, error );
file.save( ba, title.text + ".pdf");
}
private function error( event:IOErrorEvent ):void
{
body.text = event.type + " | Error occured | " + event.text;
}
}
}


cool!
Having some issues with some french special chars (try "รง" for example). And it could be cool to add support for carriage return -> new line.
but i guess all these are alivePdf issues.
nice! I thought only processing was able to convert to pdf.