AIR 3 PDF Renderer extension (Mac OSX only)

October 2nd, 2011
by admin

There’s this new feature in AIR 3 – native extensions, and there’s that ever-existing problem, how to render PDF documents in flash. In web player that’s still not possible (well, unless you sacrifice one year for writing PDF renderer) but it’s possible in AIR, for example using native extensions.
MacOS-X has the Cocoa library(framework) which performs great in rendering PDF files so I’ve written an extension which uses this lib to send BitmapData object containing rendered PDF to flash. Of course this is possible in windows and linux too, using XPDF library for example. AIR native extensions are easily extendable and altering several things in files that I’ve provided should make that extension work on windows, linux and even mobile systems.


RenderPDF extension contents:
There are many files in the archive, but all of them were used to make an extension.
PDFExt_framework contains compiled OS-X framework.

PDFExt_XCode contains an objective-c (objective-c is needed for Cocoa library) framework project.

PDFExtTest is a test app using an extension. In sample_commands folder you have bash commands to compile and package the test app. Test app displays a pdf, allows page changing and saving current page as PNG image. Test app illustrates usage of RenderPDF extension methods:
get pageCount():int,
RenderPDF.renderPage( pageNum, dpi ):BitmapData ,
loadPDF( source:String ):Boolean

PDFExtTest app screenshot:

PDFExtAS is the source code for AS3 interface for an extension.
compiled – this folder contains compiled swc, extension, extension xml descriptor and sample commands for compiling and packagind everything into an .ane file.


Several remarks:
Before compiling the framework, you must add the Cocoa framework and Air framework to the project, and then exclude them from linking, as described on “Extending AIR” article. In Info.plist file you must add the DYLD_FORCE_FLAT_NAMESPACE env variable and in project settings you modify the “Runpath search path”.

If you’ll take a look into PDFExtM.m source file, you will see that returning a BitmapData object from native function involves quite a few steps.
Also, flash BitmapData sometimes contains some extra bytes at every scanline, so memcpy from native uint array to copy the bitmap won’t work, you must iterate every pixel and skip the extra bytes at the end of a scanline (maybe there’s some super clever low-level-memory-hacking method to do it faster).
RenderPDF+sources

Tags: , , ,
Posted in flash experiments | Comments (9)

9 Responses to “AIR 3 PDF Renderer extension (Mac OSX only)”

  1. Mackey Says:

    Awesome… I was looking at what it takes to render a PDF with ANE. I can easily do this. One question… Can flex some how render a metafile… I can use my windows PDF lib to render metafiles… On windows I can open this metafile in a metafile viewer and actually search on the text in the rendered metafile viewer… Can this be done in air 3?

  2. admin Says:

    Huh, you mean “Windows metafile” ? To view it, on *nix systems this could be problematic, I don’t recall any lib that supports this filetype, but in windows you could do similar ANE using some dll for emf files. Alternatively you can use ImageMagick and the flash.desktop.NativeProcess class to generate a bitmap from emf, and to search for a text inside it, you could use some emf2txt app.

  3. Aaron Simmons Says:

    Where did you read that we have to use DYLD_FORCE_FLAT_NAMESPACE, “Runpath search path”, etc? I can’t seem to find that in any docs.

  4. admin Says:

    It’s in “Extending Adobe AIR” article by Olivier Goldman (http://www.adobe.com/devnet/air/articles/extending-air.html)

    When compiling Mac OS X frameworks for use as an extension, developers should set the following options to support correct resolution of the dependency on the AIR framework in all scenarios:

    Add @executable_path/../runtimes/air/mac, @executable_path/../Frameworks, and /Library/Frameworks to LD_RUNPATH_SEARCH_PATHS.
    Use weak framework linking and the flat namespace option.

  5. Luca_V Says:

    Hi do you think it’s possibile to have it workin on IOS?

    Thanks,
    Luke

  6. admin Says:

    Yup. Maybe some changes are needed, but the most important thing – Quartz – is in iOS too, so the native PDF support requirement is fulfulled.

  7. Luca_V Says:

    I tired to import the ANE in a Fkex Mobile project, but the IDE says that the ANE doesn’t support IOS, some suggestion where to change this behavior?

  8. admin Says:

    You should recompile native module for iOS, change the extension.xml file and package using adt with commands specific to iOS packaging.
    The example projects and other documentation (Oliver Sachs article ) is available on adobe site

  9. Luca_V Says:

    doh I’m wasting my time without results…can you provide the zip also for IOS? I really need it and i can support this devlopement with some money!

Leave a Reply