Tag Archive for 'Acrobat'

Current state with PDF support in Flash

By PDF support I mean possibility to show and generate PDF files in Flash/AIR.

Show.

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.
Once again this method relies on Adobe Reader plugin installed. But here you at least can check if the plugin is installed or not using HTMLLoader.pdfCapability. Though there’s a bug, if you install Adobe Reader plugin and don’t agree with Terms of Use pdfCapability shows OK but you will not be able to see your PDF document. What’s more this method comes with a huge list of limitations (quoted from the link above):

  • PDF content does not display in a window (a NativeWindow object) that is transparent (where the transparent property is set to true).
  • 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 displayState property of the Stage is set to StageDisplayState.FULL_SCREEN or StageDisplayState.FULL_SCREEN_INTERACTIVE).
  • 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 filters, alpha, rotation, and scaling 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 scaleMode property of the Stage object of the NativeWindow object containing the PDF content is set to StageScaleMode.NO_SCALE. 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.

But using this method you can communicate with a PDF file provided that you compile specific JavaScript in it using Adobe Acrobat JavaScript capabilities (doh?). Read further to find out how it can be done without Acrobat. I don’t know what JavaScript code inside PDF file can do, even if it has total control over the plugin this method sucks.

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.

Generate.

The most popular library for generating PDFs from ActionScript 3 is definitely AlivePDF. The project is active and many enthusiasts develop different extensions: templates, JavaScript injection (this is how you probably can inject JavaScript to any PDF file without Adobe Acrobat and use it to communicate with that file). The latest news is plans for using Alchemy to speed up PDF creation. There are a couple more projects on Google Code: PDFCase — a port of Java PDFJet and as3-pdfreader — a Chinese guy tried to port Jave PDFBox but gave up in the middle. So, we are left with the only solution — AlivePDF.

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.

Final words.

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.