Svg integration in pdf using flying saucer

I had a situation with converting html to pdf, fortunately, I can achieve this through the api flying saucer. But My HTML consists of svg tags when converting. I can not get svg in pdf format. After google, I found that it can be achieved using the link below https://stackoverflow.com/a/166239/ and the Tutorial . I do not understand what "replaceElementFactory" means

ChainingReplacedElementFactory chainingReplacedElementFactory 
        = new ChainingReplacedElementFactory();
chainingReplacedElementFactory.addReplacedElementFactory(replacedElementFactory);
chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);

Please help get rid of this.

+4
source share
3 answers

This is just a mistake in the textbook, a line with is replacedElementFactorynot needed.

.

Java:

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class PdfSvg {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document inputDoc =  builder.parse("svg.html");

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        ITextRenderer renderer = new ITextRenderer();

        ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
        chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
        renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);

        renderer.setDocument(inputDoc, "");;
        renderer.layout();
        renderer.createPDF(output);

        OutputStream fos = new FileOutputStream("svg.pdf");
        output.writeTo(fos);
    }
}

HTML:

<html>
<head>
<style type="text/css">
    svg {display: block;width:100mm;height:100mm}
</style>
</head>
<body>
    <div>
        <svg xmlns="http://www.w3.org/2000/svg">
            <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3"
                fill="red" />
        </svg>
    </div>
</body>
</html>

ChainingReplacedElementFactory, SVGReplacedElement SVGReplacedElementFactory .

+2

, @cloudformatter, . Javascript Highchart.

http://jsfiddle.net/yk0Lxzg0/1/

var click="return xepOnline.Formatter.Format('printme', {render:'download'})";
jQuery('#buttons').append('<button onclick="'+ click +'">PDF</button>');

, , div 'id' printme PDF . div .

http://www.cloudformatter.com/CSS2Pdf.APIDoc.Usage SVG, PDF, , , ..

+1

@Rajesh I hope you have already found a solution to your problem. If not (or anyone has problems with flying saucers, batiks and svg tags), then you might think about it - deleting all the tags clip-path="url(#highcharts-xxxxxxx-xx)"from <g>did the trick for me.

0
source

All Articles