'Flex printing breakthrough! And more!'에 해당되는 글 1건

  1. 2013.07.15 Flex printing breakthrough! And more!
반응형

Flex printing breakthrough! And more!

Hi guys. It’s occurred to me that this blog is wandering off topic. It’s not cheap pr0n, nor is it really supposed to be about feelings and girly rubbish… but sober, hard core technical discussions. Yes, I know cinnkitty finds those a turn on, but she also likes footy and beer.

Today is all about printing in Flex and AIR. While there is a fair amount of information out there about printing in Flex/AIR, the fact remains that at the end of the day, it’s not really designed for printing. The output is, I’ve found, unreliable and  fuzzy, essentially an image of the print area (sometimes with the scroll bars showing!), rather than a true print.

An example is this:

// AS3 code:

public textArea1:TextArea = new TextArea();

public function printText(e:Event = null):void{
trace(‘printing Plain Text’)
textArea1.htmlText = stringToPrint;//this is where the string to print goes                                                                                     textArea1.width = 800;
textArea1.height = 1000;
textArea1.visible = false;
var timer : Timer = new Timer( 1000, 1 );
timer.addEventListener(TimerEvent.TIMER, function():void{
try{
var PrintJob:FlexPrintJob = new FlexPrintJob;
PrintJob.start()
PrintJob.printAsBitmap=false;
PrintJob.addObject(Browser, FlexPrintJobScaleType.SHOW_ALL)
PrintJob.send()
PrintJob = null
Browser.height = 0;
Browser.width = 0;
}catch(e:Error){
//
}
} );
timer.start();
}

//end AS3 code

which  is fine for simple text, up to a point can even parse html a bit. Images? think again… More than a few pages? You’re joking, right?

The answer is actually on it’s way, in the form of http://www.alivepdf.org/

This guy has figured out a way of  constructing a PDF from scratch, and if you’re tricky, you can use AIR’s new PDF capabilities (Warning! You will need the latest version of Acrobat, and only the latest version of Acrobat will work! Too bad if, like me you prefer something lighter, like foxit) to construct what you want to print, save it to a temp file, then bring it up in a PDF control in AIR.

The PDF  control, being essentially Acrobat, takes care of printing, previewing, and even saving a copy to disk if your target audience don’t want to print (wasting paper and all).

A brief introduction to PDF in AIR can be found at http://agknology.com/blog/http:/agknology.com/blog/air/29 

But wait, there’s more!

If you think about it, you can do lots of things in PDF. Now that we can generate them dynamically (sort of, but there’s promises of more to come) some implications  are:

- Freedom from FLV as teh only video format (PDFs can handle WMV)

- E-book applications

- lightweight, customised PDF deployment (as the PDF’s can be generated procedurally using a few lines of (compressible) text/code, rather than the actual PDF itself being stored)

A note on the last thing- Thibault Imbert, the author, has also had a crack at a Zip class for AS3. Interesting…

A sample PDF generation application is as follows:

<?xml version=”1.0″ encoding=”utf-8″?>
< mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute” applicationComplete=”init()”>
< mx:Script>
< ![CDATA[
import org.alivepdf.fonts.Style;
import org.alivepdf.layout.Layout;
import org.alivepdf.pdf.PDF;
import org.alivepdf.*
import org.alivepdf.saving.Method;
import mx.controls.Alert;
import mx.events.CloseEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

private var display:HTMLControl;
private    var someText:String = 'Hello World';
private var actualPDF:ByteArray = new ByteArray();
private var pdf:PDF = new PDF();
//    private var page1:org.alivepdf.
private function init():void{

pdf.addPage();
}

private function showClicked(e:Event = null):void{

pdf.setFont('Arial','',10);

pdf.writeText(5, someText);
var size:int = 20;
pdf.setFontSize(size);
pdf.writeText(size, someText);
size= 5;
pdf.setFontSize(size);
pdf.writeText(size, someText);
//    pdf.writeText(size, pdf.setFontSize(10)'Hello');;
//pdf.addMultiCell(100,50,someText,1);
trace('about to save pdf');
actualPDF = pdf.savePDF(Method.LOCAL);
//trace('text size is'+pdf
//pdf.addEventListener(Event.COMPLETE, saveIt);
}

private function saveIt(e:Event = null):void{
var file:File = File.desktopDirectory;
file = file.resolvePath('temp.pdf' );
var stream:FileStream = new FileStream();;
stream.open(file, FileMode.WRITE);
stream.writeBytes( actualPDF);
stream.close();
trace('Saved file');
}

public var stringToDisplay:String = new String();

private var desktopDir:File = File.desktopDirectory;
public function openString(e:Event = null):void{

try {
desktopDir.browseForOpen('Pick a file');
desktopDir.addEventListener(Event.SELECT, openFile);
}catch(error:Error){
Alert.show('There was an error writing a file', "Error", Alert.OK )
}
}

private function openFile(e:Event = null):void{
//trace('Storage/openFile is opening file:'+path)

var file:File = e.target as File;
if (file.exists){
var stream:FileStream = new FileStream()
stream.open(file, FileMode.READ);
someText = stream.readUTFBytes(stream.bytesAvailable);
stream.close()
}else{
Alert.show("There was an error reading a file", "Error");

}

}

]]>

</mx:Script>
< mx:Button x=”111″ y=”105″ label=”make pdf” click=”showClicked()”/>
< mx:Button x=”111″ y=”151″ label=”save it” click=”saveIt();”/>
< mx:Button x=”110″ y=”57″ label=”open text” click=”openString()”/>
< /mx:WindowedApplication>

have fun! and I’ll get back to matters of the flesh, soon enough…

Posted by 1010