TranscoderException while adding svg to pdf using batik

664 views Asked by At

I am trying to insert svg content into a pdf file. I am using batik for the same. My code is as follows:

FileInputStream svg = new FileInputStream(new File("my.svg"));
Transcoder transcoder = new PDFTranscoder();

        InputStream stream = new ByteArrayInputStream(svg.toString().getBytes());
        TranscoderInput input = new TranscoderInput(stream);
        TranscoderOutput output = new TranscoderOutput(fileName);

        transcoder.transcode(input, output);
                stream.close();
        svg.close();

I get the below error on running the above code.

org.apache.batik.transcoder.TranscoderException: null Enclosed Exception: Content is not allowed in prolog.

Any help is appreciated.

Edit:

Here are the first few lines of my.svg:

<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="400"><defs><clippath id="highcharts-4"><rect rx="0" ry="0" fill="none" x="0" y="0" width="9999" height="400" stroke-width="0.000001"/></clippath><clippath id="highcharts-5"><rect fill="none" x="0" y="0" width="380" height="335"/></clippath></defs><rect rx="5" ry="5" fill="#FFFFFF" x="0" y="0" width="400" height="400" stroke-width="0.000001"/><g class="highcharts-series-group" /><path fill="none" d="M 125.41758851604905 23.732559782274937 C 130.41758851604905 23.732559782274937 133.48045946783446 31.123014540455173 137.69190702653944 41.28488983295301 L 141.90335458524441 51.44676512545085" stroke-width="1" stroke="#FFFB76" visibility="visible"  transform="translate(10,50)"/><path fill="none" d="M 32.367046022924654 137.13024555199692 C 37.367046022924654 137.13024555199692 45.21323803459761 138.69142168426376 56.001752050647916 140.83803886613063 L 66.79026606669822 142.9846560479975" stroke-width="1" stroke="#FE8E87" visibility="visible"  transform="translate(10,50)"/><path fill="none" d="M 55.60634866190446 253.96631493474996 C 60.60634866190446 253.96631493474996 67.25790985518806 249.5214601911162 76.40380649595305 243.40978491861983 L 85.54970313671804 237.29810964612346" stroke-width="1" stroke="#FFC772" visibility="visible"  transform="translate(10,50)"/><path fill="none" d="M 338.7608245153441 107.90162787736199 C 333.7608245153441 107.90162787736199 326.370709841463 110.96531929571447 316.2093021648764 115.17789499594912 L 306.04789448828984 119.39047069618377" stroke-width="1" stroke="#2AC6FF" visibility="visible"  transform="translate(10,50)"/><path fill="none" d="M 195.0461158567799 323.12499316731794 C 190.0461158567799 323.12499316731794 190.04374524245637 315.12499351855627 190.04048564776022 304.12499400150887 L 190.03722605306407 293.12499448446147" stroke-width="1" stroke="#81CF9B" visibility="visible"  transform="translate(10,50)"/><text x="200" y="35" style="font-family:lucida grande;font-size:15px;color:#888888;font-weight:bold;fill:#888888;" text-anchor="middle" class="highcharts-title" ><tspan x="200">Chart title</tspan></text><g class="highcharts-shadow"  transform="translate(10,50)"><path fill="none" d="M 189.97441355378578 42.375002605636766 A 125.625 125.625 0 0 1 278.8657401089984 256.7948245106659 L 190 168 A 0 0 0 0 0 190 168 Z" stroke="black" stroke-width="5" stroke-linejoin="round"  stroke-opacity="0.049999999999999996" transform="translate(1, 1)"/>
1

There are 1 answers

0
Robert Longson On

Add <?xml version="1.0"?> to the beginning of the file.

Also check there's nothing else before the <svg...