首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG转PDF。怎么做?

SVG转PDF。怎么做?
EN

Stack Overflow用户
提问于 2017-10-18 05:40:20
回答 1查看 960关注 0票数 2

我正在尝试将SVG文件输出到PDF中。我试过几种方法,但我总是遇到问题。

我使用此源作为参考:Convert SVG to PDF,并尝试了以下操作:

代码语言:javascript
复制
// Save this SVG into a file (required by SVG -> PDF transformation process)
File svgFile = File.createTempFile("graphic-", ".svg");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source2 = new DOMSource(svgXmlDoc);
FileOutputStream fOut = new FileOutputStream(svgFile);
try { transformer.transform(source2, new StreamResult(fOut)); }
finally { fOut.close(); }

// Convert the SVG into PDF
File outputFile = File.createTempFile("result-", ".pdf");
SVGConverter converter = new SVGConverter();
converter.setDestinationType(DestinationType.PDF);
converter.setSources(new String[] { svgFile.toString() });
converter.setDst(outputFile);
converter.execute();

我遇到了几个ClassNotFoundExceptions,大多与batik.DOM相关,这真的很奇怪,因为我可以在外部库中看到它。

接下来,我尝试使用iTextG。我遵循了SvgToPdf:https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-15中的代码

但是,我被卡住了,因为iTextG没有PdfGraphics2D,而这个方法需要它。

知道我该怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-19 17:54:41

这是我最后使用的解决方案,它确实依赖于任何库。

WebKit引擎可以呈现SVG,因此可以将SVG加载到WebView中:

代码语言:javascript
复制
webView.loadUrl(Uri.fromFile(svgFile).toString());

WebView还具有打印功能,因此您可以继续:

代码语言:javascript
复制
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
        .getSystemService(Context.PRINT_SERVICE);

// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
        new PrintAttributes.Builder().build());
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46803470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档