InDesign ExtendScript脚本有时会在导出期间创建损坏的PDF

时间:2013-12-18 21:52:18

标签: javascript pdf corruption adobe-indesign extendscript

我现在已经有这个问题了。接近我的“校对”脚本的末尾,InDesign中当前打开的文档将导出为两个不同的.pdf文件。第一个是密码保护,而第二个不是。我似乎对后者没有任何问题,但前者经常以某种方式被破坏,并且不能由任何PDF阅读器打开,包括Acrobat本身。这是导出的代码块(可单独运行,顺便说一下):

/********** BEGIN PDF EXPORTING **********/

// First, let's create and set PDF export preferences.
// This begins with creating a temporary preset if it doesn't already exist.
// This preset will be used for both the Proof page and the Cover sheet.
var tempPreset = app.pdfExportPresets.item("tempPreset");
try
{
    tempPreset.name;
}
catch (eNoSuchPreset)
{
    tempPreset = app.pdfExportPresets.add({name:"tempPreset"});
}
with (tempPreset)
{
    acrobatCompatibility = AcrobatCompatibility.ACROBAT_5;
    bleedMarks = false;
    colorBars = false;
    colorBitmapCompression = BitmapCompression.AUTO_COMPRESSION;
    colorBitmapQuality = CompressionQuality.MAXIMUM;
    colorBitmapSampling = Sampling.BICUBIC_DOWNSAMPLE;
    colorBitmapSamplingDPI = 300;
    compressTextAndLineArt = true;
    cropImagesToFrames = true;
    cropMarks = false;
    exportGuidesAndGrids = false;
    exportNonprintingObjects = false;
    exportReaderSpreads = false;
    exportWhichLayers = ExportLayerOptions.EXPORT_VISIBLE_PRINTABLE_LAYERS;
    generateThumbnails = false;
    grayscaleBitmapCompression = BitmapCompression.AUTO_COMPRESSION;
    grayscaleBitmapQuality = CompressionQuality.MAXIMUM;
    grayscaleBitmapSampling = Sampling.BICUBIC_DOWNSAMPLE;
    grayscaleBitmapSamplingDPI = 300;
    includeBookmarks = false;
    includeHyperlinks = false;
    includeSlugArea = false;
    includeStructure = true;
    monochromeBitmapCompression = MonoBitmapCompression.CCIT4;
    monochromeBitmapSampling = Sampling.BICUBIC_DOWNSAMPLE;
    monochromeBitmapSamplingDPI = 1200;
    omitBitmaps = false;
    omitEPS = false;
    omitPDF = false;
    optimizePDF = true;
    pageInformationMarks = false;
    pageMarksOffset = 0.0833;
    pdfMarkType = MarkTypes.DEFAULT_VALUE;
    printerMarkWeight = PDFMarkWeight.P25PT;
    registrationMarks = false;
    standardsCompliance = PDFXStandards.NONE;
    subsetFontsBelow = 100;
    thresholdToCompressColor = 450;
    thresholdToCompressGray = 450;
    thresholdToCompressMonochrome = 1800;
    useDocumentBleedWithPDF = false;
}
currentProcess.text = "PDF export preferences"; progressWin.show();
progressIndividual.value++; if (aProducts.length > 1) {progressOverall.value++;}

// Now let's actually set the export preferences.  These are for the proof page.
with (app.pdfExportPreferences)
{
    pageRange = proofRange;
    useSecurity = true;
    disallowChanging = true;
    disallowCopying = false;
    disallowDocumentAssembly = true;
    disallowExtractionForAccessibility = false;
    disallowFormFillIn = true;
    disallowHiResPrinting = true;
    disallowNotes = true;
    disallowPlaintextMetadata = true;
    disallowPrinting = false;
    changeSecurityPassword = "sky";
    if (multiColor)
    {
        pageRange = colorTable.toString();
    }
    if (currentProduct.pLabel != "")
    {
        pageRange += "," + labelPage.name;
    }
}
currentProcess.text = "Exporting PDF proof page"; progressWin.show();
progressIndividual.value++; if (aProducts.length > 1) {progressOverall.value++;}

// Before exporting the Proof page(s), hide the color bar on multicolor products.
if (multiColor) {document.layers.item("COLOR BAR").visible = false;}

// Then we save the proof page.
document.exportFile(ExportFormat.PDF_TYPE, File(jobFolder.toString() + "/" + saveName + ".pdf"), false, tempPreset);

当它偶尔产生损坏的PDF时,我想也许是我们不太理想的网络结构导致问题,所以我尝试将PDF文件导出到本地硬盘驱动器而不是直接导出到网络,然后将文件移动到网络。因此,上面代码块中的最后一行替换为:

// First, to the local HDD.
document.exportFile(ExportFormat.PDF_TYPE, File("~/Documents/" + saveName + ".pdf"), false, tempPreset);
$.sleep(1000);
File("~/Documents/" + saveName + ".pdf").copy(File(jobFolder.toString() + "/" + saveName + ".pdf"));
$.sleep(1000);
File("~/Documents/" + saveName + ".pdf").remove();

为了以防万一,我甚至在那些1秒延迟中添加了。可悲的是,这没有帮助。我时不时地得到一个损坏的PDF。如果损坏的文件有任何模式,我无法辨别它。有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

最后打击了我,如果在Acrobat中无法打开损坏的文件,为什么不在文件创建后测试?所以我创建了一个循环导出PDF文件并尝试在Acrobat中打开它。如果打开正常,则打印并关闭文件,返回“true”消息。如果无法执行此操作,则会向脚本返回“false”消息。然后循环重复,只要该消息是“假”。虽然不是一个很好的解决潜在原因(无论它可能是什么),但它至少是一个解决方案,可以很好地满足我们的需求。诀窍是,因为我们使用Mac,我们必须通过AppleScript路由消息,而不是使用BridgeTalk直接与Acrobat通信。

以下是主要InDesign脚本中的代码片段,它通过PDF检查循环:

// Then we save the proof page.
// The loop is to make sure that the file was saved properly.
var validFile = false; // Flag that states whether or not the file is corrupted after saving.
var rString; // String returned from Acrobat that should be either "true" or "false".
var testAndPrintFile = File("~/Documents/testAndPrint.applescript"); // The applescript file that calls Acrobat and runs a folder-level script.
var pdfFile; // A String of the filename & path that will be passed to through the applescript file to Acrobat.
var pdfArray = new Array(4); // An array to send to Acrobat.  [0] is the PDF filename as a String,
                             // [1] is duplex if true, [2] is the printer name, and [3] is to enable printing.
if (multiTwoSided || twoPages) pdfArray[1] = "true";
    else pdfArray[1] = "false";
pdfArray[2] = localPrinter;
pdfArray[3] = "true";
while (!validFile)
{
    $.writeln("If this message is seen more than once, then the Proof PDF was corrupted.");
    try
    {
        document.exportFile(ExportFormat.PDF_TYPE, File(jobFolder.toString() + "/" + saveName + ".pdf"), false, tempPreset);
    }
    catch (e)
    {
        alert("Could not save the Proof PDF.  Please close any open copies of the Proof PDF, then save and print it manually.");
    }
    pdfFile = jobFolder.toString() + "/" + saveName + ".pdf";
    pdfArray[0] = pdfFile;
    $.writeln("pdfArray contains: " + pdfArray);
    try
    {
        rString = app.doScript(testAndPrintFile, ScriptLanguage.APPLESCRIPT_LANGUAGE, pdfArray);
        validFile = rString == "true";
        // validFile = true;
        $.writeln("validFile is " + validFile);
        if (!validFile)
        {
            alert("It seems that the file " + unescape(pdfArray[0]) + " is corrupted.  Will try to export it again.");
        }
    }
    catch (e)
    {
        $.writeln("ERROR at line number " + e.line);
        $.writeln(e.description);
        throw new Error("ERROR at line number " + e.line + "\n" + e.description);
    }
}

此循环调用的testAndPrint.applescript文件:

set pdfFile to item 1 of arguments
set duplexed to item 2 of arguments
set printerName to item 3 of arguments
set printEnabled to item 4 of arguments
tell application "Adobe Acrobat Pro"
    set result to do script ("testAndPrint(\"" & pdfFile & "\", \"" & duplexed & "\", \"" & printerName & "\", \"" & printEnabled & "\");")
end tell
return result

最后,当Acrobat启动时加载到内存中的文件夹级Javascript文件,准备将其函数调用到上面的Applescript文件中:

var testAndPrint = app.trustedFunction(function (fName, duplexed, sPrinterName, bEnablePrinting)
{
    var success = true;
    app.beginPriv();
    console.println("fName is " + unescape(fName));
    console.println("sPrinterName is " + sPrinterName);
    try
    {
        var printDoc = app.openDoc(unescape(fName));
        var pp = printDoc.getPrintParams();
        if (duplexed == "true") pp.DuplexType = pp.constants.duplexTypes.DuplexFlipLongEdge;
            else pp.DuplexType = pp.constants.duplexTypes.Simplex;
        pp.printerName = sPrinterName;
        pp.interactive = pp.constants.interactionLevel.silent;
        pp.pageHandling = pp.constants.handling.none;
        if (bEnablePrinting == "true") printDoc.print({bUI: false, bSilent: true, bShrinkToFit: false, printParams: pp});
        printDoc.closeDoc(true);
    }
    catch (e)
    {
        console.println("ERROR at line number " + e.lineNumber);
        console.println(e.message);
        success = false;
    }
    app.endPriv();
    console.println("success is " + success);
    return success;
});

我希望这些信息可能对遇到类似问题的其他人有用。当然,它并不漂亮,但它确实完成了工作。