2017-10-18 12 views
0

SVGファイルをPDFに出力しようとしています。私はいくつかのアプローチを試みましたが、私は問題にぶち当たっています。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(); 

私はそれが記載されている見ることができるので、私は、本当に不思議ですbatik.DOM、に主に関連するいくつかにClassNotFoundExceptions、に走りました外部ライブラリにあります。

次に、iTextGを使ってみました。私はSvgToPdfのコードに従いました:https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-15

しかし、iTextGにはPdfGraphics2Dがなく、その方法では必要です。

どうすればいいですか?

答えて

0

ここで私はどのライブラリにも依存しています。

あなたはWebViewのにSVGをロードすることができるようにWebKitのエンジンは、SVGをレンダリングすることができます

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());