|
|
Introduction
I've been using the AlivePDF library for a few days now and have some observations that might help others just starting off. I invite others to post what you've found here as well.
Current Position
There's two ways to set the current position. You can use pdf.moveTo for drawing API operation and pdf.setXY for text based operations.
There are also pdf.setX and pdf.setY methods, but these have side effects, take a look at the code before using them.
Adding Text
Adding text is one of the most common things one would like to do while making a PDF. There are a few ways of doing this.
addText()
The addText() method will put out text at a given coordinate. No word wrapping is done.
pdf.setFont( FontFamily.ARIAL , "", 32); pdf.addText(Project.instance.title,30,50);
writeText()
The writeText method will output text at the current location. You can also specify a link that clicking on the text will take you to.
pdf.setFont( FontFamily.ARIAL , "", 32); pdf.setXY( 20, 20); pdf.writeText ( 32, "This is my text");
addMultiCell()
addMultiCell ( pWidth:Number, pHeight:Number, pText:String, pBorder:=0, pAlign:String='J', pFill:int=0)
The addMultiCell lets you constrain text to a "cell" with a certain width. The cell starts at the current position and is the given width. The height IS NOT the height of the cell, it's the height of each line of text.
pdf.setFont( FontFamily.ARIAL , "", 32); pdf.setXY( 20, 20); pdf.addMultiCell ( 200, 32, "This is my text");
Example
The following code:
pdf.setFont(FontFamily.ARIAL , "", 12);
pdf.addText("This is some addText() text",20,20 );
pdf.setXY( 100, 120);
pdf.writeText(20,"The following lines are made using writeText(). This is my first statement. You can use \\n to line break like this:\nThis is my first statement. This is my first statement. This is my first statement. This is my first statement. This is my first statement. ");
pdf.setXY( 120, 400 );
pdf.addMultiCell( 200, 20,"This text added with addMultiCell() and is constrained within a cell. It will word wrap, but won't go outside the bounding box. It starts at the current position."); will produce the following pdf:
Saving to a file in an AIR application
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();
// OLD WAY: 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);
savePDF();
}
protected function savePDF()
{
var f:FileStream = new FileStream();
var file:File =
File.applicationStorageDirectory.resolve( filename );
f.open( file, FileMode.WRITE);
var bytes:ByteArray = pdf.savePDF(Method.LOCAL);
f.writeBytes(bytes);
f.close();
}
}Opening the PDF from AIR
This isn't strictly an AlivePDF specific thing, but I bet there's a lot of people who would like to open the PDF for the user once it's written out. This will cause the user's browser to attempt to open the file. If no browser is running, one should be opened.
var request:URLRequest = new URLRequest( file.url ); navigateToURL(request);
You can also use AIR's HTML control to view PDF's
var request:URLRequest = new URLRequest( file.url );
var window:Window = new Window();
var html:HTML = new HTML();
if( HTMLControl.pdfCapability != 0 )
{
// We can't display PDF in the app, lets hope their browser can. If not,
// the browser will hopefully do a sane "Download to..." type thing.
navigateToURL( request );
return;
}
html.location = file.url;
html.percentHeight=100;
html.percentWidth=100;
window.addChild(html);
window.title = "PDF Export";
window.width = 800;
window.height = 650;
window.open();
Sign in to add a comment


Could you show a complete sample application!
Is there a way to display the PDF,or pages of the PDF, with this API? If that feature doesn't exist yet, is it planned for the future?
alivePDF contains mention of a php script. Does this mean it will not work stand-alone - that is to say on a flash application on, say, a CD running on a PC with no internet access?
Hi Joshua >> I will start making tutorials really soon ! Stay tuned :) It's coming soon.
Hi fsomm, AlivePDF is not able to render PDF. It only lets you generate PDF. If you want to render a PDF you can use AIR and HTML. (btw it uses Adobe reader engine, so it has to be installed on the computer)
Hi finbarr69,
AlivePDF can work stand alone with no problem :) It's 100% client side PDF generation, no server involved. You could generate PDF's from an online Flash app, an AIR application or even a CDROM app.
kind regards guys !
Thibault
May be it is necessary to clarify some points. As far as I understood eveything : - php is a server side scripting language, so you can't use this solution for a standalone application. - for standalone application only AIR can save a local file as PDF and reload it in an html component. If I am wrong, let me know...
Jean-Yves
Thanks, this is really a great API. Looking at the docs (of a view weeks ago), I was not able to find a way to open an existing PDF and manipulate it via AlivePDF. Is this possible at all with AlivePDF? Thanks, again!
Hi Jean Yves,
AlivePDF uses some parts of a PHP PDF library called FPDF written in PHP. AlivePDF is a full ActioNScript 3 library which can be used in Flash, Flex and AIR. You can generate a PDF from scratch and save it with those 3 platforms.
kind regards,
Thibault
Hi,
AlivePDF does not have such a feature for the moment. You cannot open an existing PDF and modify it. But this is a nice idea, why not adding this later :)
kind regards,
Thibault
Hi, I need to generate a PDF for printing. Other than addImage() a high res JPEG, are there other ways, i.e. adding vector artwork so that it can print in high res, or using existing PDF as a base?
Kenneth Kawamoto
Hi, I need to generate a PDF for printing, too! Can anybody post an example with different used functions? Tanks!
Isa
The savePDF function in the example is using package flash.filesystem which is only for AIR. If I have a flex application that generate some chart, and I want to create a pdf and allow people to download from flash player, what should I do?
Hi Michelle,
Check the following video tutorial. Hope this helps :)
http://www.alivepdf.org/?page_id=5
http://alivepdf.bytearray.org/wp-content/tutorials/alivepdf-tutorial-flex-application.swf
If not, let me know ;)
cheers,
Thibault
Hello,
I'm working with AlivePDF, but due to some restrictions on how my app will be deployed, I am modding PDF.as to use FileReference? instead of navigateToURL. Instead of posting the raw data, I create an URLVariables object like so:
var urlVars : URLVariables = new URLVariables(); urlVars.pdf = save( Method.LOCAL; myRequest.data = urlVars;
I do this because FileReference? can't accept post data as a ByteArray?.
It seems to be working--I can download the file, and if I open it in a text editor it contains all the PDF code. But if I try to open it in Adobe Reader, I get an error that reads: "There was an error opening this document. The file is damaged and could not be repaired."
Any thoughts on why it's doing this?
Many thanks,
David Ham