Search code examples
javaspringhtmlsvgthymeleaf

Convert SVG written in HTML5 to JPG using JAVA


is it possible to convert pure SVG code into JPG using Java? I've tried to use batik SVG, but it need file path to converse, and I would like to use only code.

SVG Code:

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>";

Solution

  • You can convert String into InputStream:

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

    and pass that to TranscoderInput constructor:

    TranscoderInput input = new TranscoderInput(stream);