Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For some reason, I'm not sure that's the way CAM::PDF was meant to be used...

:-) Maybe not. Here I formatted your code a little, to be able to read it:

my $pdf = CAM::PDF-> new( $file ); my $p3 = $pdf-> getObjValue( $pdf-> getObjValue( $pdf-> getObjValue( $pdf-> getRootDict-> { Names }{ value }{ JavaScript }{ val +ue } )->{ Names }{ value }[ 1 ]{ value } )-> { JS }{ value } ); $p3-> { StreamData }{ value } = << EO_JS; this.getField("AfterSave1").display = display.hidden; this.getField("AfterSave2").display = display.hidden; this.dirty=false; EO_JS $n = $pdf-> dereference( CAM::PDF::Node-> new( 'reference', $p3-> { Length }{ value })-> { +value } ); $n->{ value }{ value } = length $p3-> { StreamData }{ value };

It's good you found solution that works for your template, and I understand (you said it yourself) this code wasn't meant to be universal. And yet, it wasn't necessary to make so many assumptions.

$pdf-> getObjValue( $node-> { value }) $pdf-> getObjValue( $node-> { value }{ a_key }{ value })

In 1st line, node's type is assumed to be 'reference' (i.e., it's indirect object in PDF structure). In 2nd line it's assumed to be direct object, in this case node's type is 'dictionary', which dictionary has an entry keyed by 'a_key' which (entry), in turn, is assumed to be indirect object, i.e. that node's type assumed as 'reference'! Don't do that. Consider:

$pdf-> getObjValue( $node-> { value }) $pdf-> getValue( $node )

If node's type is 'reference', then these 2 lines return same result. But, not only 2nd line is shorter, it will also work for direct objects, i.e. if node's type is dictionary, array, number, etc. In fact, I don't remember to ever use the getObjValue.

CAM::PDF::Node-> new( 'reference', $node-> { value })-> { value } $node-> { value }

These 2 lines produce the same result :-) (i.e., if node's type is 'reference'). But of course it's better to add new stream using designated method, then it won't be necessary to hack the 'Length' property (it will be added automatically).

use strict; use warnings; use feature 'say'; use CAM::PDF; sub _n { CAM::PDF::Node-> new( @_ )} my $js_code = << 'EO_JS'; this.getField("AfterSave1").display = display.hidden; this.getField("AfterSave2").display = display.hidden; this.dirty=false; EO_JS my $file = $ARGV[ 0 ] or die; my $pdf = CAM::PDF-> new( $file ) or die; my $root = $pdf-> getRootDict; my $names_dict = $pdf-> getValue( $root-> { Names }); my $js_tree = $pdf-> getValue( $names_dict-> { JavaScript }); my $js_tree_root_ary = $pdf-> getValue( $js_tree-> { Names }); my $js_code_stream = $pdf-> createStreamObject( $js_code, 'FlateDeco +de' ); my $js_code_obj = $pdf-> appendObject( undef, $js_code_stream, 0 +); my $js_code_ref = _n( reference => $js_code_obj ); my $js_action_dict = _n( dictionary => { JS => $js_code_ref, S => _n( label => 'JavaScript' ), }); my $js_action_obj = $pdf-> appendObject( undef, _n( object => $js_a +ction_dict ), 0 ); my $js_action_ref = _n( reference => $js_action_obj ); my $js_code_name = _n( label => 'my_js_code' ); push @{ $js_tree_root_ary }, $js_code_name, $js_action_ref; $file =~ s/\.pdf$/++$&/i; $pdf-> cleanoutput( $file );

The only assumption (i.e. no checks) in code above is that input file already has some 'document-level javascript'. Then we just push 2 new entries into already existing array. However, I suspect that it is some dummy JS in your template, since you replace it (not append) with new code. Then, it wasn't necessary to stuff this dummy JS into template to begin with, you could add all necessary top level structures programmatically (similar to what is shown above). Try it for exercise, if so desired :-).


In reply to Re^5: PDF Document Level Script by vr
in thread PDF Document Level Script by Arik123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (2)
    As of 2021-01-23 05:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?