purePDF moved to github

As you may have noticed I’m not working with flash and actionscript anymore. However I still receive some requests regarding the purePDF library I published, well I don’t remember when exactly 🙂
By the way, I decided to move the code to github, thus it will be easier for anyone still interested in that library to fork and add modifications and share to other people.

Here’s the link to the project: https://github.com/sephiroth74/purePDF

Multipage TIF to PDF

Recently I’ve continued the porting of iText adding the support for a new image type: multipage tif.

The only problem with this update is that actually it’s really slow and probably with very complex image files it can hang the player for too long. Probably I should modify the ImageElement.getInstance method and make it asynchronous in order to prevent flash player to freeze…

Anyway, the current trunk version of purePDF can handle TIF image and here a simple code example:


package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.utils.getQualifiedClassName;

import org.purepdf.elements.RectangleElement;
import org.purepdf.elements.images.ImageElement;
import org.purepdf.io.RandomAccessFileOrArray;
import org.purepdf.pdf.PageSize;
import org.purepdf.pdf.PdfDocument;
import org.purepdf.pdf.PdfViewPreferences;
import org.purepdf.pdf.PdfWriter;
import org.purepdf.pdf.codec.TiffImage;

public class TestTIF extends Sprite
{
[Embed( source="assets/foxdog_multiplepages.tif", mimeType="application/octet-stream" )]
private var cls1: Class;

private var document: PdfDocument;
private var writer: PdfWriter;
private var buffer: ByteArray;
private var filename: String;
private var index: int;
private var pages: int;
private var stream: RandomAccessFileOrArray;

public function TestTIF()
{
super();
addEventListener( Event.ADDED_TO_STAGE, onAdded );
}

private function onAdded( event: Event ): void
{
stage.addEventListener( MouseEvent.CLICK, onClick );
}

private function createDocument( subject: String = null, rect: RectangleElement = null ): void
{
buffer = new ByteArray();

if ( rect == null )
rect = PageSize.A4;
writer = PdfWriter.create( buffer, rect );
document = writer.pdfDocument;
document.addTitle( getQualifiedClassName( this ) );

if ( subject )
document.addSubject( subject );
document.setViewerPreferences( PdfViewPreferences.FitWindow );
}

private function onClick( event: Event ): void
{
filename = getQualifiedClassName( this ).split( "::" ).pop() + ".pdf";
var byte: ByteArray = new cls1();
stream = new RandomAccessFileOrArray( byte );
var image: ImageElement = ImageElement.getInstance( byte );
createDocument( "Multi page TIFF Image Example", PageSize.A4 );
document.open();
// add the first page to the document
document.add( image );
// get the total number of pages
pages = TiffImage.getNumberOfPages( stream );
trace("number of pages: " + pages );
// next page index to add to document (first page is 1)
index = 2;
var timer: Timer = new Timer( 100, 1 );
timer.addEventListener( TimerEvent.TIMER, onTimerComplete );
timer.start();
}

private function onComplete(): void
{
stream.close();
document.close();
save();
}

private function onTimerComplete( event: TimerEvent ): void
{
if ( index > pages )
{
onComplete();
return;
}
document.add( TiffImage.getTiffImage( stream, index ) );
index++;
Timer( event.target ).reset();
Timer( event.target ).start();
}

private function save( e: * = null ): void
{
var f: FileReference = new FileReference();
f.save( buffer, filename );
}
}
}


Here you can see the result PDF file.
And here the input TIF image.

purePDF and transparent BitmapData

Recently I’ve updated purePDF to a new version fixing one bug which caused problems with PNG images.
However when you’re trying to add BitmapData with transparency  to a pdf document you probably get black backgrounds to your images. This is because internaly purePDF converts bitmapdata into 24bit tiff images, so no alpha informations.

This is a script you can use to convert your BitmapData images into transparent pdf ImageElements:


/**
* Create a transparent ImageElement
*
* An ImageElement with the input bitmapdata RGB informations will be
* created and an ImageElement will be used as mask ( using the alpha info from the bitmapdata )
* If the input bitmapdata is not transparent a regular ImageElement will be returned.
*/
protected function createTransparentImageElement( bitmap: BitmapData ): ImageElement
{
var output: ByteArray = new ByteArray();
var transparency: ByteArray = new ByteArray();
var input: ByteArray = bitmap.getPixels( bitmap.rect );
input.position = 0;

while( input.bytesAvailable ){
const pixel: uint = input.readInt();

// write the RGB informations
output.writeByte( (pixel >> 16) & 0xff );
output.writeByte( (pixel >> 8) & 0xff );
output.writeByte( (pixel >> 0) & 0xff );

// write the alpha informations
transparency.writeByte( (pixel >> 24) & 0xff );
}

output.position = 0;
transparency.position = 0;

var mask: ImageElement = ImageElement.getRawInstance( bitmap.width, bitmap.height, 1, 8, transparency, null );
var image: ImageElement = ImageElement.getRawInstance( bitmap.width, bitmap.height, 3, 8, output, null );

if( bitmap.transparent )
{
mask.makeMask();
image.imageMask = mask;
}
return image;
}

and here there’s a demo app: http://bit.ly/dOT2ob

Flash alpha GradientMatrix to PDF using purePDF

One of the most hard thing to traslate when creating pdf documents from existing flash movies is indeed the gradient matrix.

Not only because pdf and flash have 2 different coordinates system and because the gradient matrix is applied in 2 different ways, but also if you want to convert gradient with transparent colors inside.
Let me say that I’ve encountered the same issue Mario posted here, even if I made a little modification to his solution: GradientMatrix.as
This is the swf example. Click on the sprite to start the animation. It will rotate and translate both the sprite and its gradient matrix, then click again to stop the animation and create the pdf file at that frame.
SWF HERE
In order to create the correct gradient matrix with the right position and rotation in purePDF I’ve used the PdfShading.complexAxial static method in this way:
var cb_shading: PdfShading = PdfShading.complexAxial( writer, top_left.x, top_left.y, top_right.x, top_right.y, cb_colors, cb_ratios, true, true );

then for the alpha masking I’ve created a PdfTransparencyGroup applied to the alpha mask.

You can see my solution (which probably is not the best one, but it’s the one I discovered for now) in the code below.
[kml_flashembed movie=”/wp-content/uploads/2010/02/ExampleColorGradient2.swf” height=”300″ width=”400″ /]

Continue reading

purePDF, a complete actionscript PDF library

PurePDF is a complete library for creating PDF documents using actionscript 3 ( targeted for flash player 10 ).
The project is actually hosted on google code.

This is is a porting I’ve made of the famous java iText library ( version 4.2 ) by Bruno Lowagie into pure actionscript 3.0 (with some little modifications).
iText ( and purePDF ) has tons of features for create and manipulate pdf documents. A quick list of the features actually implemented into purepdf:

  • pdf viewers display options
  • alpha transparency, blend modes
  • layers
  • arabic RTL writing
  • support for pdf text rendering ( example )
  • tables ( nested tables, page split tables, table with images, etc…)
  • slide show ( page transitions )
  • annotations, comments, file annotations
  • patterns, shadings patterns (linear and gradient), spot colors, rgb color and cmyk color
  • linear and radial gradients with alpha ( example or example 2 )
  • forms (user input forms, textfields, combo box, list, checkbox)
  • paragraphs, phrases, chunks for text manipulation, chapters, lists…
  • images (jpeg, png, animated gif, tif, bitmapdata ) and image patterns
  • basic and advanced paths
  • afm, otf, pfm, ttc and ttf fonts (embedded and not embedded)
  • metadata, page header and footers
  • external, internal links
  • barcodes creation ( ean-ucc 13, ucc-12, ean-ucc-8, upc-e, pdf 417, ean supplements) ( example )
  • unicode, cjk fonts and text
  • file attachments
  • javascript ( example )
  • multi column text
  • Embedded movies ( example )
  • Vertical text ( see this example )
  • and many other features…

You can take a look at this PDF document (self generated using actionscript reflection) with the purePDF APIs.

Other actionscript libraries I’ve used for this project are: fzlib, as3corelib, ashashmap, alchemy, as3-commons.

You can both download the library from source code and compile it yourself ( see at the Installation guide ) or download the precompiled swc files from the project’s download section.

Moreover there are actually a lot of examples to explain all the library features and for a quick guide. You can find them listed and updated here. In General refers to the project wiki for all the updated examples/howtos and the project updates.

update: here you can find the updated online api documentation