2013-12-18 23 views
0

我現在已經遇到了這個問題。接近我的「校對」腳本的末尾,InDesign中當前打開的文檔將被導出到兩個不同的.pdf文件。第一個是密碼保護,第二個不是。我似乎對後者沒有任何問題,但前者通常會以某種方式被破壞,無法由任何PDF閱讀器打開,包括Acrobat本身。下面的代碼塊,做出口(這是運行的本身,順便說一句):InDesign ExtendScript腳本有時會在導出期間創建損壞的PDF

/********** 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。如果有任何損壞的文件格式,我一直無法辨別它。有人有想法嗎?

+0

我的想法是:腐敗是什麼樣子?文件中是否還有數據(不是零K)?如果有數據,如果將文件擴展名更改爲「.txt」並在TextWrangler中打開,它看起來像什麼? – CRGreen

+0

對不起,我從來沒有真正嘗試過關於看到腐敗看起來像什麼的建議。相反,我創建了一個解決方法,我馬上發佈。 – Sturm

回答

0

它終於打我了,如果損壞的文件無法在Acrobat中打開,那麼爲什麼不在文件創建後測試呢?所以我創建了一個導出PDF文件並嘗試在Acrobat中打開它的循環。如果打開狀態正常,則會打印並關閉該文件,並返回「真實」消息。如果無法這樣做,則會向腳本返回「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; 
}); 

我希望這個信息可能對遇到類似問題的其他人有用。當然,這不是很好,但它確實完成了工作。