Jump to content
Olivier Chenot

pdf merge ok with 4.32 release but ko with 4.42

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.