Monday, April 11, 2011

Rendering PDFs in JavaScript...?

I released Emscripten 1.0 over the weekend, which came with a demo of rendering PDFs entirely in JavaScript (warning: >12MB will be downloaded for that page). Emscripten is an LLVM-to-JavaScript compiler which allows running code written in C or C++ on the web. In the linked demo, Poppler and FreeType were compiled to JavaScript from C++.

The goal of the demo was to show Emscripten's capabilities. Over the last year it has gotten very usable, and can probably compile most reasonable C/C++ codebases (albeit with some manual intervention in some cases). It is my hope that Emscripten can help against the tendency to write non-web applications, such as native mobile applications (for iOS, Android, etc.) or using plugins on the web (Flash, NaCl, etc.). Simply put, the web is competing with these platforms. Emscripten can make the web a more attractive platform for developers, by letting them use their languages of choice, such as C, C++ or Python (without necessarily compromising on speed: the code generated by Emscripten can be optimized very well, and it is my hope that things like type inference will make it very fast eventually).

Meanwhile, getting back to the PDF rendering demo, I was thinking: How about making a Firefox plugin with it, that is, that when a PDF is clicked in Firefox it is shown in an internal PDF viewer? Aside from the novelty, I think this would be cool to do because it would be an extremely secure PDF viewer (since it would be entirely in JavaScript). If you are a plugin or frontend hacker and think it's a cool idea too, please get in touch and let's make it happen! :)


  1. You should probably follow bug 344620 [[RFE] Native (inline) PDF rendering]


    We have all the graphical primitives we need via SVG and/or canvas. The hangup was always doing the parsing, because it's extremely complicated and requires charset conversions which were difficult to do in JS. Now that we have byte data in JS via XHR, and we can use preexisting parsing libraries via emscripten, I think we have a pretty good way of actually making this happen.

  3. Hopefully this won't be needed for too long (and by 'for too long' I mean for any more than 10 years or so), but in the mean time: Very cool!

    This may be a good fit with my own project (unfortunately, neither plugin nor Firefox frontend-related). If so, I may be back with questions and if time and opportunity permit (which they never seem to do!) contributions.