6 Replies Latest reply on Jan 5, 2011 5:38 PM by maniac_coder

    How to load doc-level JavaScript via FDF

    maniac_coder

      I apologize if this question is answered elsewhere on the forum -- I've spent a fair amount of time searching through the forum and other resources brought up in response to Google searches, all without a solid result; therefore I'm hoping that someone in the forum can help out.

       

      I've got several dozen PDF forms that all use the same core set of JavaScript functions to perform various validation and formatting processes. So far, I've loaded this library as a document-level script in each PDF and then loaded the PDFs to the server.

       

      When a user requests to view their specific document, the link that they click actually generates an FDF file that is sent to their browser. The FDF causes the target PDF to be loaded in the browser, into which the FDF data is loaded. A page load Script executes the do_DocLoad function, which sets up the document for initial user use (populates combo boxes, etc.) The user then can interact with the form in various ways and then, optionally, submit the data back to the server for processing. The library functions are referenced in various field formatting or validation calls, as well as by the Submit button that I've got on the form that performs a final field validation prior to submission. So far, this process works very well.

       

      The issue is that every time a change, even a minor one, is needed in any of the core set of library functions, we have to open each and every PDF to apply the change, then save it and upload the entire batch to the server. Because of the significant amount of time that this can take (which will increase as the PDF library grows), I'd like to instead load the JavaScript functions in the FDF.

       

      The ideal would be a method to allow the FDF to cause Acrobat to load the script from a file on the server, similar to how the PDF is called. Because I don't know if that would even work, I'm looking at having the ASPX page that processes the FDF, merging the user's data into the FDF format before serving it to the browser also merge in the script and serve it all up piping hot for the user's enjoyment (or so we hope). I've tinkered with the FDF, adding a /JavaScript section at the end of the FDF as follows:

       

          << /V (~data_client_name~)/T (client_name)>>
          << /V (~data_client_address~)/T (client_address)>>]
          /F (~data_FileName~)/ID [ <  blah blah ><  blah blah >]
          /JavaScript << /Doc 2 0 R >>
        >>
      >>
      endobj
      2 0 obj [ (DocScript1) 3 0 R ] endobj
      3 0 obj <<>>
      stream

      ** script goes here **

      endstream
      endobj

       

      trailer << /Root 1 0 R >>
      %%EOF

       

      In the section where ** script goes here ** I have attempted to paste the script library, without success. This library consists of several different JavaScript functions.

       

      In testing, the above didn't work, unfortunately. :-( I got all kinds of wierd data in the form fields that are processed by the functions that I was trying to load - the page open calls didn't return any errors, which was good, but the fields formatted by the initial setup method were all messed up. I'm looking for any pointers/tips on what I might be doing wrong. Are there special characters that I need to escape/avoid in sending JavaScript this way (I'm using regular expression pattern matching and other code within the script which contain some special characters such as /, \, ^, etc.). Is there a limit to the size of the data that I can stream in this way? Is it as simple as loading my script /Before instead of /Doc? Anything else that I might try?

       

      Thanks in advance for any assistance that can be given! Every time that I get to go back and spend a couple of hours updating the core library in all of these PDFs is time that I'd rather be spending doing other things. Thanks again!

        • 1. Re: How to load doc-level JavaScript via FDF
          George_Johnson MVP & Adobe Community Professional

          Instead of attempting to send an FDF containing the document-level code to the client, you should instead look into ways of automating the process of updating the collection of PDFs with the new code. You may have trouble with your approach working with Reader anyway. You can automate this with a batch sequence (or IAC) that does one of the following:

           

          1. uses the addScript method to create the document-level code

          2. imports an FDF like you're attempting

          3. adds a single page PDF which contains all of the code to each document and then delete the page, leaving the code behind.

           

          I usually do #3 since it's the simplest.

          1 person found this helpful
          • 2. Re: How to load doc-level JavaScript via FDF
            maniac_coder Level 1

            This sounds very intriguing and not at all along the lines of what I was thinking of. I'm glad to have the forums to be able to get different perspectives on

            things like this!

             

            I have a couple of additional questions on this. I like #3 but, not having done it before, I'm curious as to how to get it to work.

             

            I presume that I would create a new batch process in Acrobat (I am running Acrobat Pro 9) and then on each document processed, do the following:

            1. Import a PDF page (from a specified file that has the document-level scripts included.

            2. Delete the page just-added

            3. Save

            4. Repeat, all docs specified.

             

            Now questions:

            1. I don't understand how the document scripts will be "left behind" when doing this.

            2. Do I need to delete the existing document script(s) before doing this, or will they automagically be overwritten?

            3. Are there any steps between the insert and the delete that I need to do?

             

            Finally, just to add another twist, have you seen this work with PDFs that are secured (we apply a password to protect the documents from alteration, with the exception of filling out form fields)?

             

            So far, in testing, the best method appears to be to store the PDFs separate in an un-secured format (it simply won't work if they're secured -- at least, I haven't gotten it to work yet -- I could probably do it interactively, but who wants to type the goofy password 500 times). I have a separate dummy PDF (1 page with just some explanation text if someone opens it and document-level scripts included) also stored in the folder. Then, run the batch process to do the insert, delete, then apply security on all docs in the folder. So far, so good. The only part that doesn't seem to be working is that the document-level scripts on the dummy PDF don't get left behind after the insert/delete. I removed the doc-level scripts on the original PDFs so that I could more easily verify that the ones from the dummy PDF get "left behind". I'm sure that I'm missing something simple here -- If you can help close that final gap, I'd greatly appreciate it!

            • 3. Re: How to load doc-level JavaScript via FDF
              George_Johnson MVP & Adobe Community Professional

              As a test, what happens of you manually do the insert/delete process?

              • 4. Re: How to load doc-level JavaScript via FDF
                maniac_coder Level 1

                The same thing, actually. Tinkering with it, I found that if I open my dummy file first, then insert the pages from the target PDF into the dummy PDF, then

                delete the dummy PDF page that I get what I want -- a PDF with all document scripts intact. (I haven't tested yet to see what happens if both docs have a script of the same name -- that test is next.)

                 

                Unfortunately, the Acrobat batch processes work well inserting a static PDF (the dummy PDF) into a series of files, but not the reverse. Any other thoughts?

                • 5. Re: How to load doc-level JavaScript via FDF
                  maniac_coder Level 1

                  I've also been tinkering with getting the script to load from the FDF and, after some tweaking, have got it working (almost). The scripts load to the PDF just fine, with the exception that I'm losing some special, non-ASCII characters -- they're getting converted to ? in the code. However, that problem is surmountable, either by encoding them differently or possibly changing the character page used by the ASPX page.

                   

                  If I could get the dummy PDF thing to work, that would be great; however, as it stands, it only works in the reverse of what I need, unfortunately, so I'm probably going to try to get the FDF fully-working. Thank you for your help and especially getting me to look at this in a different way!

                  • 6. Re: How to load doc-level JavaScript via FDF
                    maniac_coder Level 1

                    George, as noted in other posts here, I couldn't get Acrobat to work the correct way on the PDF merge, although it really is a clever idea. I instead went back and tweaked the FDF some more and now have it working, with the exception of the loss of special characters in the script that I'll have to encode differently or otherwise change how I'm handling them to get this to work. All-in-all, though, I think that the FDF process will work OK for what I'm looking for. It will allow the ASPX page to merge the scripts into the FDFs real-time as they are served up to the end-users, allowing the scripts to be maintained in a single location (which is always nice -- no more worrying about whether I missed updating the script on one of the now nearly 100 PDFs).

                     

                    Your answer was very helpful, though, and got me to thinking about this problem in a different way, so I've flagged your answer as a helpful answer. Thank you very much for your input on this!