6 Replies Latest reply on Dec 19, 2008 9:14 AM by (ScottBendigo)

    Print to PDF and specify file name.

      Hardware/Software Details:
      MacBook 2GHz Intel Core 2 Duo
      OS 10.5.5
      Adobe Acrobat Professional 8.1.2

      Hey all,

      I'm a teacher and I've been scripting PDFs for use as marking sheets for assignments. What I'm trying to achieve is a situation where I tick all the boxes, marks are calculated and comments filled-in using my form fields (which all work fine) and then produce a PDF from that file with the student's name as the new file name (which is where I'm stuck!).

      In the past, I've had a button that on mouseOver brings the field with the student's name into focus, mouseDown evoked the File:Copy menuItem (copying the student name to the system's clipboard) and then mouseUp which triggered File:Print from which I would choose 'Adobe PDF 8.0' as the printer, hit Save, paste the student's name as the file name and choose the appropriate directory.

      File:Copy is no longer a "safe" menu item according to the latest versions of Acrobat, so I'm having to manually copy the name which is complicating the process further.

      I'm working on a script activated by the mouseUp of a button in this PDF which is currently looking like this:

      >var pp = this.getPrintParams();
      // format name to remove spaces and commas
      var studName = this.getField("Name").value.split(",").join("_").split(" ").join("");
      pp.interactive = pp.constants.interactionLevel.silent;
      pp.printerName = "";
      pp.fileName = studentName + ".ps";

      Although the console will list 'AdobePDF8' as one of my printers, I've since discovered that "the destination printer cannot be set through this property in Mac OS" :( . I can set it as my default printer to work around it, but I still can't dictate the name of the new file. So, I've resorted to printing to PostScript with the aim of running them all as a batch through the AdobePDF8 printer later, except there's still a few problems.

      The name of the PostScript file won't work when I try to include the value of another variable (var studentName)
      The fonts in the PostScript files that it does produce get substituted with Courier and look terrible

      Both the API Reference and the Developer Guide seem to indicate that you can print to a file rather than a printer, but both only seem to mention PostScript file using this method.

      Is there another way to do this? If not, is there a solution to the two problems I mentioned?

        • 1. Re: Print to PDF and specify file name.
          Level 1
          I should also probably clarify why I'm printing to PDF rather than using SaveAs:

          SaveAs produces a 840KB file whereas printing through the PDF8 printer makes a 59KB file.
          I want to avoid sending the entire, fully-operational marking tool to my students to play with! ;)
          • 2. Re: Print to PDF and specify file name.
            George_Johnson MVP & Adobe Community Professional
            I think you could simplify this considerably and still get a smaller file size by doing the following:

            1. Determine the file name as you currently are
            2. Flatten the document using the doc.flattenPages() method
            3. Use doc.saveAs() to save with the file name determined in step #1

            • 3. Re: Print to PDF and specify file name.
              Level 1
              Thanks for the reply George. :)

              The other reason that I'd like to print to PDF rather than using Save As is that if I 'Save As', I then have a completed (and flattened/password-protected/neutered) marking sheet still open rather than the original (fully-operational) one - ready to mark the next assignment. If I decided not to bother removing or restricting the functionality of the completed files, I not only produce bloated, administratively-sensitive files, I also run the risk of saving over the top of the last file completed.

              As it is, the file name set up that I have isn't actually working. I'm not sure what I'm doing wrong, but the only way I can seem to get it to actually produce a file is when I basically hard-code the file name like this:

              >pp.fileName = "Lawrence_Chris.ps";

              Although I've not tested it, I'm pretty sure that the method I'm using will works for doc.saveAs() though, right?
              • 4. Re: Print to PDF and specify file name.
                Level 1
                Ok, I've pursued the SaveAs method (as George suggested) with moderate success with the following code (secure function names have been changed ;) ):

                config.js file script:
                >myTrustedSaveAs = app.trustedFunction( function ( doc, studName )
                cPath: studName,
                bCopy: true,
                bPromptToOverwrite: true
                myTrustedOpenDoc = app.trustedFunction( function ( path )
                cPath: path

                Button's onMouseUp script:
                >// get student name and turn it into a file name

                var studFile = this.getField("Name").value.split(",").join("_").split(" ").join("") + ".pdf";
                // get the current document's directory path
                var origFile = this.path;
                var pathBits = this.path.split("/");
                // whack 'em together
                var studFilePath = pathBits.join("/");
                // toss the form fields and stuff
                nNonPrint: 2
                // save the new file
                // open the original again (not working)
                // close the one I just saved

                This is what happens:
                Button is clicked,
                All the form fields and buttons are removed,
                It produces a file with the new file name (e.g. Lawrence_Chris.pdf) next to the original,
                The file closes

                I'm pretty sure that once the SaveAs runs and the new file is created, I actually still have the original file open instead of the that was just saved. This would explain why the openDoc command doesn't seem to do anything because the file it's opening is already open. The closeDoc then runs and of course I then have no open documents!

                So, now I either need a way to revert the open original file so I get all of my form fields back and don't have to reopen it, or a way to close and then immediately open the file without saving changes. :/

                Any ideas?

                slight inconvenience of needing a separate .js file written for the user's JavaScript folder before launching Acrobat to overcome security restrictions. File sizes only seem to get down to about 376KB as opposed to the 40KB files that the AdobePDF8 printer spits out. :(
                • 5. Re: Print to PDF and specify file name.
                  Level 1
                  I was right, it is the original document that remains open rather than the SaveAs document, and I've worked out how to revert the original document too using this code:


                  This means I can toss all the openDoc code as well - hooray!

                  New problem though! Revert isn't behaving properly! :(

                  If I put that revert command at the end of the script attached to the onMouseUp of my button, nothing happens. If I run the revert command through the console, it brings up the normal alert 'Are you sure you want to revert? Cancel/Revert', I click on 'Revert', and it works fine. If I put the revert command in the config.js file as part of my trusted SaveAs function, the normal alert comes up, I click on 'Revert', then another alert comes up with 'Can not close the existing file. Ok', I click Ok, and the document closes! :/

                  What am I doing wrong?
                  (Thanks for letting me think aloud, btw! :) )
                  • 6. Re: Print to PDF and specify file name.
                    You seem brilliant. I have a similar requirement.
                    I need to create a file name using data from 2 of the form fields; full name and number. I am new to this, can you please provide steps on how you do this?

                    or email me at sbendigo[at]yahoo.com