当我尝试在热敏打印机上打印图像时,为什么JavaFX WebView print()会引发错误?

时间:2018-10-26 13:48:03

标签: java javafx printing webview

当我尝试在GPrinter热敏打印机上打印包含条形码WebView的javaFX <img>时遇到问题。没有条形码<img>没问题,但是有了条形码<img>时出现以下错误并弄乱了我的打印件:

java.lang.IllegalStateException: unmanaged resource freed from pool J2D Texture Pool
    at com.sun.prism.impl.BaseResourcePool.resourceFreed(BaseResourcePool.java:463)
    at com.sun.prism.impl.ManagedResource.dispose(ManagedResource.java:127)
    at com.sun.prism.impl.BaseTexture.dispose(BaseTexture.java:297)
    at com.sun.javafx.webkit.prism.WCImageImpl.draw(WCImageImpl.java:100)
    at com.sun.javafx.webkit.prism.WCGraphicsPrismContext$7.doPaint(WCGraphicsPrismContext.java:835)
    at com.sun.javafx.webkit.prism.WCGraphicsPrismContext$Composite.paint(WCGraphicsPrismContext.java:1500)
    at com.sun.javafx.webkit.prism.WCGraphicsPrismContext$Composite.paint(WCGraphicsPrismContext.java:1485)
    at com.sun.javafx.webkit.prism.WCGraphicsPrismContext.drawImage(WCGraphicsPrismContext.java:842)
    at com.sun.webkit.graphics.GraphicsDecoder.drawImage(GraphicsDecoder.java:427)
    at com.sun.webkit.graphics.GraphicsDecoder.decode(GraphicsDecoder.java:184)
    at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:92)
    at com.sun.webkit.WebPage.print(WebPage.java:1863)
    at javafx.scene.web.WebEngine$Printable$Peer.renderContent(WebEngine.java:1687)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2053)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1945)
    at com.sun.prism.j2d.print.J2DPrinterJob$J2DPageable.printNode(J2DPrinterJob.java:1023)
    at com.sun.prism.j2d.print.J2DPrinterJob$J2DPageable.print(J2DPrinterJob.java:1013)
    at sun.print.RasterPrinterJob.printPage(Unknown Source)
    at sun.print.RasterPrinterJob.print(Unknown Source)
    at com.sun.prism.j2d.print.J2DPrinterJob$PrintJobRunnable.run(J2DPrinterJob.java:747)
    at java.lang.Thread.run(Unknown Source)

我使用的代码如下,当我尝试打印的html不包含任何代码时,它可以正常工作:

public void actualPrint(Printer printer,String cont){
System.out.println("======================================");
Platform.runLater(new Runnable(){
    public void run(){
        WebView v = new WebView();
        WebEngine engine = v.getEngine();
        engine.getLoadWorker().stateProperty().addListener(
            (ChangeListener<State>) (ov, oldState, newState) -> {
                if (newState == State.SUCCEEDED) {
                    try{
                        Printer pr = printer;
                        if(pr==null){
                            System.out.println("GOT THE DEFAULT ONE");
                            pr = Printer.getDefaultPrinter();
                        }

                        PrinterJob job = PrinterJob.createPrinterJob();
                        job.setPrinter(pr);
                        PageLayout pl = null;

                        try{
                            pl =job.getPrinter().createPageLayout(printer.getPrinterAttributes().getDefaultPaper(), 
PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
                        }
                        catch(Exception e){
                            pl = job.getPrinter().createPageLayout(job.getJobSettings().getPageLayout().getPaper(), PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
                        }


                        if (job != null) {
                            job.getJobSettings().setPageLayout(pl);
                            engine.print(job);
                            job.endJob();
                        }
                    } catch(Exception b) {
                        b.printStackTrace();
                    }

                }else if(newState == State.FAILED) {
                    System.out.print("load content failed");
                }
                });
        engine.loadContent(cont);
        }
    });
 }

0 个答案:

没有答案