Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help to generate png image from svg file

 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there
It would be much appreciated if any one could help me out.
I have a servlet where I use PNGTranscoder to generate png image and display it. This is my code:
PNGTranscoder trans = new PNGTranscoder();
File svgFile = new File(SVGFILE);
long svgSize = svgFile.length();
TranscoderInput input = new TranscoderInput(svgURI);
ByteArrayOutputStream outStream
= new ByteArrayOutputStream((int)svgSize);
TranscoderOutput output
= new TranscoderOutput(outStream);
trans.transcode(input,output);

HOWEVER, the page above codes rendered is blank. May I ask why the png file is not created corrected ?
Tons of thanks,
Lily
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not 100% sure, but it might be this part of the line with the int in ()
ByteArrayOutputStream((int)svgSize);
Eric
 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That syntax is correct. I think the reason might be that I used ByteArrayOutputStream as PNGTranscoder's output stream and ByteArrayOutputStream will not out put its contents to browser.
However, PNGTranscoder can only accept ByteArrayOutputStream as it output stream. I am wondering if there is any way that I can get around this problem.
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to pass back the contents of the ByteArrayOutputStream as part of the HttpResponse you will need to do something like this at the end of your code:
response.setContentType("image/png");
ServletOutputStream servletOutput =
response.getOutputStream();
servletOutput.write(outStream.toByteArray());
servletOutput.flush();
response.setStatus(HTTPServletResponse.SC_OK);

Hope this is what you are after.
[ April 10, 2003: Message edited by: Andy Bowes ]
 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Andy. Yes, this is what I need. I need to set contenttype to be image/png.
Thanks again,
Lily
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic