views:

90

answers:

2

I'm giving users the ability to upload an image to my Air app, then displaying this image in an image control. But I need to allow for PDF uploading in the same manner, so I need to convert the PDF to an image. I only care about the first page of the PDF they upload for now.

What I'm doing is: 1) User browses for a file with the file reference control 2) User chooses the image or PDF to upload 3) I encode said image to base64 and store it 4) I then load from that base64 with something like:

public function decodeImage(b64String:String):void{
  var decoder:Base64Decoder = new Base64Decoder();
  decoder.decode(b64String);
  var imgLoader:Loader = new Loader();
  imgLoader.loadBytes(decoder.toByteArray());
  imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,decodedImageLoaded);
}

private function decodedImageLoaded(event:Event):void{
  var imgLoader:Loader = (event.target as LoaderInfo).loader;
  var bmp:Bitmap = Bitmap(imgLoader.content);
  imgLoader.removeEventListener(Event.COMPLETE, decodedImageLoaded);
  var img:Image = new Image();
  img.source = bmp;
  this.addChild(img);
}

This method is working great for .gif, .jpg, and .png. At some point in my process, probably the initial upload I need to convert the first page of a PDF to a png so that I can use the rest of this process.

I welcome any ideas with the sole requirement being that it has to be a part of the Air app, I can't use something like ImageMagick that runs on a server, but I could use a component that I can compile in to the finished product.

+1  A: 

I believe AlivePDF for Flash now has capabilities to read a PDF file. You might try PurePDF, as well. You could potentially use ones of these to get that desired page and convert it to an image.

JasonMichael
I don't see anything in AlivePDF that reads/converts, PurePDF seems like it might have something in there. Will have to look back at that.
invertedSpear
According to this http://alivepdf.bytearray.org/?p=348, PDFi -- which can import existing PDF's -- is becoming part of AlivePDF. However there's no notice that this has actually happened and it's almost 2 years.
adamcodes
+1  A: 

Hi invertedSpear

Have you seen swftools? It has the ability to convert a PDF to a SWF, PNG, JPG, etc...

Danny Kopping
Maybe I'm reading the docs wrong but swftools seems to be standalone, I need something directly in my AIR app.
invertedSpear
Indeed it is standalone, but if you're using AIR 2.0 then you'll be able to interact with the command line scripts from AIR 2.0 directly. Is this going to be a possibility?
Danny Kopping
I see where you're going with that, could be a good solution, but would be a hard sell to our product managers to require a third party install.
invertedSpear
Well, it's not something that you'll need to "install" as such... You can just bundle the scripts with your application and add a note to your release notes detailing the addition (keep in mind swftools' license). To be honest, you're probably not going to be able to pull this off with AIR alone, unless you write a PDF processor yourself which is insanity for just one feature.
Danny Kopping