6 Replies Latest reply on Aug 17, 2011 3:41 AM by ikitzmann

    Script export to PDF: *some* hyperlinks are missing

    ikitzmann

      Hello,

       

      we have a batch script, that opens a folder of indd files, does some replacements, creates some hyperlinks and then saves the document to a new indd. After that the document is exported to a pdf file.

       

      The problem is, that in the pdf file some of the hyperlinks are not exported. Most times, the hyperlinks on the first pages are missing (hint: the script creates them from the last page to the first. There seems to be a connection...). This also depends on the input document, some are fine, some are bad. Not a kind of reliable solution.

       

      The PDF preset options are ok, because there are most of the hyperlinks in the pdf. The script seems ok, because in the saved indd are all hyperlinks as expected (remember, this is saved before the pdf export).

       

      I could replicate the issue on Windows in InDesign CS4 (German), and both CS5 and CS5.5 (English).

       

      In my stripped down test script and test document it helpes if I replace the text content of the hyperlinks with its own text (see comment in the script below) but in my full script this workaround not always succesful.

       

       

      Has anyone had the same issues before? Does anyone know a solution or at least a workaround?

       

       

      Best regards,

      Ingo

       

       

      The testdocument has just 19 pages, each with one text frame (all connected) and with the content: 1 linebreak 2 linebreak.... (numbers from 1 to 1000, each in its own line. Just copied them from a excel document).

       

      Here is the script (stripped down from my whole batch script). You will have to edit the path names and there has to be a pdf preset named "PDFHyperlinks" avaiable in InDesign:

       

      //target the latest version of InDesign
      #target "InDesign"
      
      // run main with single UNDO-Step function
      app.doScript(
           main,
           ScriptLanguage.javascript,
           undefined,
           UndoModes.fastEntireScript
           );
      //----------------end-global-----------------------------------------------------
      
      
      //----------------start-utility-functions----------------------------------------
      
      // creates a hyperlink in InDesign for the given text section and url
      function addHyperlink(doc, textSection, urlString) {
        var myHyperlinkTextSource = doc.hyperlinkTextSources.add(textSection);    
        var myHyperlinkDestination = doc.hyperlinkURLDestinations.add({destinationURL:urlString}); 
        var myHyperlink = doc.hyperlinks.add({source:myHyperlinkTextSource,   destination:myHyperlinkDestination});    
        //myHyperlink.visible = false;
        myHyperlink.visible = true; // for testing puposes
      }
      //----------------end-utility-functions------------------------------------------
      
      //----------------start-main-functions-------------------------------------------
      
      function main(){
        alert('Start!','Start!');
        
          app.scriptPreferences.enableRedraw = true;
        
        // set all files
        var inputFile = new File('~/Desktop/input.indd');
        var outputFile = new File('~/Desktop/output.indd');
        var outputPDF = new File('~/Desktop/output.pdf');
        
        // ignore errors on - so that e.g. link-errors won't stop the script
        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
        
        // process document
        var currentDocument = app.open( inputFile,false);
        //At this point, you could do things with the document without showing the
        //document window. In some cases, scripts will run faster when the document
        //window is not visible.
        
        // add hyperlinks etc.
        modifiyCurrentDocument(currentDocument);
        
        // save as InDesign
        currentDocument.save( outputFile );
      
        // Export as PDF
        var preset = app.pdfExportPresets.itemByName('PDFHyperlinks');
        preset.includeHyperlinks = true;
        currentDocument.exportFile(ExportFormat.PDF_TYPE, outputPDF , false, app.pdfExportPresets.itemByName('PDFHyperlinks'));
        
        currentDocument.close();
      
        // ignore errors off
        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
      
        alert('Finished!','Finished!');
      
      }
      
      
      
      
      // creates hperlinks for the found numbers
      function makeNumberHyperlinks(doc, myFounds) { 
        for (var i = myFounds.length-1; i >= 0; i-- ) 
        { 
          var number = myFounds[i].contents;
          // create URL
          var urlString = 'http://www.test.de?parameter1=abcd&parameter2=' + number + 'parameter3=xyz';
          // create hyperlink in InDesign
          addHyperlink(doc, myFounds[i], urlString);
          
          // uncommenting the following line is a workaround in *some* cases
          //myFounds[i].contents = number;
        }
      } 
      
      
      
      function modifiyCurrentDocument(currentDocument) {
        // set default search options
        with (app.findChangeGrepOptions) { 
          includeFootnotes = true;  
          includeHiddenLayers = false; 
          includeLockedLayersForFind = false; 
          includeLockedStoriesForFind = false; 
          includeMasterPages = true; 
        } 
      
        // make nr hyperlinks
        app.findGrepPreferences = NothingEnum.nothing;
        app.changeGrepPreferences = NothingEnum.nothing;
        app.findGrepPreferences.findWhat = '([0-9]+)'; // numbers are only digits
        var numbersFound = currentDocument.findGrep();
        //alert(numbersFound.length);
        makeNumberHyperlinks(currentDocument, numbersFound);
      }
      
      //----------------end-main-functions---------------------------------------------
      
        • 1. Re: Script export to PDF: *some* hyperlinks are missing
          John Hawkinson Level 5

          Is the script creating invalid hyperlinks? Do they work? What if you export to IDML and then re-open in InDesign, do they work?
          If they don't, what if you inspect the hyperlinks in the IDML -- how are the broken ones different?

          • 2. Re: Script export to PDF: *some* hyperlinks are missing
            ikitzmann Level 1

            Is the script creating invalid hyperlinks? Do they work?

             

            No, the hyperlinks are ok, because in the indd the script saves before the PDF export, all hyperlinks are fine. Even if I export a new PDF from this indd afterwards, all hyperlinks are correct in the PDF. So I believe the script is correct regarding the creation of the hyperlinks. In the posted example, all hyperlinks are even created with the same lines of code - so I would not expect any differences.

             

            What if you export to IDML and then re-open in InDesign, do they work?
            If they don't, what if you inspect the hyperlinks in the IDML -- how are the broken ones different?

             

            I have not tried that, but I expect they are ok, just as in the created indd. But I will test this.

             

            Any other ideas?

             

            As a workaround I will try to reopen the "correct" saved indd and use this as source for the PDF export. But this would be very suboptimal because this will lead to much more running time of the script.

            • 3. Re: Script export to PDF: *some* hyperlinks are missing
              John Hawkinson Level 5
              No, the hyperlinks are ok, because in the indd the script saves before the PDF export, all hyperlinks are fine. Even if I export a new PDF from this indd afterwards, all hyperlinks are correct in the PDF. So I believe the script is correct regarding the creation of the hyperlinks. In the posted example, all hyperlinks are even created with the same lines of code - so I would not expect any differences.

              So you're saying the problem occurs only when you export the PDF within the script immediately after generating the hyperlinks.

              If you save, close, and reopen the document, the PDF export is fine?

              What if you export the PDF by hand (not through scripting) while the document is still open?

               

              I guess you should try to narrow down the point at which this bad PDF export happens.

              Do you have any 3rd party plugins?

              This sounds suspiciously reminiscent of the kinds of problems that cause the background PDF export to hang...

              [do you have preflight enabled? Does disabling it, restarting InDesign, and then testing again change anything?]

               

              mumble mumble reference counting leaks bugs mumble mumble.

               

              How many PDFs is this and how often do you do this? I would not think that re-opening the files would be that bad, unless they were huge.

               

              Do you ever have more than one document open at a time? That could also be a place for refcount bugs to creep in.

              1 person found this helpful
              • 4. Re: Script export to PDF: *some* hyperlinks are missing
                ikitzmann Level 1

                I guess you should try to narrow down the point at which this bad PDF export happens.

                Do you have any 3rd party plugins?

                This sounds suspiciously reminiscent of the kinds of problems that cause the background PDF export to hang...

                [do you have preflight enabled? Does disabling it, restarting InDesign, and then testing again change anything?]

                 

                Do you ever have more than one document open at a time? That could also be a place for refcount bugs to creep in.

                Only one own 3rd party plugin, and this does only import some files and was not used in this test document (but is used in the "real" files). Disabling preflight did not make any difference. The script only opens one document a time and I start it within an empty InDesign.

                 

                How many PDFs is this and how often do you do this? I would not think that re-opening the files would be that bad, unless they were huge.

                About 500 input files, each is processed into up to 6 variants, so the final pdf count will be around 3000 files. That means, a performance loss by let's say 20 % means an increase of the runtime by hours! If there is no other solution/workaround than reopening the file we have to live with that. But this is not a solution I would prefer.

                 

                So you're saying the problem occurs only when you export the PDF within the script immediately after generating the hyperlinks.

                If you save, close, and reopen the document, the PDF export is fine?

                Yes, it is only in the PDF created by the script. If I open the created indd or idml an manually export this to PDF, everything is fine.

                 

                What if you export the PDF by hand (not through scripting) while the document is still open?

                This is something that could lead to finding the bug. To try this, I had to modify my script because it opens the files in the background. I changed the script so that the files are opened in the foreground like normal files. And suddenly, the hyperlinks appear in the generated PDF! I have to check this with my other corrupt files.

                So it makes a difference if the file is opened in the background or not. I thought the only difference is that opening the file in the background is a little bit faster because there is less view rendering. But I have not thought there are any functional differences. I will do some tests with my "real" files, if they are fine too.

                • 5. Re: Script export to PDF: *some* hyperlinks are missing
                  John Hawkinson Level 5

                  Because of its similarity to other bugs that are causing serious serious problems for people (e.g. broken background PDF exports), I strongly urge you to report this bug to Adobe, because it might help in tracking down some of the others (for which there is no such "convenient" workaround).

                   

                  I agree it is unlikely, but 3rd party plugins have been known to cause all kinds of problems even when you think they are not doing anything. I would encourage you to test this behavior with the plugin removed/siabled, even though I agree it does not sound like it is related.

                  • 6. Re: Script export to PDF: *some* hyperlinks are missing
                    ikitzmann Level 1
                    Because of its similarity to other bugs that are causing serious serious problems for people (e.g. broken background PDF exports), I strongly urge you to report this bug to Adobe, because it might help in tracking down some of the others (for which there is no such "convenient" workaround).

                    I have filled a bug report.

                    I agree it is unlikely, but 3rd party plugins have been known to cause all kinds of problems even when you think they are not doing anything. I would encourage you to test this behavior with the plugin removed/siabled, even though I agree it does not sound like it is related.

                    I will try this, just to be sure that it is not the plugin. But I expect that there is no difference because the plugin is written by ourself and so we do not "think" it does not anything in this testcase, we are nearly sure.

                     

                     

                    In the meantime, I have tried also to open the files in the foreground in my "real" script with the "real" files and the links appear there too. But this will need some deeper tests before I will trust the results. But it looks promising. I will definitely avoid to process documents in the background in future scripts except when someone finds a way to prevent these bugs.

                     

                    So, thanks John. Your questions brought me to a workaorund which seems to work by now. It is not a "solution" but I can live with this for the moment.