My favorites | Sign in
Project Logo
                
Search
for
Updated Sep 03, 2007 by marc.hughes
APINotes  
Some notes about different parts of the API, as found from users.

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();

Comment by Joshua.e.Knight, Oct 28, 2007

Could you show a complete sample application!

Comment by fsomm...@autospaces.com, Nov 06, 2007

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?

Comment by finbarr69, Nov 10, 2007

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?

Comment by thibault.imbert, Nov 12, 2007

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

Comment by jy.linet, Nov 19, 2007

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

Comment by fsomm...@autospaces.com, Nov 28, 2007

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!

Comment by thibault.imbert, Dec 10, 2007

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

Comment by kennethkawamoto, Feb 05, 2008

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

Comment by isabelmarquardt, Apr 03, 2008

Hi, I need to generate a PDF for printing, too! Can anybody post an example with different used functions? Tanks!

Isa

Comment by michelle.wei.xue, Jun 20, 2008

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?

Comment by thibault.imbert, Jun 22, 2008

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

Comment by misterdham, Jul 03, 2008

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

Comment by chalaouxfr, Jul 30, 2008

Sorry to ask again about that but:

Is it possible to generate and save a pdf with images directly with flex(flash) client without the php script on the server.

If not, could I have an explanation about the problem ?

FR

Comment by copperm...@hanmail.net, Aug 22, 2008

hi~

I'm a Korean.

I use FLEX.

but I test now your AIR sample testing now.

My 2bytes word Korean. So I can't print Korean. like ??? .

How can i write Korean in PDF file.

good luck

Comment by lehmann.dieter, Oct 28, 2008

I am trying to display a flex chart on a pdf. I use the addImage(chart, .... ) method which according to the doc's creates a snapshot and adds the image to the pdf. This unfortunately results in the message when Acrobat is opened "There was an error opening this document. The file is damaged and could not be repaired".

Do you know of any issue adding charts as an ImageSnapShot?..?

Comment by jeremy.richard.87, Jan 08, 2009

Can I save a pdf open in AIR with HTMLLoader ?

Because I try to save the pdf with the javascript command app.execMenuItem("SaveAS") but it didn't work.

Comment by daxeshpro, Jan 09, 2009

Can I render the content in AlivePDF without generating the pdf physically?

Or something that allows me to dynamically add content without file generation and render the output on a HTML page.

Comment by raufaamir, Feb 01, 2009

I am working on flash application. I want to save that application's output as pdf in realtime. Is it possible in alivepdf?

one more question that can i save pdf for printing (eps, li or any vector format) purpose ?

Comment by casey415, Feb 17, 2009

I have a Flex application that users fill out and then press a button to create a PDF of their work. The users work within a canvas and I set AlivePDF to print the canvas as follows.

myPDF.addPage(); myPDF.addimage(nameOfCanvas);

This works well when all their work is visible in their browser's window. If the browser window is small the PDF created only shows the portion of their work that is visible plus vertical and horizontal scroll bars. Is there a way to create a PDF of the entire canvas instead of the visible portion in the browser?

Comment by sunpatel, Mar 03, 2009

I have created a demo app for generating pdf from a VBox with dynamic child....it doesn't work either. Take a look. I am using Flex4 SDK to save the PDF locally in FP 10.

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

<mx:Script>
<![CDATA[
import org.alivepdf.images.ResizeMode?; import org.alivepdf.saving.Method; import org.alivepdf.images.ImageFormat?; import org.alivepdf.pages.Page; import org.alivepdf.display.; import org.alivepdf.layout. import org.alivepdf.pdf.PDF;
private var mPDF:PDF;
private function onPDFCreate():void {
mPDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4 ); mPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE ); mPDF.setTitle('Module'); for(var i:int=0; i<10; i++){
var newPage:Page = new Page( Orientation.PORTRAIT, Unit.MM, Size.A4 ); mPDF.addPage( newPage ); var btn:Button = new Button(); btn.label = 'Click '+i; vbox.addChild(btn); mPDF.addImage(vbox, 0, 0, 100, 100, ImageFormat?.PNG, 100, 1,
ResizeMode?.FIT_TO_PAGE );
} var file:FileReference = new FileReference?(); file.save(mPDF.save(Method.LOCAL), "Module.pdf.pdf");
}
]]>
</mx:Script>
<mx:VBox id="vbox" borderStyle="solid" borderColor="0xff00ff"/> <mx:Button id="btn" label="CreatePDF" click="onPDFCreate()"/>
</mx:Application>

Need a quick solution...please help!!

Comment by fortcollinsjoe6, Mar 24, 2009

I am having difficulties getting save to work from a Flex application. I am running the flex application in IIS 6.

I am trying to use the .cs file you have provided in the latest version because installing php is currently not an option.

I have gone into my machine web.config and commented out

<add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="true" /> line.

When I run using either INLINE or ATTACHMENT nothing happens.

In Firefox it opens a new tab with a blank page. In IE it open a new browser with "Internet Explorer cannot display the webpage." Here is my save method call

pdf.save(Method.REMOTE, "http://localhost/PortMap/Create.cs", Download.INLINE, "Report.pdf");

Please Help

Comment by fortcollinsjoe6, Mar 24, 2009

I was able to find a solution using the .net code file. I created an asp.net web application. I copied the code from the create.cs file into the code behind of the default.aspx.

After compiling I publish the project to the web folder that hosts the flex application (copy the /bin folder, the default.aspx file and web.config files).

For the url in the save method I use http://localhost/PortMap/default.aspx.

Everything seems to be working great. Thanks for the effort, this provides functionality that will be incredibly helpful.

-joe

Comment by piet.vanremortel, Mar 30, 2009

@casey: For creating a PDF of a chart, I managed to make an invisible Canvas of larger size, regenerating the image (AS code, not MXML) to that invisible Canvas, and snapping that canvas in a callLater() step after the renderer did its work on the invisible Canvas. That solved the problem of having a small or partial chart put in the PDF.

P

Comment by parag.kamat, Apr 21, 2009

@fortcollinsjoe6: instead you can use the save method's last parameter pdf.save(Method.REMOTE, "http://localhost/PortMap/Create.cs", Download.INLINE, "Report.pdf","self");

Using "self" as the last parameter will stop opening the new blank window. Hope this helps.

Blank is the default parameter which results in opening new blank window.

Comment by parag.kamat, Apr 21, 2009

@piet.vanremortel hi .. could you pls share the code for this? I am kinda stuck becos of the very same problem.

Comment by agoel.khs, Apr 24, 2009

Anyway to save a PDF with the whole canvas having vertical & horizontal scrolls..?? Since addImage only takes an image of current state of canvas not the clipped content. Pls help.

Comment by supp...@paperwink.com, Apr 24, 2009

A couple of questions... First of all, is there any way to expand the available PDF fonts to those embedded in the SWF?

Second, is there already a script available to save the PDF on a server, without prompt, and then send a complete confirmation back to Flash?

Thank you for this great library!

Comment by supp...@paperwink.com, Apr 27, 2009

Also, I can't seem to use variables in the .setFont method. is greatly holding back development at the moment.?

Comment by jean.szabo, May 26, 2009

I try to make a pdf, with frenche texte, japanese and hebrew.

Is it possible to make it ? Actually, it's just ???????? for japanese and hebrew.

Comment by nvlashi, Jun 07, 2009

@piet.vanremortel

Can you share the code that you used to create the invisible canvas and resize the graph to fit?

Thanks!!

Comment by elenafante, Jun 11, 2009

@nvlashi

I built the chart in actionScript then I cheated to get the size that fits in the page. It kinda works.

First I created dynamically a chart + a legend and put them both into a panel.

Then the alivePDF part :

var pdf : PDF = new PDF( Orientation.LANDSCAPE, Unit.MM, Size.A4 ); pdf.setDisplayMode( Display.FULL_PAGE ); pdf.addPage(); pdf.addImage( panel.getChildAt(0), 10, 10, 215, 140, ImageFormat?.PNG, 100, 1, ResizeMode?.FIT_TO_PAGE ); pdf.save( Method.REMOTE, "http://localhost/myproject/create.php", Download.ATTACHMENT, "stuff.pdf" );

When I call the function containing that bit of code, I get the save window, and my chart is just fine, pdf, everything right.


Then I tried to add the legend and I can't get to see it. I mean, I can click the image that's supposed to contain the legend but there's nothing but a white rectangle. I added the whole panel as an image and everything goes really weird, even if I place my panel at 0,0 the chart is way too far and I can't get to see it.

Does anybody have an idea how to display a simple Legend? I promise there's nothing special on it.

Thanks!

Comment by yale.shawn, Jul 08, 2009

I am trying to get the save method to work via .net webservice rather than posting to a .cs page. Are there any examples of this at present?

Comment by Tomer.Doron, Sep 07, 2009

If you are coding for flash 10, you can avoid the whole server side interaction and simply use FileReference? to save the output on the client side.

var pdf:PDF = new PDF(); 

pdf.setDisplayMode (Display.FULL_PAGE, Layout.SINGLE_PAGE)       
pdf.addPage(); 
pdf.setFont(FontFamily.ARIAL); 
pdf.addText("test", 5, 5); 

var buffer:ByteArray = pdf.save(Method.LOCAL); 
var file:FileReference = new FileReference(); 
file.save(buffer, "test.pdf");
Comment by makanmel, Oct 05, 2009

Is there a method to convert PDF to XML? I need such client-side converter to be written in AIR.

Thank you, Max.


Sign in to add a comment
Hosted by Google Code