SVG в PDF. Как?

Я пытаюсь вывести файл SVG в PDF. Я пробовал несколько подходов, но продолжаю сталкиваться с проблемами.

Я использовал этот источник в качестве ссылки: конвертировать SVG в 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, что действительно странно, так как я вижу его во внешних библиотеках.

Далее я попробовал использовать iTextG. Я следовал за кодом в SvgToPdf: https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-15

Но затем я застреваю, поскольку iTextG не имеет PdfGraphics2D, и этот метод требует этого.

Любая идея о том, как я могу пойти по этому поводу?

1 ответ

Решение

Вот решение, к которому я пришел, которое опирается на любые библиотеки.

Движок 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());
Другие вопросы по тегам