0

I have an svg image which I want to export as png.

In client side using javascript, I am converting it into base64

var s = new XMLSerializer().serializeToString(document.getElementById("svg"))
var encodedData = window.btoa(s);

In server side I want to decode it and create a .png file

BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(string);

But this gives me a file which can't be opened.

Or is there any other way I can export the svg as png. I can't use toDataUrl since my svg contains images from external source

Jerry
  • 987
  • 4
  • 16
  • 46

3 Answers3

0

Java 8 has support for Base 64 encode and decode

You can do it as follows

byte [] barr = Base64.getDecoder().decode(encoded); 

However, why are you passing it as a base 64 encoded string? Why not send image as multipart/form-data?

Prathik Rajendran M
  • 1,152
  • 8
  • 21
  • Sorry I have no idea how to pass the svg as multipart/form-data. It should be converted into png and then sent? – Jerry Apr 26 '16 at 11:14
  • The file format is immaterial. This answer contains some instructions on how to do file upload in java http://stackoverflow.com/questions/19510656/how-to-upload-files-on-server-folder-using-jsp – Prathik Rajendran M Apr 26 '16 at 11:21
  • Thanks for the reply. I am drawing an svg using d3.js and I want to save it as png image. Since I am using external image sources in svg I am getting CORS error when I try with **toDataUrl** . The reference which you gave won't help me :( – Jerry Apr 26 '16 at 12:46
0

For mime type decoding, use this,

byte[] decodedBuffer = Base64.getMimeDecoder().decode( encodedBuffer );

Resource Link: https://examples.javacodegeeks.com/core-java/util/base64/java-8-base64-encoding-example/

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • @Jerry And also provide the full code in your question – SkyWalker Apr 26 '16 at 12:45
  • I tried it is decoding. But when I create a file still it is coming as not supported file \n `byte[] image = Base64.getMimeDecoder().decode( encodedBuffer ); String dir = System.getProperty("java.io.tmpdir"); String fileName = input.getShortName(); File file = new File(dir + File.separatorChar + fileName); FileOutputStream fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } fop.write(image); fop.flush(); fop.close();` – Jerry Apr 26 '16 at 12:47
  • Sorry it was my mistake .. Decoding was correct only . Once I get the svg file I was just changing the extension to png and downloading. Now I am using batik library for svg to png conversion. It is working fine. :) – Jerry Apr 27 '16 at 12:58
  • @Jerry Nice. At last you have got quell. Thanks for sharing. – SkyWalker Apr 27 '16 at 13:14
0

The decoded byte array will be of svg format. Convert it into png using some library ( I am using apache batik transcoder )

    BASE64Decoder decoder = new BASE64Decoder();
    byte[] image = decoder.decodeBuffer(string);
    String fileLocation  = "C:\temp";
    String fileName = "New-" + System.currentTimeMillis();
    File file = new File(fileLocation +File.separator+ fileName + ".svg");
    FileOutputStream fop = new FileOutputStream(file);
    if (!file.exists()) {
      file.createNewFile();
    }
    fop.write(image);
    fop.flush();
    fop.close();
    PNGTranscoder transcoder = new PNGTranscoder();
    TranscoderInput tinput = new TranscoderInput(new FileInputStream(fileLocation + File.separator + fileName +".svg"));
    OutputStream ostream = new FileOutputStream(fileLocation + File.separator + fileName +".png");
    TranscoderOutput toutput = new TranscoderOutput(ostream);
    try {
          transcoder.transcode(tinput, toutput);
    } catch (TranscoderException e) { 
          System.out.println("error*");
           e.printStackTrace();
    }
    ostream.close();
Jerry
  • 987
  • 4
  • 16
  • 46