|
APINotes
Some notes about different parts of the API, as found from users.
IntroductionI'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 PositionThere'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 TextAdding 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"); ExampleThe 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 applicationpublic 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 AIRThis 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
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
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
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?..?
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.
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.
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 ?
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?
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">
ResizeMode?.FIT_TO_PAGE ); </mx:Application>Need a quick solution...please help!!
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
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
@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
@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.
@piet.vanremortel hi .. could you pls share the code for this? I am kinda stuck becos of the very same problem.
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.
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!
Also, I can't seem to use variables in the .setFont method. is greatly holding back development at the moment.?
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.
@piet.vanremortel
Can you share the code that you used to create the invisible canvas and resize the graph to fit?
Thanks!!
@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!
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?
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");Notes for successfully sizing and implementing a background watermark image for all pages on your PDF including wrapped pages. This was something that took a bit of fiddling to get right so hopefully someone else will find it useful.
Creating and sizing the background image:
I tried to use a PNG file but ran into a load of Alpha channel not supported errors. Instead of looking for the correct PNG settings I opted for a JPG instead. Sizing your image to A4 specs @ 300dpi will result in an image 4 times the rendered page size. Create your background image 638px x 853px @ 300dpi for an A4 full page background.
Adding the watermark to every page:
Embed your image
Add event listener
Create the event handler
private function pageAdded ( pEvt:PageEvent ):void { trace( pEvt.page ); // Set margins __doc.setBottomMargin ( 25 ); __doc.setLeftMargin( 20 ); __doc.setRightMargin( 20 ); __doc.setTopMargin( 20 ); // add the background image __doc.addImageStream( new logoLargeBytes() as ByteArray ); }Hours of testing and stuffing around to get that working for my document creation class, mind you I'm new to ActionScript?... Hope it helps others. :)
Is there a method to convert PDF to XML? I need such client-side converter to be written in AIR.
Thank you, Max.