将pdf转换成svg [英] convert pdf to svg

查看:1084
本文介绍了将pdf转换成svg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将PDF转换为SVG,请建议一些能够有效执行此操作的库/可执行文件.我已经使用apache PDFBox和Batik库编写了自己的Java程序-

I want to convert PDF to SVG please suggest some libraries/executable that will be able to do this efficiently. I have written 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);
    }

此解决方案效果很好,但是生成的svg文件的大小非常大.(比pdf大很多倍).我通过在文本编辑器中查看svg找出了问题所在.即使字符的字体属性相同,它也会将原始文档中的每个字符括在自己的块中.例如,单词hello将显示为6个不同的文本块.有没有办法解决以上代码?或者,请提出另一种更有效的解决方案.

This solution works great but the size of the resulting svg files in huge.(many times greater than the pdf). I have figured out where the problem is by looking at the svg in a text editor. it encloses every character in the original document in its own block even if the font properties of the characters is the same. For example the word hello will appear as 6 different text blocks. Is there a way to fix the above code? or please suggest another solution that will work more efficiently.

推荐答案

Inkscape也可以用于将PDF转换为SVG.实际上,它非常出色,尽管它生成的代码有点肿,但至少,它似乎没有程序中遇到的特定问题.我认为将其直接集成到Java中具有挑战性,但是inkscape为该功能提供了一个方便的命令行界面,因此,最简单的访问方法可能是通过系统调用.

Inkscape can also be used to convert PDF to SVG. It's actually remarkably good at this, and although the code that it generates is a bit bloated, at the very least, it doesn't seem to have the particular issue that you are encountering in your program. I think it would be challenging to integrate it directly into Java, but inkscape provides a convenient command-line interface to this functionality, so probably the easiest way to access it would be via a system call.

要使用Inkscape的命令行界面将PDF转换为SVG,请使用:

To use Inkscape's command-line interface to convert a PDF to an SVG, use:

inkscape -l out.svg in.pdf

然后您可以使用以下方式致电:

Which you can then probably call using:

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

我认为exec()是同步的,并且仅在进程完成后才返回(尽管我不确定100%如此),因此您应该能够在此之后仅读取"out.svg".无论如何,谷歌搜索"java系统调用"将提供有关如何正确执行该部分的更多信息.

I think exec() is synchronous and only returns after the process completes (although I'm not 100% sure on that), so you shoudl be able to just read "out.svg" after that. In any case, Googling "java system call" will yield more info on how to do that part correctly.

这篇关于将pdf转换成svg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆