Posted by: edsyrett | 6 March 2009

Printing using layout and images


I needed to print an exam ready for someone to fill in.  Looking at the demos for printing in the SDK documentation, they were a bit thin on the ground. 

According to the documentation, printing is simple – you create a FlexPrintJob, create your display objects and add them to the job using addObject() and then call send().   What could be easier?

Well a lot of things actually.  

The documentation doesn’t mention a bunch of pitfalls that you need to overcome before you can print properly.  Here is a brief list of the ones I found…
  1. Images need to be downloaded before you start printing.  Image loading is inherently asynchronous, so if you request the images at print time, there is no guarantee that they will be loaded by the time printing occurs. So you have to explicitly go through your document, request each image, and wait until they are all loaded before starting to create the display objects for printing.
  2. The sample code in the documentation always seems to lay out the display objects with specific x and y coordinates.  But what happens if you want to use the standard layout components?  When I tried that, nothing appeared.  I simply tried to use VBox and HBox to position the individual parts of the page, and whatever I tried, the layout of the controls on the display objects didn’t work properly. Its seems that you have to add any display objects that you want to print to the actual display list otherwise they won’t be laid out properly, if at all.  In other words you have to add your printed page to a parent that already exists on the display list.  There might be a function somewhere that you can call to ensure that all your pages are laid out, but I couldn’t find it.
  3. In  addition to point 2, you have to allow the LayoutManager a few frames to actually perform the layout, otherwise some containers seemed to be laid out properly, but others didn’t.  In my test app, I create the controls and then the printing is done at the other end of a callLater(). 

My working test app is here.  The printed layout is pretty much the same as the displayed layout, and it only works because I have solved the particular problems described above.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: