By PDF support I mean possibility to show and generate PDF files in Flash/AIR.
Flash and Flex can’t work with PDF files. Period. The only way to show a PDF file (without converting it to any other format) is using an HTML wrapper. For Flex there’s Flex iFrame library. You can use it to show an HTML code with embeded PDF file with installed Adobe Reader plugin (assuming that it IS installed in the system). If you are publishing to AIR you can use HTMLLoader to do the same thing:
var request:URLRequest = new URLRequest("http://www.example.com/test.pdf"); pdf = new HTMLLoader(); pdf.height = 800; pdf.width = 600; pdf.load(request); container.addChild(pdf);
Read more about it.
- PDF content does not display in a window (a NativeWindow object) that is transparent (where the
property is set to
- The display order of a PDF file operates differently than other display objects in an AIR application. Although PDF content clips correctly according to HTML display order, it will always sit on top of content in the AIR application’s display order.
- PDF content does not display in a window that is in full-screen mode (when the
property of the Stage is set to
- If certain visual properties of an HTMLLoader object that contains a PDF document are changed, the PDF document will become invisible. These properties include the
properties. Changing thse renders the PDF file invisible until the properties are reset. This is also true if you change these properties of display object containers that contain the HTMLLoader object.
- PDF content is visible only when the
property of the Stage object of the NativeWindow object containing the PDF content is set to
. When it is set to any other value, the PDF content is not visible.
- Clicking links to content within the PDF file update the scroll position of the PDF content. Clicking links to content outside the PDF file redirect the HTMLLoader object that contains the PDF (even if the target of a link is a new window).
- PDF commenting workflows do not function in AIR.
This basically means that you can’t do anything with the box where your PDF is displayed, neither animate nor control it. It’s just sitting there messing with your cool AIR app.
Convert to SWF.
More popular way is to convert PDF to SWF and load it in your application. The only open source tool I found is pdf2swf (part of swftools). Apparently it is used by every service which makes flash PDF viewers like scribd.com, safaribooksonline.com, pagegangster.com and alike. I actually found out about safaribooksonline when I was looking where to download AIR 1.5 Cookbook because there’s a chapter about PDF in AIR (which actually said nothing more that what I wrote here). Safari and PageGangster don’t secure their files but I couldn’t break Scribd data format. I know that it’s using pdf2swf because swftools home page mentions it.
This tool indeed converts PDF files to SWF. The latest version publishes to AVM2 with -T9 flag which you can use in AS3. It generates a lot of StaticText objects which of course are not TextFields but you can use TextSnapshot to work with StaticTexts (get text, select text). It does have conversion glitches, for some reason it may convert text to bitmaps instead, I can’t get how exactly it works with fonts. The latest version has a lot of bugs fixed but on my tests I failed making Flex select text with TextSnapshot. I’m waiting for a reply in mailing list.
The tool works, bad news is that you need to run it to convert PDF files to SWFs. That’s one more step in the chain.
I don’t know all possibilities of AlivePDF so I can’t really tell how well it works. But at least it seems that there’s one working library for creating PDFs in ActionScript3.
That’s a shame that PDF files can work with Flash content but ActionScript can’t work with PDF files. I’m not counting HTML wrapped PDF display in Flex/AIR. We need API at least for reading PDF content and displaying it on the screen. With ability to index and search text, add annotations and handle button events.
I was hoping that in the next Flash Player we’d get such API, but no, looks like we will have to wait longer.