This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes need help to generate png image from svg file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "need help to generate png image from svg file" Watch "need help to generate png image from svg file" New topic
Author

need help to generate png image from svg file

lily zou
Ranch Hand

Joined: Jan 15, 2002
Posts: 50
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

Joined: Nov 08, 2001
Posts: 15357
    
    6
not 100% sure, but it might be this part of the line with the int in ()
ByteArrayOutputStream((int)svgSize);
Eric
lily zou
Ranch Hand

Joined: Jan 15, 2002
Posts: 50
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

Joined: Jan 14, 2003
Posts: 171
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 ]

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
lily zou
Ranch Hand

Joined: Jan 15, 2002
Posts: 50
Thanks a lot, Andy. Yes, this is what I need. I need to set contenttype to be image/png.
Thanks again,
Lily
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: need help to generate png image from svg file
 
Similar Threads
SVG to JPG
Need Java program to convert SVG to PDF format
FOP, Error in FO header
converting WMF(Windows Meta File) to JPEG
Converting SVG to PDF