Printing in AIR 2

I recently had the opportunity to modify an application that Rick Rocheleau wrote to demonstrate the printing improvements in AIR 2. With the AIR 2 Release Candidate publicly available, it seems like a good time to let developers experiment with the new and improved APIs. I’m releasing the source code for a version of Rick’s app that I modified to use Flex 4, which also includes some helpful comments for developers.

Here’s a screencast that summarizes the application and some of the AIR printing APIs:

What I demonstrate in the video–and what you can try for yourself by downloading the code and/or AIR file–is a Flex 4 (Spark) group that lays out text in a printable area; this area is constrained by the paper selection, and will print to any attached printer exactly as it appears on the screen. The printing APIs in AIR 2 let you hand it any sprite, which will get passed along to the system in either vector (PrintMethod.VECTOR) or bitmap (PrintMethod.BITMAP) modes. AIR 2 has increased performance for both these modes, and also adds vector printing support on Mac. There’s also a new printing mode called auto (PrintMethod.AUTO) that will leave the mode decision up to the runtime; this has the advantage of not requiring you to guess which mode is best for printing the user’s content. In auto mode, the runtime will choose between vector or bitmap mode for performance and speed. In this application the appearance of a translucent watermark on the content will cause the runtime to choose bitmap mode, and its absence will result in it choosing vector mode.

Another notable improvement is the ability to get a list of printers from the system without having to start a PrintJob first; you can use this information to create a drop down box of choices for the user, and set which printer should be used for future jobs. You can also set the paper size, change orientation, receive information about the printable area based on your paper selection, and gain more control over the printing UI. You can choose to display the page setup dialog (without needing to start a PrintJob), and you can also choose to print by displaying the native system UI or suppress the native UI and send the job directly to the printer. The latter of those two options is useful if you want your application to control all aspects of the page/printer setup, and/or if you want to streamline the printing process for the user.

I encourage you to watch the screencast to learn more, and to check out the source code and application:

Source code: PrintFeatureDemo.zip

AIR file: PrintFeatureDemo.air

You’ll need a version of AIR 2 to both compile and run this print demo. You can use the publicly available AIR 2 Release Candidate:

AIR 2 Release Candidate on Adobe Labs: http://labs.adobe.com/technologies/air2/

AIR 2 will give you more control and performance with printing; AIR 2 is not yet a final release, and this printing app is just a demo. If you believe you’ve found bugs in either, don’t hesitate to report them here. I’ll make sure they’re addressed. I’m also eager to hear about your ideas or improvements for the printing app.

Update 1: I uploaded new versions of the AIR file and source code, which should fix a problem with running or compiling the code using AIR 2 Beta 2.

Update 2: The documentation for AIR 2 Beta 2 is available through Adobe Labs, but here’s the direct link to the Beta ActionScript 3 docs: http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/index.html

Update 3: We just released an AIR 2 Release Candidate (May 11th, 2010), and I’ve updated the post to point you to this version. I’ve also updated the AIR file and code, so that it’ll work with AIR 2 RC (and not AIR 2 Beta 2).

29 Responses to Printing in AIR 2

  1. dan says:

    I get the error when I try to install the example air app.—————————–“Sorry, an error has occured.”This application requires an update to Adobe AIR that is not available for your system.Please view the system requirements for Adobe AIR and update your system accordingly.I installed: air2_b2_runtime_win_020210.exe on a Vista 64 system.—————————–My other question is how can you print directly to the printer and send specific “codes” or “printer language sequences” to the device. I am referring to barcode printers like Zebra printers.

  2. Daniel Koestler says:

    Thanks for the tip. I’ve updated the AIR file and code, and it should work now.Looking through the docs (a good link, which I’ll include in the post), I haven’t come across any way to send raw commands to a printer. The APIs abstract the interface between the printer drivers and the application, so that you’ll have to use the options available based on that driver. I don’t have any experience with Zebra printers, though–so if you get some useful tips, please let me know.If there’s some functionality missing that you need for controlling your printers, that would make for a good feature request–or perhaps there’s some workaround.

  3. dan says:

    Thanks the update worked.If I find a method to send printer control codes or customs sequences I’ll pass them along.

  4. jaya says:

    Thanks I was looking 4 such a sample..Here is the problem I faceI was able to install and the appln launches, but when I click print, data is sent to printer and the application terminates saying “PrintFeatureDemo.exe has encountered a problem and need to close. v r sorry for inconvenience”.My System do meet minimum requirements for Air 2.0 rc1.. still Im facing this issue..Any idea what could be the reason?Thanks Again! ๐Ÿ™‚

  5. Daniel Koestler says:

    That sounds like a possible bug, Jaya. Can you tell me more about your system setup? I’ll also send you an email.

  6. Dev says:

    dead link, cannot download source file

  7. David Doull says:

    I tried this out and no matter what options I choose the watermark never prints, including forcing bitmap printing (printing on XP)… a air printing bug or just something with this demo app?

    • dkoestle says:

      Hey David,

      Can you tell me more information about your setup, such as whether it’s 32 or 64 bit XP and the make and model of your printer? It could be a bug in AIR, as printing with the watermark works on other systems.

  8. kocu says:

    Hi, sorry I can not download your link, I only get 0 bytes file.

    I’ve already tried downloading the official one from adobe, the application run fine. But when I open the source file, I can not compile it since it has some errors:

    – PrintJob.printers are not recognize (undefined property)
    – PrintUIOptions type was not found
    – printJob.printer also not recognize (undefined preperty)

    do you have any idea why? please help ๐Ÿ™

    • dkoestle says:

      Hi kocu,

      Which file are you having trouble downloading? I was able to get both the AIR file and the source ZIP from my blog post. I’m also able to compile the PrintFeatureDemo app from the source that I provide here.

  9. kocu says:

    hi dkoestle, finally found the main problem, I didn’t realise that AIR SDK 2.0 is not included before Flex SDK 4.1 and mine was still using Flex SDK 4.0, I downloaded the update today and the code works like a charm! Many thanks ๐Ÿ™‚ Cheers

  10. kocu says:

    ah, sorry forgot to answer your question. Yesterday I tried to download the source file and receive only 0 KB zip file, but today I redownloaded it successfully, perhaps it was connection issue, thx.

  11. Cristian says:

    Dear dkoestle

    I’m developing an AIR application with Flex Builder 3, and I need print without the print dialog, So I was trying to use the AIR SDK 2 in order to use the PrintUIOptions class, and everything goes ok..
    but when I run the app I have the following error

    “VerifyError: Error #1014: Class flash.printing::PrintUIOptions could not be found.”

    but when I imported the class the flex didnt tell something wrong about it.

    Thanks

    • Sammey says:

      Hey Cristian,

      I see this is a very old message, but for the ones who land here just like me:
      I had the PrintUIOptions error as well, but then I noticed my Air version was set to 1.5 ๐Ÿ˜›
      Just set it correctly like this :

      Inside your application.xml

  12. Dhruv Chauhan says:

    How can I read a PDF & put the content in this text area and then can print it? Is this possible to do?

    Please help me in this
    Thanks
    Dhruv

  13. Brollie says:

    Hi I tried out the code but seem to have difficulty with specifing other fonts to print in the CDATA, Although I inserted ” Properly-formed HTML

    p {font-family: fantasy, cursive, Serif;}

    CSS font-family


    HTML font code is done using CSS.

    I dont see it applied in the final print. Can I assume CDATA does mean that I can use normal HTML ?

  14. Brollie says:

    Never mind, Windows 7 hides these font’s if you locale does not use the language. You have to change windows 7 settings

  15. Erik says:

    Somehow there seems to be going something “wrong” with spooling pages on the printer-queue.
    I try to call the PrintJob more then once (although instantiating a new one) each time when I call it. But my system somehow only prints one. The rest is spooled, and in the printerqueue as supposed to be, but the printer doesn’t print more then once.

    By the way are the properties PrintJob.isSupported (as shown in the docs) not available?
    Seems to be a buggy API

    Any ideas on the printerqueue?

    Kind Regards,

    Erik

  16. Mark says:

    Thanks so much for making the demo available for download !

  17. Joรฃo Dias de Carvalho Neto says:

    Hi

    I’m so gratefull! Your code help me so much!

    I’m still in one small doubt and I will be gratefull if you could help me.

    Can I manage Air configurations? I see a increase on my disk at each print (in order of 1.5 2.0 Mb) My Application needs to print every time and at a whole day printing 200 Mb will be used on disk.

    I try to use the flash.system.System.gc() after set my printJob variable to null but it fails; (My intent was to deallocate memory to see the possiblity of be deallocate disk space)

    Do you know how can I dealocate this extra space on my disk at application runtime?

  18. Ben Cessa says:

    Hi, I’m tryng to print some HTML content without displaying it on the screen but so far is not working, this is the code I’m using: http://pastebin.com/Lf0b5CCF

    I’m not getting any sort of error, just a blank screen, I already try with on-screen elements and it do work, any ideas on how can I achieve this?

  19. Robert Caldwell says:

    I am interested to know whether we can also set other settings for the print driverโ€”for instance, duplex printing or tray selection?

  20. Joshua Cane says:

    Thank you for this demo. I am able to successfully print one page, however, how do you print all the content of the Emma excerpt? It seems that only what is visible on the screen will print, but how can you adjust the code to print all the content?
    Thank you in advance.

  21. I’m experiencing a very unusual error. When I connect to the b&w printers on my LAN everything prints fine. When I choose color printers the file spools and the size continues to increase until the application crashes. Any advice on what may be going on and how to fix it?

  22. hix says:

    This tutorial would be very usefull in case a book would have only one page :)) The same goes for the print ability in air … useless in case you need to print more text, like results from a database query

  23. Anand Kumar says:

    I want to know whether printing is supported in AIR 2.7 or AIR 3.0 Beta on devices like iPad.

    Please let me know any solution/workaround to make print functional on iOS device.

    • dkoestle says:

      I looked into this, and we currently don’t plan on supporting printing on iOS for those releases. Sorry. Out of curiosity, what application are you trying to create?

  24. Anand Kumar says:

    Thanks for your quick response Daniel.

    I am trying to create some sample applications to check the capability and rendering of the application created using flash builder 4.5 on devices like iPad/android.

    Thanks again! ๐Ÿ™‚