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.
- 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.
- 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.
- 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.