qq has asked for the wisdom of the Perl Monks concerning the following question:

I'm unable to get PDF::Reuse to help me add javascript to a PDF. I'm following very closely one of the examples in the Tutorial.

Since I don't have access to Acrobat (no trial version on OSX) its hard to tell whats going on. I just get an empty looking pdf. (If I omit the prCompress line below and call strings on the doc, I do see the javascript in there). Here is the code:

#!/usr/bin/perl use strict; use warnings; use PDF::Reuse; my $js = do { local $/; <DATA>; }; prDocDir('doc'); prFile('book.pdf'); prCompress(1); prJs($js); prInit('nameAddress();'); prField('First_Name','Lars'); prEnd(); __DATA__ function nameAddress(page, xpos, ypos) { var thePage = 0; if (page) { thePage = page; } var myRec = [ 40, 650, 0, 0]; // default position if (xpos) { myRec[0] = xpos; } if (ypos) { myRec[1] = ypos; } var labelText = [ "First_Name", "Surname", ]; for ( var i = 0; i < labelText.length; i++) { myRec[2] = myRec[0] + 80; // length of the label myRec[3] = myRec[1] - 15; // height // a label field is created var fieldName = labelText[i] + "Label"; var lf1 = this.addField(fieldName, "text", thePage, myRec); lf1.fillColor = color.black; lf1.textColor = color.black; lf1.readonly = true; lf1.textSize = 12; lf1.value = labelText[i]; lf1.display = display.visible; // a text field for the customer // to fill-in his/her name is created myRec[0] = myRec[2] + 2; // move 2 pixels to the right myRec[2] = myRec[0] + 140; // length of the fill-in field var tf1 = this.addField(labelText[i], "text", thePage, myRec); tf1.fillColor = ["RGB", 1, 1, 0.94]; tf1.strokeColor = ["RGB", 0.7, 0.7, 0.6]; tf1.textColor = color.black; tf1.borderStyle = border.s; tf1.textSize = 12; tf1.display = display.visible; myRec[0] = myRec[0] - 82 // move 82 pixels to the left myRec[1] = myRec[1] - 17; // move 17 pixels down } }

Any advice?

qq

Replies are listed 'Best First'.
Re: PDF::Reuse - adding javascript
by dragonchild (Archbishop) on Dec 13, 2004 at 14:56 UTC
    • Have you verified that the Javascript will work on an OS-X box?
    • Have you verified that the Javascript is being called?
    • Have you verified that if (page) { thePage = page; } is executing right?
    • Have you verified that the Javascript is even being written to the resultant PDF?

    In other words, make sure that what you're trying to do will even work where you're trying to do it before seeing whether PDF::Reuse is doing the right thing.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Good advice, but how can I put it into practice?

      Because I don't have Acrobat I don't know of any other way to embed javascript in a PDF. Nor to tell if its being called. I don't believe the code would work outside of a PDF, because its using the the PDF object models.

      I mentioned calling strings on the resultant pdf - so I know its actually being put in.

      However I've just installed a standalone javascript interpreter to see if that gives me more info.

      Thanks, qq

        I mentioned calling strings on the resultant pdf - so I know its actually being put in.

        Actually, you know it's being put into the data structure. You actually have no idea whether or not it's in the PDF because you're not checking the PDF. Unless, of course, you're parsing the PDF with a second script calling PDF::Reuse ...

        Now, I would definitely get Acrobat Reader - there is a free version of that for every single OS that has a GUI and more than 2 users. That way, you can verify your PDF. You don't need Adobe Acrobat (the pay version) to make sure the Javascript is working - you would need it to build a new PDF.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: PDF::Reuse - adding javascript
by simonm (Vicar) on Dec 13, 2004 at 18:33 UTC
    I would suggest finding people with Windows and/or Linux machines and having them try opening it. If it works, you'll know it's a platform problem, and if it doesn't, one of the other platforms might provide you with additional diagnostic information. (Also, try checking /var/log/system.log on your OS X box to see if Acrobat's logging some kind of complaint.)