Батик Конвертировать SVG в JPEG - PullRequest
0 голосов
/ 18 марта 2019
JPEGTranscoder transcoder = new JPEGTranscoder();
String urlPath = "D:/CRD_Material/Scheme/scheme-2.svg";

String s1 = "<svg width='350' height='450' xmlns='http://www.w3.org/2000/svg'  xmlns:xlink='http://www.w3.org/1999/xlink'>"
            + "<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->  "
            + "<g><title>Layer 1</title> "
            + " <rect fill='none' stroke='#000000' stroke-width='2' stroke-linejoin='none' stroke-linecap='none' x='18' y='23' width='213' height='352' id='svg_6'/>"
            + "   <line fill='none' stroke='#000000' stroke-width='2' stroke-linejoin='none' stroke-linecap='none' x1='17' y1='336' x2='230' y2='336' id='svg_7'/>"
            + "   <line fill='none' stroke='#000000' stroke-width='2' stroke-linejoin='none' stroke-linecap='none' x1='20' y1='58' x2='231' y2='58' id='svg_8' stroke-dasharray='2,2'/>"
            + "   <rect id='svg_3' height='65' width='181' y='74' x='31' stroke-width='2' stroke='#000000' fill='#0'/>  </g> </svg>";

FileWriter writer = new FileWriter("D:/PDFBox/test.svg");
writer.write(s1);
writer.close();

transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));

TranscoderInput input = new TranscoderInput(new FileInputStream("D:/PDFBox/test.svg"));
OutputStream ostream = new FileOutputStream("D:/PDFBox/out.jpg");
TranscoderOutput output = new TranscoderOutput(ostream);

transcoder.transcode(input, output);
ostream.close();
System.exit(0);

Это мой код, и я хочу преобразовать SVG String в изображение JPEG. Во время выполнения этого кода я получил следующую ошибку:

org.w3c.dom.DOMException: <unknown>:
The attribute "stroke-linecap" represents an invalid CSS value ("none").
Original message:
The "none" identifier is not a valid value for the "stroke-linecap" property

Кто-то, пожалуйста, помогите мне .. Заранее спасибо

1 Ответ

1 голос
/ 18 марта 2019

Единственными действительными значениями для stroke-linecap являются butt, round и square, см., Например, здесь . По умолчанию butt. Так что либо измените none на один из них, либо удалите сегменты stroke-linecap='none' везде.

Ваш svg-код также имеет аналогичную проблему с stroke-linejoin, см. Действительные значения здесь .

И, наконец, fill='#0' тоже неверно. Если вы хотите черный, используйте fill='#000000' или fill='black'.

Таким образом, новая строка может быть

String s1 = "<svg width='350' height='450' xmlns='http://www.w3.org/2000/svg'  xmlns:xlink='http://www.w3.org/1999/xlink'>"
        + "<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->  "
        + "<g><title>Layer 1</title> "
        + " <rect fill='none' stroke='#000000' stroke-width='2' x='18' y='23' width='213' height='352' id='svg_6'/>"
        + "   <line fill='none' stroke='#000000' stroke-width='2' x1='17' y1='336' x2='230' y2='336' id='svg_7'/>"
        + "   <line fill='none' stroke='#000000' stroke-width='2' x1='20' y1='58' x2='231' y2='58' id='svg_8' stroke-dasharray='2,2'/>"
        + "   <rect id='svg_3' height='65' width='181' y='74' x='31' stroke-width='2' stroke='#000000' fill='#000000'/>  </g> </svg>";
...