Java只生成带有Batik的SVG,不包含任何DOCTYPE
我正在尝试生成svg,稍后将用于从HTML文件生成PDF。我使用OpenHtmlToPdf,它可以将内联svg与其他html代码一起转换为pdf 问题是Batik示例生成svg以及Java只生成带有Batik的SVG,不包含任何DOCTYPE,java,svg,batik,Java,Svg,Batik,我正在尝试生成svg,稍后将用于从HTML文件生成PDF。我使用OpenHtmlToPdf,它可以将内联svg与其他html代码一起转换为pdf 问题是Batik示例生成svg以及和标记 我只想要标签及其内容 这是我用于生成svg的代码: public class TestSVGGen { public void paint(Graphics2D g2d) { g2d.fill(new Rectangle(10, 10, 400, 5)); g2d.fill(new Recta
和
标记
我只想要
标签及其内容
这是我用于生成svg的代码:
public class TestSVGGen {
public void paint(Graphics2D g2d) {
g2d.fill(new Rectangle(10, 10, 400, 5));
g2d.fill(new Rectangle(10, 15, 10, 125));
g2d.fill(new Rectangle(110, 15, 10, 125));
g2d.fill(new Rectangle(210, 15, 10, 125));
g2d.fill(new Rectangle(310, 15, 10, 125));
g2d.fill(new Rectangle(400, 15, 10, 125));
g2d.drawString("test",20,150);
}
public static void main(String[] args) throws IOException {
// Get a DOMImplementation.
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// Ask the test to render into the SVG Graphics2D implementation.
TestSVGGen test = new TestSVGGen();
test.paint(svgGenerator);
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
boolean useCSS = true; // we want to use CSS style attributes
Writer out = new FileWriter("test-svg.xhtml");
svgGenerator.stream(out, useCSS);
}
}
这就是我得到的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
/><g
><g
><rect x="10" width="400" height="5" y="10" style="stroke:none;"
/><rect x="10" width="10" height="125" y="15" style="stroke:none;"
/><rect x="110" width="10" height="125" y="15" style="stroke:none;"
/><rect x="210" width="10" height="125" y="15" style="stroke:none;"
/><rect x="310" width="10" height="125" y="15" style="stroke:none;"
/><rect x="400" width="10" height="125" y="15" style="stroke:none;"
/><text x="20" xml:space="preserve" y="150" style="stroke:none;"
>test</text
></g
></g
></svg
>
我在谷歌上发现:-转码器API似乎很难完成这项工作。但是我想在我在google上发现之前,我会使用substring删除所有内容:-似乎代码转换API很难完成这项工作。但我想我会先用substring删除所有内容
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
/><g
><g
><rect x="10" width="400" height="5" y="10" style="stroke:none;"
/><rect x="10" width="10" height="125" y="15" style="stroke:none;"
/><rect x="110" width="10" height="125" y="15" style="stroke:none;"
/><rect x="210" width="10" height="125" y="15" style="stroke:none;"
/><rect x="310" width="10" height="125" y="15" style="stroke:none;"
/><rect x="400" width="10" height="125" y="15" style="stroke:none;"
/><text x="20" xml:space="preserve" y="150" style="stroke:none;"
>test</text
></g
></g
></svg
>
public class TestSVGGen {
public void paint(Graphics2D g2d) {
g2d.fill(new Rectangle(10, 10, 400, 5));
g2d.fill(new Rectangle(10, 15, 10, 125));
g2d.fill(new Rectangle(110, 15, 10, 125));
g2d.fill(new Rectangle(210, 15, 10, 125));
g2d.fill(new Rectangle(310, 15, 10, 125));
g2d.fill(new Rectangle(400, 15, 10, 125));
g2d.drawString("test",20,150);
}
public static void main(String[] args) throws IOException {
Document document = newEmptyDocument();
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// Ask the test to render into the SVG Graphics2D implementation.
TestSVGGen test = new TestSVGGen();
test.paint(svgGenerator);
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
boolean useCSS = true; // we want to use CSS style attributes
Writer out = new FileWriter("test-svg.xhtml");
svgGenerator.stream(out, true);
}
public static Document newEmptyDocument() {
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document ret;
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
ret = builder.newDocument();
return ret;
}