java - 使用 JAVA 将 HTML5 编写的 SVG 转换为 JPG

标签 java spring html svg thymeleaf

是否可以使用Java将纯SVG代码转换为JPG?我尝试使用batik SVG,但它需要文件路径来进行转换,并且我只想使用代码。

SVG 代码:

String chart = "<svg width=\"700\" height=\"297\" aria-label=\"A chart.\" style=\"overflow: hidden;\"><defs id=\"defs\"><clipPath id=\"_ABSTRACT_RENDERER_ID_0\"><rect x=\"175\" y=\"57\" width=\"350\" height=\"184\"></rect></clipPath><filter id=\"_ABSTRACT_RENDERER_ID_1\"><feGaussianBlur in=\"SourceAlpha\" stdDeviation=\"2\"></feGaussianBlur><feOffset dx=\"1\" dy=\"1\"></feOffset><feComponentTransfer><feFuncA type=\"linear\" slope=\"0.1\"></feFuncA></feComponentTransfer><feMerge><feMergeNode></feMergeNode><feMergeNode in=\"SourceGraphic\"></feMergeNode></feMerge></filter></defs><rect x=\"0\" y=\"0\" width=\"700\" height=\"297\" stroke=\"none\" stroke-width=\"0\" fill=\"#ffffff\"></rect><g><rect x=\"538\" y=\"57\" width=\"149\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g><rect x=\"538\" y=\"57\" width=\"149\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g><text text-anchor=\"start\" x=\"569\" y=\"68.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">Wynik</text></g><rect x=\"538\" y=\"57\" width=\"26\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#3366cc\"></rect></g></g><g><rect x=\"175\" y=\"57\" width=\"350\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g clip-path=\"url(http://localhost:8080/summary#_ABSTRACT_RENDERER_ID_0)\"><g><rect x=\"175\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"262\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"350\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"437\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"524\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect></g><g><rect x=\"176\" y=\"64\" width=\""+  String.valueOf(getGroupAnswer.get(0).getValue() * 5.79) +"\" height=\"23\" stroke=\"#ffa500\" stroke-width=\"1\" fill=\"#ffa500\"></rect><rect x=\"176\" y=\"101\" width=\""+  String.valueOf(getGroupAnswer.get(1).getValue() * 5.79) +"\" height=\"23\" stroke=\"#b87333\" stroke-width=\"1\" fill=\"#b87333\"></rect><rect x=\"176\" y=\"138\" width=\""+  String.valueOf(getGroupAnswer.get(2).getValue() * 5.79) +"\" height=\"22\" stroke=\"#c0c0c0\" stroke-width=\"1\" fill=\"#c0c0c0\"></rect><rect x=\"176\" y=\"174\" width=\""+  String.valueOf(getGroupAnswer.get(3).getValue() * 5.79) +"\" height=\"23\" stroke=\"#ffd700\" stroke-width=\"1\" fill=\"#ffd700\"></rect><rect x=\"176\" y=\"211\" width=\""+  String.valueOf(getGroupAnswer.get(4).getValue() * 5.79) +"\" height=\"23\" stroke=\"#e5e4e2\" stroke-width=\"1\" fill=\"#e5e4e2\"></rect></g><g><rect x=\"175\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#333333\"></rect></g></g><g></g><g><g><text text-anchor=\"middle\" x=\"175.5\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">0</text></g><g><text text-anchor=\"middle\" x=\"262.75\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">15</text></g><g><text text-anchor=\"middle\" x=\"350\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">30</text></g><g><text text-anchor=\"middle\" x=\"437.25\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">45</text></g><g><text text-anchor=\"middle\" x=\"524.5\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">60</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"80.35\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST4</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"116.95\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST1</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"153.55\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST5</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"190.15\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST3</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"226.75000000000003\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST2</text></g></g></g><g><g><text text-anchor=\"middle\" x=\"350\" y=\"284.4562\" font-family=\"Arial\" font-size=\"13\" font-style=\"italic\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">Wynik całkowity</text><rect x=\"175\" y=\"273.4062\" width=\"350\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect></g></g><g></g></svg>";

最佳答案

您可以将字符串转换为InputStream:

String chart = "<svg width=\"700\...";
InputStream stream = new ByteArrayInputStream(chart.getBytes(StandardCharsets.UTF_8.name()));

并将其传递给 TranscoderInput 构造函数:

TranscoderInput input = new TranscoderInput(stream); 

关于java - 使用 JAVA 将 HTML5 编写的 SVG 转换为 JPG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47528280/

相关文章:

java - 在 gwt.xml 中设置语言环境不起作用

java - 如果方法是事务性的,则 Spring 无法实例化 bean

java - tomcat 中的 Spring Boot war 无法正常工作

java - Restful Spring 服务中的 session 范围

html - Safari 6 表格最大宽度问题

html - CSS匹配选择器的最后一个匹配项?

javascript - 点击下拉列表

java - 如何防止 Hibernate 刷新列表?

java - Set元素添加到List,然后乱七八糟

java - 通过 session 将属性从一个 Portlet 传递到另一个 Portlet