Product DocsMenu

Coveo Platform 6.5 >
Developer Help > Open Converter API > Open Converters > Samples > Sample 4 - Convert Document to PDF Format

Sample 4 - Convert Document to PDF Format

Open Converter Sample #4 (JScript Version)

// ****************************************************************************
//  This open converter sample converts a document from an unsupported format
//  (such as PostScript documents) to the PDF format using the Ghostscript
//  tool.
//
//  Required parameter:
//    - GhostScriptExecutablePath: The Ghostscript tool executable path.
//                                 Example: c:\gs\gs7.04\bin\gswin32c.exe
// ****************************************************************************

// Get the shell object from the script global vriables.
var shellObject = null;
try {
    shellObject = CustomConversion.GetGlobalVariable("shellObject");
} catch (e) {
    // Lock here to make sure to instanciate only one instance of the shell object.
    Mutex.AcquireLock("WScriptShellMutex");

    // Retry to get the shell object, it may have been instanciated by another script.
    try {
        shellObject = CustomConversion.GetGlobalVariable("shellObject");
    } catch (e) {
        // Create the shell object and add it to the global variables.
        CustomConversion.Trace("Creating the system shell object...", 0);
        shellObject = new ActiveXObject("WScript.Shell");
        CustomConversion.SetGlobalVariable("shellObject", shellObject);
    }

    // Release our mutex in order for other scripts to retrieve the shell object.
    Mutex.ReleaseLock();
}

// Save the document to convert to a temporary file.
var inputFileName  = CustomConversion.InputDocument.TemporaryFileName();
var outputFileName = CustomConversion.OutputDocument.TemporaryFileName();
CustomConversion.InputDocument.SaveToFile(inputFileName);

// Lock here to instantiate GhostScript executable one at a time.
Mutex.AcquireLock("GhostScriptMutex");

try {
    // Perform the document format conversion.
    var ghostScriptCommandLine = '\"' + CustomConversion.GetParameter("GhostScriptExecutablePath") + '\"';
    ghostScriptCommandLine += " -sOutputFile=\"" + outputFileName + '\"';
    ghostScriptCommandLine += " -q -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dNOPROMPT ";
    ghostScriptCommandLine += '\"' + inputFileName + '\"';
    shellObject.Run(ghostScriptCommandLine, 0, true);

    // The document format was successfully changed to PDF. Convert this document.
    CustomConversion.OutputDocument.LoadFromFile(outputFileName);
} catch (e) {
    // Unable to change the document format. Reject the document and report the error.
    DocumentInfo.IsValid = false;
    CustomConversion.Trace("Error: " + e.description, 2);
}

// Release our mutex in order for other scripts to be able to use the GhostScript executable.
Mutex.ReleaseLock();

// Clear our temporary files.
try {
    CustomConversion.DeleteFile(inputFileName);
} catch (e) {
}
try {
    CustomConversion.DeleteFile(outputFileName);
} catch (e) {
}

Open Converter Sample #4 (VBScript Version)

'****************************************************************************
' This open converter sample converts a document from an unsupported format
' (such as PostScript documents) to the PDF format using the Ghostscript
' tool.
'
' Required parameter:
'   - GhostScriptExecutablePath: The Ghostscript tool executable path.
'                                Example: c:\gs\gs7.04\bin\gswin32c.exe
'****************************************************************************

Option Explicit
On Error Resume Next

' Get the shell object from the script global vriables.
Dim shellObject: Set shellObject = CustomConversion.GetGlobalVariable("shellObject")
If Not IsObject(shellObject) Then
    ' Lock here to make sure to instanciate only one instance of the shell object.
    Call Mutex.AcquireLock("WScriptShellMutex")

    ' Retry to get the shell object, it may have been instanciated by another script.
    Set shellObject = CustomConversion.GetGlobalVariable("shellObject")
    If Not IsObject(shellObject) Then
        ' Create the shell object and add it to the global variables.
        Call CustomConversion.Trace("Creating the system shell object...", 0)
        Set shellObject = CreateObject("WScript.Shell")
        Call CustomConversion.SetGlobalVariable("shellObject", shellObject)
    End If

    ' Release our mutex, so other scripts can retrieve the shell object.
    Call Mutex.ReleaseLock()
End If

' Save the document to convert to a temporary file.
Dim inputFileName: inputFileName = CustomConversion.InputDocument.TemporaryFileName
CustomConversion.InputDocument.SaveToFile(inputFileName)

' Lock here to instantiate GhostScript executable one at a time.
Call Mutex.AcquireLock("GhostScriptMutex")

' Perform the document format conversion.
Err = 0
Dim DOUBLE_QUOTE: DOUBLE_QUOTE = chr(34)
Dim outputFileName: outputFileName = CustomConversion.OutputDocument.TemporaryFileName
Dim ghostScriptCommandLine: ghostScriptCommandLine = CustomConversion.GetParameter("GhostScriptExecutablePath")
ghostScriptCommandLine = DOUBLE_QUOTE & ghostScriptCommandLine & DOUBLE_QUOTE
ghostScriptCommandLine = ghostScriptCommandLine & " -sOutputFile=" & DOUBLE_QUOTE & outputFileName & DOUBLE_QUOTE
ghostScriptCommandLine = ghostScriptCommandLine & " -q -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dNOPROMPT "
ghostScriptCommandLine = ghostScriptCommandLine & DOUBLE_QUOTE & inputFileName & DOUBLE_QUOTE
Call shellObject.Run(ghostScriptCommandLine, 0, True)

' Release our mutex in order for other scripts to be able to use the GhostScript executable.
Call Mutex.ReleaseLock()

If Err.Number = 0 Then
    ' The document format was successfully changed to PDF. Convert this document.
    CustomConversion.OutputDocument.LoadFromFile(outputFileName)
Else
    ' Unable to change the document format. Reject the document and report the error.
    DocumentInfo.IsValid = False
    Call CustomConversion.Trace("Error: " & Err.Description, 2)
End If

' Clear our temporary files.
Call CustomConversion.DeleteFile(inputFileName)
Call CustomConversion.DeleteFile(outputFileName)

See also: What are Open Converters?, How to Create an Open Converter, Setting Up an Open ConverterOpen Converter Samples