没有足够的资源可用于完成此操作

时间:2013-12-18 15:58:02

标签: java groovy

我们正在尝试打印批次(我们每天打印超过1500个PDF文件)。我们在Windows 2003 Server上使用带有PDFBox的groovy,内存为4 GB,可用空间超过250 GB。

我们监控服务器,我们没有任何内存峰值,工作只使用40%。我们收到此消息:

java.awt.print.PrinterException: Not enough resources are available to complete this operation.
    at sun.awt.windows.WPrinterJob._startDoc(Native Method)
    at sun.awt.windows.WPrinterJob.startDoc(WPrinterJob.java:1249)
    at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1371)
    at sun.print.RasterPrinterJob$print.call(Unknown Source)
    at impressionPDF.run(impressionPDF.groovy:155)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:627)
    at groovy.lang.Script.evaluate(Script.java:219)
    at groovy.lang.Script$evaluate.callCurrent(Unknown Source)
    at JAAMSKZL.TXT.printPDF(JAAMSKZL.TXT.groovy:19)
    at sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:361)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
    at JAAMSKZL.TXT$_run_closure2.doCall(JAAMSKZL.TXT.groovy:83)
    at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
    at groovy.lang.Closure.call(Closure.java:412)
    at groovy.lang.Closure.call(Closure.java:425)
    at groovy.sql.Sql.eachRow(Sql.java:953)
    at groovy.sql.Sql.eachRow(Sql.java:906)
    at groovy.sql.Sql.eachRow(Sql.java:843)
    at groovy.sql.Sql$eachRow.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
    at JAAMSKZL.TXT.run(JAAMSKZL.TXT.groovy:70)
    at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
    at groovy.lang.GroovyShell.run(GroovyShell.java:229)
    at groovy.lang.GroovyShell.run(GroovyShell.java:159)
    at groovy.ui.GroovyMain.processOnce(GroovyMain.java:550)
    at groovy.ui.GroovyMain.run(GroovyMain.java:337)
    at groovy.ui.GroovyMain.process(GroovyMain.java:323)
    at groovy.ui.GroovyMain.processArgs(GroovyMain.java:120)
    at groovy.ui.GroovyMain.main(GroovyMain.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
    at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)

这是代码:

    import java.awt.Dimension
import java.awt.print.*
import javax.print.*
import javax.print.attribute.*
import javax.print.attribute.standard.*
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPageable
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.pdmodel.common.PDRectangle
    Attribute traduireMedia(String nomImprimante, String nomMedia) {
        // Chercher l'imprimante par son nom.
        AttributeSet aset = new HashAttributeSet();
        aset.add(new PrinterName(nomImprimante, null));
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, aset);
        if (services.length == 0) {
            throw new RuntimeException("Imprimanté inconnue " + nomImprimante);
        }
        PrintService imprimante = services[0];        
        Media[] mediaList = (Media[]) imprimante.getSupportedAttributeValues(Media.class, null, null);
        Media objetMedia = null;
        if (mediaList != null) {
            for (Media media : mediaList) {
                if (nomMedia.equalsIgnoreCase(media.toString().trim())) {
                    objetMedia = media;
                    break;
                }
            }
        }
        if (objetMedia == null) {
            throw new RuntimeException(
                    String.format("L'imprimante %s ne support pas le média \"%s\".",
                            nomImprimante, nomMedia));
        }
        return objetMedia;
    }
class OGIPageable extends PDPageable {
    PrinterJob myJob;
    PDDocument myDocument;
    float myXMargin;
    float myYMargin;
    public OGIPageable() throws PrinterException {
        super(null);
        this.myXMargin = 0;
        this.myYMargin = 0;
    }
    public OGIPageable(PDDocument document, PrinterJob job, float xMargin, float yMargin) throws PrinterException {
        super(document,job);
        this.myJob = job;
        this.myDocument = document;
        this.myXMargin = xMargin;
        this.myYMargin = yMargin;
    }
    public PageFormat getPageFormat(int i) throws IndexOutOfBoundsException {
        PageFormat format = myJob.defaultPage();
        List<PDPage> allPages = myDocument.getDocumentCatalog().getAllPages(); 
        PDPage page = allPages.get(i); // can throw IOOBE
        PDRectangle cropRect = page.findCropBox()
        if ((myXMargin > 0) && (myYMargin > 0) && (cropRect != null)) {
            cropRect.move((float)myXMargin*-1, myYMargin)
            page.setCropBox(cropRect);
        }
        Dimension crop = cropRect.createDimension();
        Dimension media = page.findMediaBox().createDimension();

        // Center the ImageableArea if the crop is smaller than the media
        double diffWidth = 0.0;
        double diffHeight = 0.0;
        if (!media.equals(crop)) {
            diffWidth = (media.getWidth() - crop.getWidth()) / 2.0;
            diffHeight = (media.getHeight() - crop.getHeight()) / 2.0;
        }
        int vOrientation = PageFormat.PORTRAIT;
        if(media.getWidth() > media.getHeight()) {
            vOrientation = PageFormat.LANDSCAPE;
        }

        format.setOrientation(vOrientation);
        Paper paper = format.getPaper();
        if (vOrientation == PageFormat.LANDSCAPE) {
             paper.setImageableArea(diffHeight, diffWidth, crop.getHeight(), crop.getWidth());
             paper.setSize(media.getHeight(), media.getWidth());
        } else {
             paper.setImageableArea(diffWidth, diffHeight, crop.getWidth(), crop.getHeight());
             paper.setSize(media.getWidth(), media.getHeight());
        }
        format.setPaper(paper);
        return format;
    }     
}
PDDocument document = null;
try {    
/*    println "loading file "+fichierPDF*/
    document = PDDocument.load(fichierPDF)
    if (document.isEncrypted()) {
      /*  println "decrypt PDF..."*/
        document.decrypt(password)
    }  
    PrintServiceAttributeSet serviceAttributeSet = new HashPrintServiceAttributeSet()
    serviceAttributeSet.add(new PrinterName(imprimante, null))  
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, serviceAttributeSet)
    if (printService.length == 1) {
        float xMargin = 0.0
        float yMargin = 0.0
        MediaPrintableArea[] mediaPrintArea = printService[0].getSupportedAttributeValues(MediaPrintableArea.class, null, null)
        if ((mediaPrintArea != null) && (mediaPrintArea.length > 0) && (type.equalsIgnoreCase('PDF'))) {
            xMargin = mediaPrintArea[0].getX(MediaPrintableArea.INCH) * 72
            yMargin = mediaPrintArea[0].getY(MediaPrintableArea.INCH) * 72
        }
       /* println "margin x:$xMargin y:$yMargin (pts)"*/
        File input = new File(fichierPDF)
        PrinterJob printJob = PrinterJob.getPrinterJob()
        printJob.setJobName(input.name)
        printJob.setPageable(new OGIPageable(document, printJob, xMargin, yMargin))
        printJob.setPrintService(printService[0]);        
        // Paramètres d'impression
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet()
        aset.add(new Copies(iCopies))
        if (isRectoVersion) {
            aset.add(Sides.DUPLEX)
        }
        if (media) {
            aset.add(traduireMedia(imprimante,media))
            println "media "+ media +" found on printer "+imprimante
        }

        i = 4
        tentativeImpression = true
        while (tentativeImpression) {
            i--
            try {
                println "sending document $input.name to printer $imprimante"
                printJob.print(aset)
                tentativeImpression = false;
            } catch (Throwable exx) {
                if (i == 0) {
                    exx.printStackTrace()
                    System.exit(5)
                }
                println "$exx.message"
            }            
        }        
        println "closing document $input.name"
        document.close();
    } else {
        println ("print service $imprimante not found")
        System.exit(5)
    }
} catch (Throwable t) {
    t.printStackTrace()
    System.exit(5)
}

这是调用PDFbox(groovy文件)的代码:

import groovy.sql.Sql

copyFile = { File src,File dest->
  def input = src.newDataInputStream()
  def output = dest.newDataOutputStream()
  output << input
  input.close()
  output.close()
}
def printPDF(String inFile, String inPrinter, String inTray, String inType,Boolean inRecto) {
  iCopies = 1
  isRectoVersion = inRecto
  imprimante = inPrinter
  media = inTray
  fichierPDF = inFile
  type = inType
  evaluate(new File(home + "/BACKEND/scripts/impressionPDF.groovy"))
}
! Localisation du backend et de la configuration:
home = "&ROOT#/&ENV#"
config = "&CONFIG#"
libPath = home + "/BACKEND/lib"

! Initialisation de l'environnement JAVA:
evaluate(new File(home + "/BACKEND/scripts/bootstrap.groovy"))

try {
  db = Sql.newInstance("&DATABASE_URL#", "&DATABASE_USERNAME#", "&DATABASE_PASSWORD#", 'oracle.jdbc.driver.OracleDriver')

  db.call("alter session set NLS_DATE_FORMAT = 'yyyy.mm.dd'")
  verdoc_id = ""
  sql_decsheet = "select giv.verdoc_id, " +
    "printer, " +
    "pdf_file_id, " +
    "type_document, " +
    "tray, " +
    "givd.fichier_destype," +
    "from   dual"
  db.eachRow(sql_decsheet) { row ->
    fichierPDF = &DVIMAGE_REP_ENTREE# +"\\" + row.pdf_file_id

if (row.fichier_destype == 'RTF' && ("&CONFIG#" == 'SNA' || "&CONFIG#" == 'UQU')){
      printPDF(fichierPDF, row.imprimante, row.plateau_imprimante,row.fichier_destype,true);
    }else{
      printPDF(fichierPDF, row.imprimante, row.plateau_imprimante,row.fichier_destype,false);
    }
  }
} catch (Exception ex) {
  ex.printStackTrace()
  System.exit(5)
}

0 个答案:

没有答案