SVG 到 PDF。如何?
SVG to PDF. How to?
我正在尝试将 SVG 文件输出为 PDF。我尝试了一些方法,但我一直 运行 遇到问题。
我使用这个来源作为参考:
Convert SVG to PDF
并尝试了以下操作:
// 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 有关,这真的是 st运行ge,因为我可以在外部库中看到它。
接下来,我尝试使用 iTextG。我遵循了 SvgToPdf 中的代码:https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-15
但后来我卡住了,因为 iTextG 没有 PdfGraphics2D,而该方法需要它。
知道我该怎么做吗?
这是我最终采用的解决方案,它依赖于任何库。
WebKit 引擎可以呈现 SVG,因此您可以将 SVG 加载到 WebView 中:
webView.loadUrl(Uri.fromFile(svgFile).toString());
WebView也有打印的能力,那么你可以继续:
// 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());
我正在尝试将 SVG 文件输出为 PDF。我尝试了一些方法,但我一直 运行 遇到问题。
我使用这个来源作为参考: Convert SVG to PDF 并尝试了以下操作:
// 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 有关,这真的是 st运行ge,因为我可以在外部库中看到它。
接下来,我尝试使用 iTextG。我遵循了 SvgToPdf 中的代码:https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-15
但后来我卡住了,因为 iTextG 没有 PdfGraphics2D,而该方法需要它。
知道我该怎么做吗?
这是我最终采用的解决方案,它依赖于任何库。
WebKit 引擎可以呈现 SVG,因此您可以将 SVG 加载到 WebView 中:
webView.loadUrl(Uri.fromFile(svgFile).toString());
WebView也有打印的能力,那么你可以继续:
// 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());