SVG line not getting rendered properly in PDF

我的未来我决定 提交于 2020-01-02 11:02:14

问题


I am using iText and some SVG rendering library.

I want to render a SVG image to PDF. I am using following code to do that. Now the problem is I am giving an area chart SVG to it, it is not rendering line properly. Attaching screenshots.

Following classes has been used:

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.SVGDocument;

and here the cop snippet:

    String parser = XMLResourceDescriptor.getXMLParserClassName();
    factory = new SAXSVGDocumentFactory(parser);

    UserAgent userAgent = new UserAgentAdapter();
    DocumentLoader loader = new DocumentLoader(userAgent);
    ctx = new BridgeContext(userAgent, loader);
    ctx.setDynamicState(BridgeContext.DYNAMIC);

    builder = new GVTBuilder();

    PdfContentByte cb = writer.getDirectContent();


    PdfTemplate chartImageTemplate = cb.createTemplate(1000, 1000);


    SVGDocument svgDoc=null;
    try {
        InputStream in = new ByteArrayInputStream(chartImage.getBytes());
        svgDoc = factory.createSVGDocument("", in);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try{
        GraphicsNode mapGraphics = builder.build(ctx, svgDoc);

        DefaultFontMapper mapper = new DefaultFontMapper();
        PdfGraphics2D g2 = (PdfGraphics2D)cb.createGraphics(1000, 500, mapper);
        Graphics2D g2d = g2;
        mapGraphics.paint(g2d);
        g2d.dispose();

        cb.addTemplate(chartImageTemplate, 0, 0);
        return mapGraphics.getGeometryBounds().getHeight();
    }catch(Exception e){

    }

You can find SVG here: http://speedy.sh/xvDp8/demo.svg

Actual SVG:

Rendered PDF

来源:https://stackoverflow.com/questions/25889780/svg-line-not-getting-rendered-properly-in-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!