danfickle / openhtmltopdf

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!
https://danfickle.github.io/pdf-templates/index.html
Other
1.92k stars 359 forks source link

svg path tag problem #536

Open bomb12138 opened 4 years ago

bomb12138 commented 4 years ago
DEBUG 08-17 15:36:29,244 Could not complete request  (FrameworkServlet.java:954) 
java.lang.IllegalStateException: Don't use the main context as long as a copy is active!
    at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.checkNoCopyActive(PdfBoxGraphics2D.java:1033)
    at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.setTransform(PdfBoxGraphics2D.java:1133)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:565)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.CanvasGraphicsNode.primitivePaint(CanvasGraphicsNode.java:159)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at com.openhtmltopdf.svgsupport.PDFTranscoder$1.render(PDFTranscoder.java:257)
    at com.openhtmltopdf.pdfboxout.PdfBoxFastOutputDevice.drawWithGraphics(PdfBoxFastOutputDevice.java:1147)
    at com.openhtmltopdf.svgsupport.PDFTranscoder.transcode(PDFTranscoder.java:243)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
    at com.openhtmltopdf.svgsupport.BatikSVGImage.drawSVG(BatikSVGImage.java:201)
    at com.openhtmltopdf.pdfboxout.PdfBoxSVGReplacedElement.paint(PdfBoxSVGReplacedElement.java:70)
    at com.openhtmltopdf.pdfboxout.PdfBoxFastOutputDevice.paintReplacedElement(PdfBoxFastOutputDevice.java:277)
    at com.openhtmltopdf.render.displaylist.DisplayListPainter.paintReplacedElement(DisplayListPainter.java:167)
    at com.openhtmltopdf.render.displaylist.DisplayListPainter.paintReplacedElements(DisplayListPainter.java:149)
    at com.openhtmltopdf.render.displaylist.DisplayListPainter.paint(DisplayListPainter.java:266)
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.paintPageFast(PdfBoxRenderer.java:903)
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.writePDFFast(PdfBoxRenderer.java:624)
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPdfFast(PdfBoxRenderer.java:559)
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:477)
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:414)
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:396)
    at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:42)
    at com.atguigu.util.PdfUtil.toPdf(PdfUtil.java:66)
    at com.atguigu.controller.TeacherController.toPDF(TeacherController.java:69)
    at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
<html>
<head>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="main-svg" width="1163" height="545" style="" viewBox="0 0 1163 545">
    <defs id="defs-52f0ea">
        <g class="clips">
            <clipPath class="axesclip" id="clip52f0eax">
                <rect x="80" y="0" width="1003" height="545"></rect>
            </clipPath>
        </g>
    </defs>
    <g class="shapelayer">
        <path data-index="0" fill-rule="evenodd" d="M50.91,250H109.85V100H200.91Z" clip-path="url('#clip52f0eax')" style="opacity: 0.4; stroke: rgb(0, 0, 0); stroke-opacity: 0; fill: rgb(255, 182, 193); fill-opacity: 1; stroke-width: 0px;"></path>
    </g>
</svg>
</body>
</html>
public static void Pdf(HttpServletResponse response, String pdfFileName,String example) throws Exception {
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "filename=" + new String((pdfFileName + ".pdf").getBytes(), "iso8859-1"));
        OutputStream os = response.getOutputStream();
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.useFont(ResourceUtils.getFile("classpath:pdf/fonts/simsun.ttf"), "simsun");
        builder.useFastMode();
        builder.withHtmlContent(example, ResourceUtils.getURL("classpath:pdf/img/").toString());
        builder.toStream(os);
        builder.useSVGDrawer(new BatikSVGDrawer(BatikSVGDrawer.SvgScriptMode.INSECURE_ALLOW_SCRIPTS, BatikSVGDrawer.SvgExternalResourceMode.INSECURE_ALLOW_EXTERNAL_RESOURCE_REQUESTS));
        builder.run();
    }

I try to remove the clip-path or style in the path tag and it can work completely.Is this a bug or my problem?

danfickle commented 4 years ago

If it shows in a browser correctly, it is probably a bug. @rototor might be able to help, but I think he is pretty busy.

rototor commented 4 years ago

That's likely a bug in Apache Batik, some missing .dispose() call on a child Graphics2D. You can call createGraphics() on a Graphics2D object, to obtain a "child" Graphics2D, in which you can change all states without affecting the parent. You should dispose() it after using it. Especially in the PdfBoxGraphics2D adapter this is important, as it's required to correctly manage the save/restore state in the PDF stream. If the PdfBoxGraphics2D adapter would not throw this exception you would have an invalid PDF stream... (i.e. Acrobat might refuse to render it...)

What Batik version are you using? 1.12? Or the new released 1.13?

I assume that this will make no difference, but you could try anyway. I'll try to look into this, but I can not say when I find time...

bomb12138 commented 4 years ago

Now i use the 1.12.thanks.I'll try what you say.

bomb12138 commented 4 years ago

Finally i have solved the problem.I find that it can work if i just remove the opacity setting.So setting the opacity to 1 and the fill-opacity to 0.4 can resolve.This way just bypasses the bug and it still exists.

rototor commented 4 years ago

This is a bug in Batik, I've opened an issue there (https://issues.apache.org/jira/browse/BATIK-1290)