How can I convert a PDF file to an image? - apache-flex

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.

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.

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

Related

Upload mutliple files using FileUpload asp.net

I have been struggling with this issue for some time now.
I have a web interface where I create projects, with this you should be able to upload multiple documents to the server, which is attached to the project they belong to.
So far I made it work with only uploading one file at a time.
My idea was maybe to add the "file" objects in an arralylist and then bind it to a gridview and then upload it to the server?
I know this sound a bit confusing, but I'm confused also!
This is how my method look like when I upload a single file to the server and also save the file's ID, name and context in the database:
File file = new File();
Projects_Overview po = new Projects_Overview();
po.Name = TextBoxName.Text;
po.Time = time;
po.Owner = TextBoxOwner.Text;
po.Responsibility = TextBoxResp.Text;
po.Created = datecreate;
po.StartDate = datestart;
po.FinishDate = datefinish;
po.ETC = dateETC;
po.Description = TextBoxDesc.Text;
po.Comments = TextBoxComments.Text;
po.Remember = TextBoxRemember.Text;
ie.Projects_Overview.AddObject(po);
ie.SaveChanges();
if (uploadFile.HasFile)
{
string filepath = uploadFile.PostedFile.FileName;
string fileName = Path.GetFileName(filepath);
file.Context = Path.GetFileNameWithoutExtension(uploadFile.PostedFile.FileName);
file.Extension = Path.GetExtension(uploadFile.PostedFile.FileName).Replace(".", "").Substring(0, 4);
file.FileName = Guid.NewGuid();
string fileSavePath = Server.MapPath("") + "\\Wordfiles\\" + Convert.ToString(file.FileName) + ".upl";
uploadFile.PostedFile.SaveAs(fileSavePath);
file.ProjectID = po.ID;
ie.File.AddObject(file);
ie.SaveChanges();
Can someone tell me how to "attach" multiple files and then save them in the folder along with the other data as I posted above?
Cheers
I believe that HTML 5 has the built-in capability to handle multi-file uploads (where HTML4 does not), but I have not worked with HTML5 yet. With HTML4, you must use an external control (typically either javascript or flash), as there is no built-in mechanism to allow multi-file uploads.
I did write a multi file upload page that works with HTML4 or HTML5 utilizing PLupload (an external control) to perform the file picking. I had to find a generic file handler to interface the control to the .aspx upload page. I think that I found that here.
It took a couple hours to implement, but it's worked great.

Scripting.FileSystemObject location (from JavaScript)

I have a file for my Windows sidebar gadget that stores some settings for the user, but the FSO object seems to pick its native residence as the desktop, meaning that if I don't specify a directory, it will put this file on the desktop. I would specify the whole location, but I want to be able to put this on other people's computers without having stuff on their desktop or elsewhere besides the gadget folder.
I know this is possible in XMLHttpRequest, but I've had trouble with that in the past, and it would be better if I could just avoid it altogether, if possible.
function writeSetting(text)
{
var fil = new ActiveXObject("Scripting.FileSystemObject");
var writer = fil.OpenTextFile("loc.txt", 2, true);
writer.WriteLine(text);
writer.Close();
}
Use the System.Gadget.path property to get the gadget's path and append to it as needed. See the example in the link.
Happy coding.

Saving a file in Flex Air

I'm trying to copy my SQLite file that is used in my Air app to user's selected directory using
var fileSaveDest:FileReference = new FileReference();
fileSaveDest.save(dbWorkedFile,'Inventory.DB');
dbWorkedFile is a File
dbWorkedFile = File.documentsDirectory.resolvePath("Inventory.db");
I tried this but the saved file isn't a valid SQLite file.
Also, I was wondering whether it's possible to embed SQLite to Air? If so how can I import and export the database?
Many thanks
In the end I couldn't get FileReference.save() to work so I go with the regular File's browseForSave()
dbWorkedFile.addEventListener(Event.SELECT, savingDatabase);
dbWorkedFile.browseForSave('Specify save location');
private function savingDatabase(event:Event):void
{
var selectedFile:File = File(event.target);
//To ensure the file is still loaded
dbWorkedFile = File.applicationStorageDirectory.resolvePath("Inventory.db");
dbWorkedFile.copyTo(selectedFile, true);
}
There is a short article describing how to include some SQLLite files in an AIR application on Adobe website (cookbooks section).

Adobe Air upload progress without FileReference

I'm deploying a small application with Adobe Air. My application will do batch upload from filepath which stored in a text file.
For example, in a text file name "list.txt", there is a string "C:\myfiles\IMG_0001.JPG". Now I want to upload this image file, keep tracking of upload progress :-<
I want to use FileReference to get the upload progress, but I don't know how to import from file's path. I also wonder how to use FileReference to upload this file without prompting a dialog for user to select file.
Thank you so much :)
Try the following. I have done a file upload without dialog box using following code.
var uploadURLs:URLRequest = new URLRequest("Your upload URL Here");
var params:URLVariables=new URLVariables();
params.title = "Hello";//URL Parameters if there is any
uploadURLs.data = params;
uploadURLs.method=URLRequestMethod.POST;
file = new File("Path to File");
file.addEventListener(ProgressEvent.PROGRESS , updateProgress);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, doneUpload);
file.addEventListener(IOErrorEvent.IO_ERROR,fileError);
file.upload(uploadURLs);
Hope this helps

Printing in Adobe AIR - Standalone PDF Generation

Is it possible to generate PDF Documents in an Adobe AIR application without resorting to a round trip web service for generating the PDF? I've looked at the initial Flex Reports on GoogleCode but it requires a round trip for generating the actual PDF.
Given that AIR is supposed to be the Desktop end for RIAs is there a way to accomplish this? I suspect I am overlooking something but my searches through the documentation don't reveal too much and given the target for AIR I can't believe that it's just something they didn't include.
There's AlivePDF, which is a PDF generation library for ActionScript that should work, it was made just for the situation you describe.
Just added a Adobe Air + Javascript + AlivePDF demo:
This demo doesn't require flex and is pretty straight forward.
http://www.drybydesign.com/2010/02/26/adobe-air-alivepdf-without-flex/
One of the other teams where I work is working on a Flex-based drawing application and they were totally surprised that AIR / Flex does not have PDF authoring built-in. They ended up rolling their own simple PDF creator based on the PDF specification.
Yes it is very easy to create PDF using AlivePDF, here is the sample code, first method create a pdf and second method save the pdf on disk and return the path, feel free to ask any question.
public function createFlexPdf() : String
{
pdf = new PDF();
pdf.setDisplayMode (Display.FULL_WIDTH,Layout.ONE_COLUMN,Mode.FIT_TO_PAGE,0.96);
pdf.setViewerPreferences(ToolBar.SHOW,MenuBar.HIDE,WindowUI.SHOW,FitWindow.RESIZED,CenterWindow.CENTERED);
pdf.addPage();
var myFontStyle:IFont = new CoreFont ( FontFamily.COURIER );
pdf.setFont(myFontStyle,10);
pdf.addText('Kamran Aslam',10,20);//String, X-Coord, Y-Coord
return savePDF();
}
private function savePDF():String
{
var fileStream:FileStream = new FileStream();
var file:File = File.createTempDirectory();
file = file.resolvePath("temp.pdf");
fileStream.open(file, FileMode.WRITE);
var bytes:ByteArray = pdf.save(Method.LOCAL);
fileStream.writeBytes(bytes);
fileStream.close();
return file.url;
}

Resources