1 Reply Latest reply on Nov 18, 2010 8:18 AM by Cookie4ddict

    Set filename of printed PDF via script

    Cookie4ddict

      Hi folks!

       

      I have a little problem with generating a PDF. First my Workflow:

      I have a InDesign document with 6 pages. This document is merged with a databasefile containing 100 records. After merging it, we have to generate a PDF file. This PDF file is opened within acrobat. Within the document, I'm searching for an ID each record has so I can split the document to 100 files (each with 6 pages) and name it by the ID I found.

      The script within Acrobat is finished and working. I thought, the InDesign script is finished ,too. But I was wrong -.-

      I merged the databasefile with the document and exported it as PDF. But after exporting, we noticed that the script within Acrobat isn't finding the adressheader where the ID is in. The script only noticed the text after that header. The result is, that Acrobat get's always "null" as ID

      If we print the PDF with our PDFprinter, the header could be read by our Acrobat script. I don't know why this is... But now I changed the script to print the files via our PDF printer. Unfortunately I can't set a name for my exported file - do you know if there is a possibility to print PDF's without prompting after each one and with a via script given name?

       

      Here you can see the old script for InDesign and right after it, the Acrobat sript. Maybe I made some mistake by generating my PDFexport and don't need to use the printer?

       

      INDESIGN SCRIPT:

       

      /**
        * prompts filebrowser and stores name and path of file in variable
        */
      var sourceDocument = File.openDialog("Bitte Indesign-Dokument auswählen", "*.indd", false);

      /**
        * stores only prefix of filename for use as new filename
        */
      var newName = sourceDocument.name.substr(0,  sourceDocument.name.length-5);

      /**
        * stores folder where file is stored
        */
      var dbSourceFolder = sourceDocument.parent+"/";

      /**
        * prompts for databasefile where generating should begin
        */
      var dbstartfile = File.openDialog("Bitte Start-Datenbankdatei auswählen", "*.txt", false);

      /**
        * gets basename of databasefile
        */
      var dbstartfilename = dbstartfile.name.slice(0, dbstartfile.name.search(/_Teil+/));

      /**
        * gets number of first databasefile
        */
      var i = dbstartfile.name.slice(dbstartfile.name.search(/_Teil+/)+5).slice(0, -4);

      /**
        * generates path name and name of first databasefile to use
        */
      var dbSource = dbstartfile;

      /**
         *set PDF preset for generating PDF
         */
      var PDFPreset= app.pdfExportPresets.item("GAG-PDF");

      /**
         * stops throwing of alerts
         */
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

      // if databasefile isn't existing message will be thrown
      if( dbSource.exists == false ) {
          // restart of alert throwing
          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
          alert("Datei " + dbSourceFolder+dbprefix+"_Teil"+i+".txt konnte nicht gefunden werden! \n\rBitte starten Sie den Vorgang erneut und geben Sie die richtige Datenbankdatei an." );
      }
      // else process starts
      else {
          while(  dbSource.exists == true ) {
              // opens source indesign document without showing it
              mergeDocument = app.open(File(sourceDocument), false);
              // sets which databasefile should be used for data merge
              mergeDocument.dataMergeProperties.selectDataSource(File(dbSource));
              // starts merging of indesign document and database file
              mergeDocument.dataMergeProperties.mergeRecords();
              // exports generated document as PDF file
              app.activeDocument.exportFile(ExportFormat.pdfType, File(sourceDocument.parent+"/"+newName+"_Teil"+i+".pdf"), false, PDFPreset);
              // closes opened indesign document
              mergeDocument.close(SaveOptions.no);
              i++;
              // change filename of database file to get next file
              dbSource = File(dbSource.parent+"/"+dbstartfilename+"_Teil"+i+".txt");
          }
      }
      // restart of alert throwing
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
      alert("PDF-Generierung abgeschlossen!");

       

       

      ACROBAT SCRIPT:


      /**
      * Path where files should be saved
      * Special Characters like spaces should be escaped with \
      * If you want to modify the folder, use following form:
      * "/Driveletter/Foldername/../LastFolderName/"
      * Make sure not to forget the / before and after the location
      */
      var filepath = "/c/pdf_split_test/";

       

      /**
      * Number of expose pages - feel free to change
      */

       

      var pageType = app.prompt("Bitte geben Sie die gewünschte Seitenzahl der Exposés an.", "");
      alert(pageType);

       

      /**
      * regular expression for search
      */

       

      var idNumber = /08\d\d\d\d\-\d\d\d\-\d\d\d\d\d-\d\d\d-\d\d/g;

       

      /**
      * if possible this function extracts the searched number as string
      *
      * @param rematch string which should be searched in document
      * @return null if rematch is not found or string if rematch is found
      */

       

      function ExtractFromDocument(reMatch) {
        try {
               var Out = new Object();
               for (var i = 0; i < 1; i++)
            {
                numWords = this.getPageNumWords(i);
                var PageText = "";
                for (var j = 0; j < 30;j++) {
                    var word = this.getPageNthWord(i,j,false);
                    PageText += word;
                }
                var strMatches = PageText.match(reMatch);
                if (strMatches == null) continue;
            }
            return strMatches;
        } catch(e)
        {
            app.alert("Processing error: "+e)
        }
      }

       

      /**
      * tries to load given filename (extracted number)
      *
      * @param filename string of file which should be checked
      * @param n number to iterate while checking for files
      * @return true if file exists or false if not
      */

       

      function checkIfFileExists(filename, n) {
          var existingDoc = false;
          try {
              if( n == 0) {
                  var checkDoc = app.openDoc(filepath+filename+"-000.pdf");
              } else {
                  var checkDoc = app.openDoc(filepath+filename+"-000_"+n+".pdf");
              }
              checkDoc.closeDoc();
              existingDoc = true;
          } catch (e) {
          }
          if( existingDoc == true ) {
              n = n+1;
              n = checkIfFileExists(filename, n);
          }
          return n;
      }

       

      var pageAmount = this.numPages;
      for( i=0; i<pageAmount; i+pageType ) {
          var filename = ExtractFromDocument(idNumber);
          fileExistence = checkIfFileExists(filename, 0);
          if(fileExistence != 0) {
              this.extractPages({nEnd:(pageType-1), cPath : filepath+filename+"-000_"+fileExistence+".pdf"}); 
          } else {
              this.extractPages({nEnd:(pageType-1), cPath : filepath+filename+"-000.pdf"});
          }
          this.deletePages({nStart:0, nEnd: pageType-1});
      }

        • 1. Re: Set filename of printed PDF via script
          AdobeScripts Level 3

          Hi,

          I have a little problem with generating a PDF. First my Workflow:

          I have a InDesign document with 6 pages. This document is merged with a databasefile containing 100 records. After merging it, we have to generate a PDF file. This PDF file is opened within acrobat. Within the document, I'm searching for an ID each record has so I can split the document to 100 files (each with 6 pages) and name it by the ID I found.

          Why you don't export 6-page PDFs directly from InDesign?

           

          robin

          www.adobescripts.co.uk