PDF to html5 conversion – Forms and Javascript

In a PDF file it is possible to attach Javascript to a PDF form. This has many uses including validation, formatting or popping up a message. I have been looking at passing this through for our PDF to HTML5 conversion and this little example shows nicely some of the issues. It is easiest to follow in the raw PDF data so here is the data…

I have a PDF file with a text field (object 1139). The /AA key indicates a dictionary with a /Fo action. This is stored in object 1141.

1139 0 objTx/Subtype/Widget/P 7 0 R/T(AktionJavaScirptAlert)/AA<>/DA(/Helv 0 Tf 0 g)/MK<<>>>>
endobj

This has a fragment of Javascript attached to an action.

1141 0 obj<Hallo”\)\r\n})>>

HTML5 allows us to add Javascript actions to the Form fields so all we need is to extract the Javascript and attach it to the Form component. Piece of cake? Not quite so easy…

Adobe’s dialect of Javascript is not entirely the same or compliant with the Browsers Javascript engine. Here is what I need to convert it into to work in HTML5. The object app. is used by Adobe but redundant in HTML and the double quotes need to become single quotes.

<input type=”text” tabindex=”2″ id=”AktionJavaScirptAlert” value=”” pdfFieldName=”AktionJavaScirptAlert” onClick=”{
alert(‘Hallo’)
}” />

Once done it works as expected.

So some interesting debugging lined up for the next few weeks. It should make for some interesting blog articles…

This post is part of our “HTML5 Article index” in these articles, we aim to help you understand the world of HTML5.

 

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

2 thoughts on “PDF to html5 conversion – Forms and Javascript

  1. This is an interesting idea, but it will work only in the most basic cases: There is a big difference between Acrobat JavaScript and browser based JavaScript. Both share the same core language, so anything that just uses the core language should work, but the really useful part of the JavaScript implementation – for both the browser and Acrobat – comes from the domain specific parts of the JavaScript implementation. In the browser, you are dealing with web sites, HTML and communication with a server and more, whereas in Acrobat you are dealing with PDF specific contents (e.g. forms objects, annotations, optional content…), animations in PDFs, signatures, and a whole bunch of other stuff. Even a simple form field validation will very likely use some of the Acrobat specific validation functions that you wont have access to in HTML5.

    I would love to hear your take on this, and if you are planning on converting some of the Acrobat specific JavaScript functions. I’m pretty sure that a lot could be done by providing some library functions that would mimic the Acrobat specific DOM.

  2. There will be some things that work (control on Layers) but the idea is to get as much as possible working such as the AF formatting and validating commands.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>