aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Need Java program to convert SVG to PDF format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Need Java program to convert SVG to PDF format" Watch "Need Java program to convert SVG to PDF format" New topic
Author

Need Java program to convert SVG to PDF format

Mansa Kumar
Greenhorn

Joined: Nov 16, 2008
Posts: 6
Hello,
I need to convert some SVG (scalable vector graphics) files to PDF format. I tried using Apache Batik toolkit to do the conversion and it didnt work. Does someone know about any other toolkit or a java program that can convert SVG files to PDF.

I am primarily looking for a java program and not an application because I need to run this java program in server.

Please let me know if you have any idea.
Thanks in advance!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
Hello "mkumar"-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, what does "it didnt work" mean? What exactly did you try, and what happened?


Ping & DNS - my free Android networking tools app
Mansa Kumar
Greenhorn

Joined: Nov 16, 2008
Posts: 6
By "it didnt work" I meant that I had a java program using apache batik for converting SVG to PDF and that didnt work.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
Yes, that's how I understood it, but that doesn't answer the question "What exactly did you try, and what happened?" TellTheDetails
Mansa Kumar
Greenhorn

Joined: Nov 16, 2008
Posts: 6
My client used Verydoc to convert an EMF (enhanced metafiles) to SVG and I had to convert that SVG file to a PDF. I am getting an exception while converting the given SVG to PDF using the apache batik supported program.

The error message is:
java.io.IOException: The prefix "xlink" for attribute "xlink:href" associated with an element type "image" is not bound.
at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(Unknown Source)
at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(Unknown Source)
at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown Source)
at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown Source)
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)

and the Java source code is:
//Create transcoder
Transcoder transcoder = new PDFTranscoder();
//Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();

//Setup input
File svgFile = new File(svg);
File pdfFile = new File(pdf);

InputStream in = new java.io.FileInputStream(svgFile);
try {
TranscoderInput input = new TranscoderInput(in);

//Setup output
OutputStream out = new java.io.FileOutputStream(pdfFile);
out = new java.io.BufferedOutputStream(out);
try {
TranscoderOutput output = new TranscoderOutput(out);
//Do the transformation
transcoder.transcode(input, output);
} finally {
out.close();
}
} finally {
in.close();
}

Because of this, I am looking for another java program which can successfully convert this SVG file to PDF (If not PDF then JPEG/TIFF in the worst case.).

Thanks a ton.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
Does it work if you alter the SVG so that it declares the proper XLink URI namespace? Its proper value should be "http://www.w3.org/1999/xlink". If that works, maybe you can get the SVG-emitting process to write that out, or -alternatively- add an intermediate step that adds that namespace declaration to the SVG file.
Mansa Kumar
Greenhorn

Joined: Nov 16, 2008
Posts: 6
Thanks a lot for the idea. The generated SVG was in fact missing the namespace binding along with couple of mismatched end tags. I added xmlns:xlink="http://www.w3.org/1999/xlink" in the svg tag and added the missing end tags and it worked fine.

My client uses Verydoc to create SVG files and Verydoc generated SVG file was not well formed XML and because of this I have to look for another java program that might convert these to PDF overlooking well formedness (like the way IE does, btw IE renders these SVG files well).

Any idea about non-batik program?
Thanks again.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
I doubt that there is any serious software that will handle malformed XML. IE makes a best effort to display something, just like it does with crappy HTML, but you shouldn't expect that.

It's inexcusable for a commercial software to generate malformed XML; have their developers fix that - it's a major bug.
Mansa Kumar
Greenhorn

Joined: Nov 16, 2008
Posts: 6
Thanks again Ulf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Java program to convert SVG to PDF format