Olivier Chenot

pdf merge ok with 4.32 release but ko with 4.42

2 posts in this topic

Hi,

I face something very weird. When I use a groovy pdf merge function, it works perfectly well with scriptMaster 4.32 release but fails with 4.42 one.

Returned error is "ERROR: the document has no pages".

The script : you can find it on french forum : https://www.fmsource.com/topic/45116-scriptmaster-manipulation-de-documents-pdf/#comment-262760

// iText_ConcatenatePDF ( fm_fileList ; fm_outputPath ; fm_setCompression )
// 20150130 Clem
// v1.0
// Concatène une liste de documents pdf.
//
// ***** PARAMETERS *****
// fm_fileList			: Une liste de chemins menant à des documents PDF.
// fm_outputPath		: Le chemin de sortie du document final.
// fm_setCompression    : Active la compression du flux de données. Valeurs acceptées :
//                        Pour activer la compression    : 1, TRUE, true, True, "1", "true", "TRUE".
//				          Pour désactiver la compression : toute autre valeur y compris "". 
// Remarque: Activer la compression génèrera automatiquement un PDF de version 1.5 //
// ***** DEPENDENCIES ***** // iText.jar // http://sourceforge.net/projects/itext/files/iText//// ********************************************************************************************************************

import com.itextpdf.text.Document
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.PdfSmartCopy
import com.itextpdf.text.pdf.PdfWriter

try {
    def fileArray = fm_fileList.tokenize("\n")
    if (fileArray.empty) throw new Exception("Empty file list.")

    // check for pdf document
    fileArray.each {
        f =  new File(it)
        if ( ! (f.exists() && f.getName().toLowerCase().endsWith(".pdf"))) throw new IOException ("File \"${f}\" not found as file or resource or is not a PDF document.")
    }

    // compression
    try {
        setCompression = fm_setCompression.toBoolean()
    } catch (Exception e) {
        setCompression = false
    }

    // merge pdf documents
    try {
        // Document implements DocListener, IAccessibleElement
        new Document().with { doc ->
            // PdfSmartCopy extends PdfCopy extends PdfWriter extends DocWriter implements PdfViewerPreferences, PdfEncryptionSettings, PdfVersion, PdfDocumentActions, PdfPageActions, PdfRunDirection, PdfAnnotations
            copy = new PdfSmartCopy(doc, new FileOutputStream(fm_outputPath))

            if ( setCompression) {
                // PdfWriter extends DocWriter implements PdfViewerPreferences, PdfEncryptionSettings, PdfVersion, PdfDocumentActions, PdfPageActions, PdfRunDirection, PdfAnnotations
                copy.pdfVersion = PdfWriter.VERSION_1_7
                copy.setFullCompression() // throws DocumentException
            }
            open() // Document
            fileArray.each { f ->
                new PdfReader(f).with {
                    for (page in 1..getNumberOfPages()) {
                        copy.addPage(copy.getImportedPage(it, page))
                    }
                    copy.freeReader(it) // throws IOException
                    close() // PdfReader close
                }
            }
            //copy.close() // PdfCopy close
            close() // Document close
        }
        return 1
    } catch (Exception e){
        return("ERROR: $e.message")
    } finally{
        copy.close()
    }
} catch (Exception e) {
    return ("ERROR: $e.message")
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now