Convert pdf to svg

I want to convert PDF to SVG, please suggest some libraries / executables that can do this efficiently. I wrote my own java program using the apache PDFBox and Batik libraries -

PDDocument document = PDDocument.load( pdfFile ); DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document. String svgNS = "http://www.w3.org/2000/svg"; Document svgDocument = domImpl.createDocument(svgNS, "svg", null); SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(svgDocument); ctx.setEmbeddedFontsOn(true); // Ask the test to render into the SVG Graphics2D implementation. for(int i = 0 ; i < document.getNumberOfPages() ; i++){ String svgFName = svgDir+"page"+i+".svg"; (new File(svgFName)).createNewFile(); // Create an instance of the SVG Generator. SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx,false); Printable page = document.getPrintable(i); page.print(svgGenerator, document.getPageFormat(i), i); svgGenerator.stream(svgFName); } 

This solution works fine, but the size of the resulting svg files is huge (many times larger than in pdf format). I found out where the problem is while looking at svg in a text editor. it covers each character in the source document in its own block, even if the font properties of the characters are the same. For example, the word hello will appear as 6 different text blocks. Is there a way to fix the above code? or suggest another solution that will work more efficiently.

+38
pdf svg pdfbox batik
Nov 08 2018-10-10T00:
source share
3 answers

Inkscape can also be used to convert PDF to SVG. This is actually remarkably good, and although the code that it generates is a little bloated, at least it does not have much of a problem that you encounter in your program. I think it would be difficult to integrate it directly into Java, but inkscape provides a convenient command line interface for this function, so perhaps the easiest way to access it is through a system call.

To use the Inkscape command-line interface to convert PDF to SVG, use:

 inkscape -l out.svg in.pdf 

What you can probably call using:

 Runtime.getRuntime().exec("inkscape -l out.svg in.pdf") 

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29

I think that exec () is synchronous and returns only after the process is complete (although I am not 100% sure about this), so after that you can just read "out.svg". In any case, Googling's "java system call" will give more information on how to properly complete this part.

+48
Nov 08 '10 at 12:12
source share

Check out pdf2svg :

Use

 pdf2svg <input.pdf> <output.svg> [<pdf page no. or "all" >] 

If using all specify the name of the file with %d in it (which will be replaced by the page number).

 pdf2svg input.pdf output_page%d.svg all 

And for some troubleshooting see: http://www.calcmaster.net/personal_projects/pdf2svg/

+30
Dec 21 '10 at 17:18
source share
 pdftk 82page.pdf burst sh to-svg.sh 

contents of to-svg.sh

 #!/bin/bash FILES=burst/* for f in $FILES do inkscape -l "$f.svg" "$f" done 
0
Oct. 10 '17 at 6:40
source share



All Articles