I was doing some research in PDF exporting yesterday which gave me the opportunity to mess around with AlivePDF . This is a library was built by Thibault Imbert to enable Flex to generate PDF and is based on FPDF, a php library.
As a proof of concept i grabbed a quick drawing app to see if i could print out what ever i draw to a PDF. By the looks of the library this test only scratches the surface of its functionality, but it serves my needs at the moment.
Out of pure laziness i grabbed the simple drawing app from here http://www.munkiihouse.com/?p=4 (Thanks!) and added in the AlivePDF generation.
Draw on the canvas with your mouse, and then click Generate PDF
The first thing you need to do in order to make this work is download the latest AlivePDF SWC and add it to your Flex project. You could find that here http://code.google.com/p/alivepdf/downloads/list
Flex Code:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:mh="mh.components.*" viewSourceURL="srcview/index.html"> <mx:Style source="styles/obsidian/obsidian.css" /> <mx:Script> <![CDATA[ import org.alivepdf.pdf.PDF; import org.alivepdf.layout.Orientation; import org.alivepdf.layout.Size; import org.alivepdf.layout.Unit; import org.alivepdf.display.Display; import org.alivepdf.saving.Method; import org.alivepdf.fonts.FontFamily; import org.alivepdf.fonts.Style; import org.alivepdf.colors.RGBColor; private var myPDF:PDF; function generatePDF ( e:MouseEvent ) { // we create the PDF myPDF = new PDF( Orientation.LANDSCAPE, Unit.MM, Size.LETTER ); // we set the zoom to 100% myPDF.setDisplayMode ( Display.FULL_WIDTH ); // we add a page myPDF.addPage(); myPDF.addImage(dc_main); // to save the PDF your specificy the path to the create.php script // alivepdf takes care of the rest, if you are using AIR and want to save the PDF locally just use Method.LOCAL // and save the returned bytes on the disk through the FileStream class myPDF.save( Method.REMOTE, "http://project.unthinkmedia.com/un/createPDF/create.php", "drawing.pdf" ); } ]]> </mx:Script> <mx:Button horizontalCenter="0" click="generatePDF(event)" label="Generate PDF" id="generate_btn" /> <mx:Panel layout="absolute" title="Draw On Me" backgroundColor="#ffffff" borderThicknessBottom="10" left="20" right="20" top="20" bottom="20"> <mx:Canvas x="0" y="0" width="100%" height="100%" backgroundColor="#ffffff"> <mh:DrawableCanvas id="dc_main" backgroundColor="#ffff80" backgroundAlpha="0" width="100%" height="100%" y="0" x="0"> </mh:DrawableCanvas> </mx:Canvas> </mx:Panel> </mx:Application>
Along with the swc you will also need to add this PHP code to your server. It is supplies on the AlivePDF site, and I believe he mentioned that he has the same code in different languages if you need it. Here is the PHP code.
<?php $method = $_GET['method']; $name = $_GET['name']; if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) { // get bytearray $pdf = $GLOBALS["HTTP_RAW_POST_DATA"]; // add headers for download dialog-box header('Content-Type: application/pdf'); header('Content-Length: '.strlen($pdf)); header('Content-disposition:'.$method.'; filename="'.$name.'"'); echo $pdf; } else echo 'An error occured.'; ?>
Browse Timeline
Comments ( 33 )
[...] http://www.webresourcesdepot.com/pdf-with-flash-using-alivepdf/ http://blog.unthinkmedia.com/?p=53 [...]
Software House Rulez » Blog Archive » Flex3 - applicazioni di esempio interessanti added these pithy words on Oct 20 08 at 11:34 amExporting PDFs in Flex using AlivePDF http://blog.unthinkmedia.com/?p=53
otakurzo (Otaku RzO) added these pithy words on May 07 09 at 5:40 pm[...] PDF en Flex y, particularmente, como guardar texto en archivos de este tipo y me topé con un popular ejemplo en el que se presenta una pantalla donde es posible dibujar garabatos con el mouse para luego [...]
RIA212 » Blog Archive » Sobre AlivePDF y la clase FileReference added these pithy words on Jan 20 10 at 8:54 am[...] library, with which one can create PDFs in Flash and Flex. I searched for tutorials and found this. As you can see in this example one can download a PDF file by opening a new browser page. I wanted [...]
Flash AS3: PDF attachment with email « Flashscript.biz added these pithy words on Feb 01 10 at 2:05 amThank you so much. Worked perfectly!
Regards from Brazil,
Ved
@Dan,
Sorry it has taken me so long to get back to you, been really busy at work.I’m not too familiar with Linux, but check out this http://alivepdf.bytearray.org/?p=17 .There is a comment there that has some PERL code that may help. Let me know if that helps, if not i try my best to get an answer for you on Monday.
@Ved
No problem, it was actually quite simple, AlivePDF did all the work. I just made it do something interesting to spark some creativity from flash/flex folks.
I live in a Brazilian area, so not exactly Brazil, but at least I get some good food.
Hi Alex,
It’s really good application u have done. You have nearly solve my problem but can u please suggest me jsp code to replace your php code of create.php as I’m using java environment.
Eagerly waiting for ur reply.Regards,
Vivek
@Vivek,
I found this on the AlivePDF Forum from a user named java_rule, See if this works:
java_rule’s environment:
AlivePDF v. 0.1.4.4
Adobe Reader 8.1.2
JDK 1.5
Flex 3package javaPDF; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class CreatePDF extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int i = 0; int k = 0; int maxLength = req.getContentLength(); byte[] bytes = new byte[maxLength]; String method = req.getParameter(”method”); String name = req.getParameter(”name”); ServletInputStream si = req.getInputStream(); while (true) { k = si.read(bytes, i, maxLength); i += k; if (k == -1){ break; } } if (bytes != null) { ServletOutputStream stream = resp.getOutputStream(); resp.setContentType(”application/pdf”); resp.setContentLength(bytes.length); resp.setHeader(”Content-Disposition”,method + “;filename=” + name); stream.write(bytes); stream.flush(); stream.close(); } else { resp.setContentType(”text”); resp.getWriter().write(”bytes is null”); } } } var myPDF:PDF = new PDF ( Orientation.PORTRAIT, Unit.MM, Size.A4 ); myPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE ); myPDF.addPage(); myPDF.addImage(pie, 1, 1, 100, 100, ImageFormat.JPG , 100, 160, ResizeMode.FIT_TO_PAGE, BlendMode.DARKEN, true, “”); myPDF.save( Method.REMOTE, ‘http://localhost:8080/JSP_PDF_Generator/CreatePDF’, Download.ATTACHMENT, ‘myPDF.pdf’ );
This is great!
Are there any .NETers out there that could post an ASPX equivalent of the PHP script?
hi, do you have a page made in coldfusion?
regards,
Gustavo
do you have something for coldfusion, and also how could I convert a flex form into pdf…regards.
Gus
Can the create.php screipt be replaced by a coldfusion implementation?
The demo doesn’t seem to work. The generated pdf is saved but will not open in Acrobat or preview. I’m using Firefox 3.0.3 on Mac OSX 10.4.11
@Chris Interesting, I ran it on Firefox 3.0.3 on Mac OSX 10.5.5 and it seems to work. Out of curiosity could you try to open it using Preview and see if that works?
@Chris just realized you did try Preview. Hmmm… Have you tried any other AlivePDF examples? Let me know if that. Thanks
@phil & @ gustavo sorry i don’t have any Coldfusion examples, if i happen to run into one i will post it.
Any ideas how we could rewrite your php file onto coldfusion?
how can I save files without the create.pdf file is this really necessary?
Regards,
Gustavo
Does anyone know if Thibault Imbert or anyone else has a ruby on rails version of the server side script?
Thanks for the example!
![]()
BTW the name needs to be forth parameter,
alivePDF.save( Method.REMOTE, “PDF.php”, Download.ATTACHMENT, “drawing.pdf” );
As of Flash Player 10 you can save files locally without going to the server using the FireReference class but it takes a bit to setup. This article covers how to, Develop for Flash Player 10 in Flex Builder 3,
http://www.communitymx.com/content/article.cfm?page=1&cid=105CFThen you would target Flash Player 10 and use something like this,
var f:FileReference = new FileReference();
var bytes:ByteArray = alivePDF.save(Method.LOCAL);
f.save(bytes, “drawing.pdf”);Note: the save method is not available on players before 10.
hi alex that very good for example and if we have a word document how to export to pdf.
you have any idea?
thanks!
Great example and this is what I need..!!
How can I install alivePDF to flex?
I downloaded zip file but should i copy this folder somewhere???
I don’t have PHP install in my server, do I need it? I don’t think I can install PHP, can I use something else.
I’m using coldfusion cfc’s to extract data to flex.
Thanks
JFB
To JFB:
Copy the AlivePDF.swc in the bin folder from the zip filer folder to the libs in your Flex project. The server needs to support PHP.
I wonder how to set the page output size(Letter, Legal or 11×17 for example) and orgintation.
Hope this helps.
RubyOnRails server side script.
send_data( request.raw_post, :type => ‘application/pdf’, :filename => params[:name])
Thanks.
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.<