我有一个servlet,我试图从包含svg代码的字符串中创建pdf。因此,我有此代码:

public class Conversion extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String svgString = req.getParameter("svg");
        StringReader svg = new StringReader(svgString);
        File pdf = new File("/tmp/chart.pdf");

        Transcoder transcoder = new PDFTranscoder();
        TranscoderInput transcoderInput = new TranscoderInput(svg);
        TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(pdf));

        try {
            transcoder.transcode(transcoderInput, transcoderOutput);
        } catch (TranscoderException e) {
            e.printStackTrace();
        }

        resp.setContentType("application/pdf");
        InputStream in = new FileInputStream(pdf);
        ServletOutputStream out = resp.getOutputStream();

        byte[] buffer = new byte[4096];
        while(in.read(buffer, 0, 4096) != -1){
           out.write(buffer, 0, 4096);
        }
        in.close();
        out.flush();
        out.close();
    }
}


执行时出现此错误:

java.lang.ClassNotFoundException: org.apache.batik.dom.svg.SVGDOMImplementation
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    at org.apache.fop.svg.AbstractFOPTranscoder.<init>(AbstractFOPTranscoder.java:107)
    at org.apache.fop.svg.PDFTranscoder.<init>(PDFTranscoder.java:84)
    at com.Webmap_read.Conversion.doGet(Conversion.java:38)
    at com.Webmap_read.Conversion.doPost(Conversion.java:68)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)


当我检查“构建路径”时,似乎从以下站点下载的Batik 1.8存档文件中包含的所有JAR。但是类http://apache.lauf-forum.at/xmlgraphics/batik/binaries/不存在。如何导入所有必需的JAR /类,为什么它们不包含在标准zip中?现在让我发疯了……或者也许有更简单的方法可以将SVG转换为PDF?

最佳答案

看一下http://www.findjar.com/class/org/apache/batik/dom/svg/SVGDOMImplementation.html,检查是否有列出的jar包含在应用程序类路径中。

我建议您从https://xmlgraphics.apache.org/batik/download.html(官方网站)下载Batik。

关于java - Java batik :ClassNotFoundException:org.apache.batik.dom.svg.SVGDOMImplementation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34178503/

10-08 22:12