Problem with Close PDF in Workflow

Hi,

I have error when I want to close PDF. My code:

var PDF1 = Watch.GetPDFEditObject();
PDF1.Open(Watch.GetJobFileName(),false);
var oPages = PDF1.Pages();

var nPagesNumber = oPages.Count();

var nNumberOfElements = nPagesNumber/4;

var nCurrentElement = 0;
while( nCurrentElement < nNumberOfElements){

    var sCustomerNumber = oPages.Item(nCurrentElement*3).ExtractText2(2.0, 0.6, 4.0, 0.86);

    ++nCurrentElement;

}

oPages = null;
PDF1.Save(false);
PDF1.Close();

When I comment line when i declare sCustomerNumber I dont have error. But when i Want to use Item method i have error:

W3602 : Error 0 on line 82, column 1: AlambicEdit.AlambicEditPDF.1: Unable to close PDF file: there are still 37 pages opened.

My pdf has over 300 pages.

In documentation:

http://help.objectiflune.com/files/EN/alambicedit-api/AlambicEdit.html#9ed7a4a27a52e4f1df95c6bf427e0a3b

Closes the PDF file. If changes were made but not saved, they will be silently lost. All IPage objects must be released before closing a PDF.

I think here is problem but how can I release object?

Somobody has any ideas?

Hi,

Seems to be a JavaScript issue. Would like an OL dev to chime in about this.

Does this work? (VBScript)

Dim PDF1, oPages, nPagesNumber, nNumberOfElements, nCurrentElement

Set PDF1 = Watch.GetPDFEditObject
PDF1.Open Watch.GetJobFileName,false
Set oPages = PDF1.Pages()

nPagesNumber = PDF1.Pages.Count

nNumberOfElements = nPagesNumber / 4

nCurrentElement = 0
do while nCurrentElement < nNumberOfElements
Dim sCustomerNumber

sCustomerNumber = oPages.Item(nCurrentElement*3).ExtractText2(2.0, 0.6, 4.0, 0.86)
nCurrentElement = nCurrentElement + 1
loop

PDF1.Save(false)
PDF1.Close()

Regards,

S

Thanks for answer.

It should work, but I have much more JS code in this script, and I cant separate this part of code.

So I prefer use JavaScript rather than VB.

Hmm… so still I’m looking for solution for JS.

I thought as much. The reason why I code in Workflow using VBScript instead of JavaScript is because I always get headaches using JavaScript. That and the fact that Workflow and Designer use different versions of ECMAScript. (I think 5 for Workflow and 6 for Designer)

I’m sure one of the OL devs will respond here too.

Regards,

S

can confirm that as well, it seems the script cannot release the pdf page object, may be a bug with the Workflow js interface.
Best you open a ticket with your local OL Support team.

I think this is a JavaScript Garbage Collection issue where some page objects are not immediately released after you null them. You could probably workaround it by forcing Garbage collection before closing the PDF with CollectGarbage()

var PDF1 = Watch.GetPDFEditObject();
    PDF1.Open(Watch.GetJobFileName(),false);
    var oPages = PDF1.Pages();
    var nPagesNumber = oPages.Count();
    var nNumberOfElements = nPagesNumber/4;
    var nCurrentElement = 0;
    while( nCurrentElement &lt; nNumberOfElements){
            var sCustomerNumber = oPages.Item(nCurrentElement*3).ExtractText2(2.0, 0.6, 4.0, 0.86);
            ++nCurrentElement;
    }
    oPages = null;
    PDF1.Save(false);
    CollectGarbage();
    PDF1.Close();

Alternatively, you could also catch the error and just simply carry on:

var PDF1 = Watch.GetPDFEditObject();
    PDF1.Open(Watch.GetJobFileName(),false);
    var oPages = PDF1.Pages();
    var nPagesNumber = oPages.Count();
    var nNumberOfElements = nPagesNumber/4;
    var nCurrentElement = 0;
    while( nCurrentElement &lt; nNumberOfElements){
            var sCustomerNumber = oPages.Item(nCurrentElement*3).ExtractText2(2.0, 0.6, 4.0, 0.86);
            if(sCustomerNumber){
                   try{
                         ++nCurrentElement;
                    }catch(e){
                         Watch.Log("Garbase Collection issue on  "+ nCurrentElement+"... continue",2);
                    }
            }
    }
    oPages = null;
    PDF1.Save(false);
    try{
        PDF1.Close();
    }catch(e){
        Watch.log("Garbage Collection issue closing PDF...continue",2);
    }

mariuszwoch,

What you are reporting is a known issue, our developers recommend to use VBScript in this scenario.

There an example of how to bypass the error message, but is not safe because it may crash the engine.

var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var sTempName = Watch.ExpandString("%f.tmp");
var sTempPath = Watch.ExpandString("%t");
var objOutputFile = objFSO.GetFolder(sTempPath).CreateTextFile(sTempName);
var sLine = "";

var objPDF = Watch.GetPDFEditObject();
objPDF.Open(Watch.GetJobFileName(), false);

var objPageText;
var objPages = objPDF.Pages();

for(var i=0; i<objPages.Count(); i++) {
  objOutputFile.writeline('###Page ' + i);
  objPageText = objPages.Item(i).ExtractText2(0, 0, 8.3, 11.7);
  if (objPageText) {
    try {
      objOutputFile.writeline(objPageText);
    } catch(e) {
      Watch.log("Ran into issue on page "+i+"... moving on",3);
    }
  }
  objPagetext = null;
}

objPages = null;
try {
  objPDF.Close();
} catch(e) {
  Watch.log("Ran into issue closing PDF... moving on",3);
}
objOutputFile.Close();