Except for extracting the data for further processing, it is also typical that you would want to create reports of the data. In this case, PDF is one of the most popular formats, because it looks virtually identical on every system.
Generating PDF files is a huge topic, so we can't show everything, but this recipe should get you started.
We will use the purePDF library to create the PDF file. It can be found at http://code.google.com/p/purepdf/. Download the library through the download link. At the time of this writing, purePDF_0.77.20110116.zip
was the current version.
The library comes packaged as an SWC class library. To use these in FlashDevelop, move both files into the lib
folder of your project. Inside FlashDevelop, right-click on the files and choose Add to Library. Their filenames should turn blue in the project view.
Now create a Recipe8
document class and copy the same starting code from the previous recipe.
Add the export functionality as follows:
Recipe8
constructor:var loadText:TextField = new TextField(); loadText.text = "click here to save to file"; loadText.autoSize = TextFieldAutoSize.LEFT; loadText.addEventListener(MouseEvent.CLICK, onClick); addChild(loadText);
FontsResourceFactory.getInstance().registerFont( BaseFont.HELVETICA, new BuiltinFonts.HELVETICA() );
onClick
method, where the bulk of the work is done:private function onClick(event:MouseEvent):void { saveFile = new FileReference(); saveFile.addEventListener(Event.COMPLETE, onComplete); var pdf:ByteArray = new ByteArray(); var writer:PdfWriter = PdfWriter.create(pdf, PageSize.A4); var document:PdfDocument = writer.pdfDocument; document.addTitle("Data file"); document.open(); var table:PdfPTable = new PdfPTable(2); table.addStringCell("x"); table.addStringCell("y"); for (var i:int = 0; i < data.length; i++) { for (var j:int = 0; j < data[i].length; j++) { table.addStringCell(data[i][j]); } } document.add(table); document.close(); saveFile.save(pdf, "data.pdf"); }
onComplete
method to signal the user that the file was stored:private function onComplete(event:Event):void { trace("file was saved"); }
The save
method, from the FileReference
class, has the same limitations as the load
method we saw in a previous recipe. You can only call it after an action from a user, which is why we connect it to clicking on a text field.
Using fonts in PDF files is fairly complicated, although if you know how they are dealt with in Flash, you've already got a good basis. In general, you can use a built-in font (which we do in this recipe) or you can embed your own font. In any case, these fonts must first be registered before they can be used.
More information on using fonts can be found on the purePDF wiki at http://code.google.com/p/purepdf/wiki/FontsHowTo.
The PDF writing we did in this recipe was close to the minimal possible to generate a well-formed PDF. Creating a PDF consists of two parts:
The main difference between this recipe and the previous one is that we don't need to bother ourselves with the details of the PDF format. In the CSV recipe, we needed to worry about placing commas or semicolons and new lines at the right places. In this recipe, the purePDF library takes care of all of that for us and we can describe the PDF through the usage of the classes in the library.
Generating PDF files is a huge topic and can easily cover an entire book. Therefore this recipe was only a minimal primer. Now we'll look at a few more things you can do.
Once you get to understand how PDF works, it is surprisingly easy to create nicely formatted headers and footers. It's also possible to add page numbers should you have multiple pages in your document.
See for instance this example to get started:
http://code.google.com/p/purepdf/source/browse/examples/src/HeaderFooter1.as.
You can add several types of images to spice up your PDF. In fact, you could extract the bitmap data from the graph we drew with the ActionScript application and put that next to the data points.
This example is a good start: http://code.google.com/p/purepdf/source/browse/examples/src/ImageBitmapData.as.
To obtain the bitmap data, you can draw the graph to a BitmapData
object:
PurePDF is a port of the popular iText PDF library (see http://itextpdf.com/). This open source library is available for Java and C#. And while some things have been changed to fit ActionScript, almost all of the documentation for iText will also be applicable to purePDF.
The book iText in Action printed by Manning and written by Bruno Lowagie, the iText creator, is a very thorough guide to the iText library and is the perfect companion if you want to continue creating PDF files.
18.116.36.192