16 Replies Latest reply on Oct 29, 2008 10:55 AM by George_Johnson

    SaveAs...again.  Where does each code component go?

    Level 1
      The code you see below was copied from the LifeCycle forum.

      Can someone help me understand where each section of this code goes to save the PDF? I have no problem placing scripts on the 5 to 10 user's PCs that will be running this thing. However, what is not clear from the half a million or so pages I just went through this morning or the JS API reference is where each section of this goes.

      I would imagin you would need the trusted function parts in the folder *.js. However, you need to pass variables at some point to be able to specify the filename and location. Do you also have to specify the document name? If so how? Does "this." really work?

      I would also imagine that you need a component to call the trusted function from the folder .js. But how does this part work if the folder .js only runs at app init?

      I am going crazy trying to figure out the big "DISABLE" button for acrobat PDF security. Please help. After I get this figured out I am going to post back a good solution. This search has taken awhile.

      Thanks!

      Bret

      mySaveAs = app.trustPropagatorFunction(function(doc,path)
      {
      app.beginPriv();
      doc.saveAs(path);
      app.endPriv();
      })
      myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path)
      {
      // privileged and/or non-privileged code above
      app.beginPriv();
      mySaveAs(doc,path);
      app.endPriv();
      // privileged and/or non-privileged code below
      });

      myTrustedSpecialTaskFunc(this, "/c/jesus/mySavedDoc.pdf");
        • 1. Re: SaveAs...again.  Where does each code component go?
          George_Johnson MVP & Adobe Community Professional
          > myTrustedSpecialTaskFunc(this, "/c/jesus/mySavedDoc.pdf");
          >
          > Do you also have to specify the document name? If so how? Does "this." really work?

          The value if "this" when the function is called is equal to the current document object, assuming it's called from a document event (or there's a document opened in Acrobat). That other parameter of that function is a file path that will be used to save the document.

          Since you've set up the function as a trusted function, it can be called from a document event, for example the Mouse Up event of a button, which you would not be able to do otherwise.

          Note that you would not include that last line in the folder JavaScript. That last line is what you would use in the document to call the function.

          George
          • 2. Re: SaveAs...again.  Where does each code component go?
            Level 1
            The call that started the below mess:

            FNsaveAs(this, saveFilename);

            The code in the FNsaveAs.js file in "C:/program files/adobe/acrobate 9.0/javascripts/":

            // should allow priviledged calls to saveAs method
            TrustFNsaveAs = app.trustPropagatorFunction(function(docvalue,saveFilename)
            {
            app.beginPriv();
            docvalue.saveAs(saveFilename);
            app.endPriv();
            })
            FNsaveAs = app.trustedFunction(function(docvalue,saveFilename)
            {
            // privileged and/or non-privileged code above
            app.beginPriv();
            TrustFNsaveAs(docvalue,saveFilename);
            app.endPriv();
            // privileged and/or non-privileged code below
            });

            The error I get when I click on the button that has a mouse up event that runs the javascript function call:

            Acrobat JavaScript Debugger Functions Version 9.0
            Acrobat EScript Built-in Functions Version 9.0
            Acrobat Annotations / Collaboration Built-in Functions Version 9.0
            Acrobat Annotations / Collaboration Built-in Wizard Functions Version 9.0
            Acrobat SOAP 9.0
            Exception in line 1 of function top_level, script Field:Mouse Up

            NotAllowedError: Security settings prevent access to this property or method.
            App.trustedFunction:1:Field btn.form.save:Mouse Up

            Do I have everything put in the right places? Can I use a variable to send data to the trusted function as a specified path?

            Thanks and please advise.

            Bret
            • 3. Re: SaveAs...again.  Where does each code component go?
              George_Johnson MVP & Adobe Community Professional
              What version of Acrobat are you using? Are you attempting this with Reader?

              George
              • 4. Re: SaveAs...again.  Where does each code component go?
                Level 1
                Negative George. This is Acrobat Professional Version 9. I want to be able to make this PDF in Pro and use it in Reader however. I will of course install the .js piece in their javascript folders.

                Am I doing something backwards?

                Bret
                • 5. Re: SaveAs...again.  Where does each code component go?
                  George_Johnson MVP & Adobe Community Professional
                  The error message you're getting doesn't make sense to me, if as you say the app.beginPriv code is in a folder-level JavaScript file. That is one of the few places it is allowed. I will test the code you posted to see if it works for me...

                  George
                  • 6. Re: SaveAs...again.  Where does each code component go?
                    Patrick Leckey Level 3
                    > I want to be able to make this PDF in Pro and use it in Reader however.

                    Do you mean you want to be able to use saveAs in Reader? If that is the case, the form will need to be Reader Enabled in order to give Reader save rights.

                    Also, are you sure all of your code is in the JS file?

                    > App.trustedFunction:1:Field btn.form.save:Mouse Up

                    This error explicitly says that you are trying to call the App.trustedFunction method from the Mouse Up event on a button, which is not allowed. Any app.trustedFunction calls need to be in the folder-level JS.
                    • 7. Re: SaveAs...again.  Where does each code component go?
                      George_Johnson MVP & Adobe Community Professional
                      Your code worked for me, though I did have to change one line to:

                      docvalue.saveAs({cPath: saveFilename});


                      If I were you, I would reboot and try again. Note that for this to work for Reader, the form will have to be Reader-enabled.

                      George
                      • 8. Re: SaveAs...again.  Where does each code component go?
                        George_Johnson MVP & Adobe Community Professional
                        > This error explicitly says that you are trying to call the App.trustedFunction method from the Mouse Up event on a button...

                        Actually, it indicates that the code was triggered by the Mouse Up event of the button, it does not indicate the location of the code that caused the error.

                        George
                        • 9. Re: SaveAs...again.  Where does each code component go?
                          Patrick Leckey Level 3
                          > Actually, it indicates that the code was triggered by the Mouse Up event of the button, it does not indicate the location of the code that caused the error.

                          I thought if it was from a folder-level script it shows the name of the file in the error - I can't test right now but I believe this is the case.
                          • 10. Re: SaveAs...again.  Where does each code component go?
                            Level 1
                            Ok. All.  It is working. The following code, assuming it is put in the right places which I am about to share, will save a form to a default location specified by a variable.  Much thanks to the individuals above who have contributed to this...what appears to be the first complete code sample of how to get around the PDF security measures.<br /><br />Not to mention any names...*Coughs* George Johnson, PDL, and the original code slingers at the LifeCycle Forum */Coughs*<br /><br />Requirements for the 'Save button' Scenario:<br />1)Call Doc Level Function by a button event, MOUSE UP probably best<br />2)Doc Level Function performs any pre-save tasks or validation you need plus calls Priviledge Function<br />3)Priviledge function located in a file in the Acrobat or Reader javascript program directory ( most commonly C:/program files/Adobe/<either 'Reader' or 'Acrobat x.x'>/Javascript ) if you have windows<br />4)Enable Reader features for deployment in Adobe Acrobat.<br /><br />IN THE MOUSE UP BUTTON EVENT JAVASCRIPT CLIP:<br /><br />FNsaveform();<br /><br />IN THE FUNCTION CALLED BY THE MOUSE UP BUTTON EVENT (i.e. FNsaveform(); )<br /><br />function FNsaveform()<br />{<br />// set the path location<br />// could also do something like the below example<br />// var saveFilename = ("/h/location/" + variable filename + ".pdf")<br />var saveFilename = "/h/location/the_actual_file.pdf"<br />// call the function to save the file<br />// 'this' refers to the current document<br />// 'saveFilename' is the variable we created above for the file<br />// location<br />FNsaveAs(this, saveFilename);<br />// end of function<br />}<br /><br />IN THE 'filename.js' FUNCTIONS PUT INTO THE JAVASCRIPT FOLDER IN THE ACROBAT PROGRAM DIRECTORY<br /><br />// should allow priviledged calls to saveAs method<br />TrustFNsaveAs = app.trustPropagatorFunction(function(docvalue,saveFilename) <br />{ <br />app.beginPriv(); <br />docvalue.saveAs({cPath: saveFilename}); <br />app.endPriv(); <br />}) <br />FNsaveAs = app.trustedFunction(function(docvalue,saveFilename) <br />{ <br />// privileged and/or non-privileged code above <br />app.beginPriv(); <br />TrustFNsaveAs(docvalue,saveFilename); <br />app.endPriv(); <br />// privileged and/or non-privileged code below <br />});<br /><br />Now I had some extra validation and filename creation stuff in there.  The validation made sure the fields were filled that I used to create the filename which I then passed to 'saveFilename' which was then passed to the function to save the file.  It works great but make sure as the outstanding individuals above noted, in order for it to work 1) on another PC and 2) in Acrobat Reader the 'filename.js' must be in the program directory and the features for READER FEATURES must have been enabled under ADVANCED > ENABLE FEATURES IN ADOBE READER.<br /><br />Hopefully nobody will search for like 3 hours and get nowhere like I did this morning!  Good luck to all!  God bless!
                            • 11. Re: SaveAs...again.  Where does each code component go?
                              George_Johnson MVP & Adobe Community Professional
                              What I would do at this point is at least change the names of your trusted functions for security reasons. The reason that the saveAs method is not allowed to execute if the code is in the document is so that any old PDF that a user opens up can't saveAs itself without the user knowing.

                              You've decided to circumvent this security measure, which is fine, but you've also announced to the world that the FNsaveAs function can be used on your user's systems instead. This may not seem like a big deal since we are unlikely to know you or know who your users are, but you should ponder the possibilites.

                              Also, note that in this case the use of the trust propagator function is a bit superfluous since FNsaveAs does nothing other than call TrustFNsaveAs, unless you plan on calling it from other trusted functions.

                              George
                              • 12. Re: SaveAs...again.  Where does each code component go?
                                Level 1
                                George,

                                Thanks! Already done.

                                Bret
                                • 13. Re: SaveAs...again.  Where does each code component go?
                                  Level 1
                                  Hello All -

                                  I have followed the instructions that are listed above but I cannot seem to get this to run...

                                  I receive the following error message:
                                  Acrobat JavaScript Debugger Functions Version 7.0
                                  Acrobat Database Connectivity Built-in Functions Version 8.0
                                  Acrobat EScript Built-in Functions Version 8.0
                                  Acrobat Annotations / Collaboration Built-in Functions Version 8.0
                                  Acrobat Annotations / Collaboration Built-in Wizard Functions Version 8.0
                                  Acrobat Multimedia Version 8.0
                                  Acrobat SOAP 8.0

                                  TypeError: docvalue.saveAs is not a function
                                  4:Folder-Level:App:SavingFileDocument.js
                                  TypeError: docvalue.saveAs is not a function
                                  4:Folder-Level:App:SavingFileDocument.js
                                  • 14. Re: SaveAs...again.  Where does each code component go?
                                    George_Johnson MVP & Adobe Community Professional
                                    How did you create the file you're trying to save? What code are you using to call the FNSaveAs function?

                                    George
                                    • 15. Re: SaveAs...again.  Where does each code component go?
                                      Level 1
                                      George -

                                      Thanks for responding...I figured it out. I had to change my code just a little bit...it was not finding the document object. I changed if from :

                                      FNsaveAs(this, saveFilename);

                                      to this :

                                      var myDoc = event.target ;
                                      ...
                                      FNsaveAs(myDoc, saveFilename);
                                      • 16. Re: SaveAs...again.  Where does each code component go?
                                        George_Johnson MVP & Adobe Community Professional
                                        For the benefit of others, you need to use "event.target" to get a reference to the current document for PDFs created with LiveCycle Designer. For documents created with Acrobat, use "this".

                                        George