java - Create PDF file with embedded SVG in Java
问题描述
I have an SVG file and I need to create a PDF that has the SVG embedded. I tried with Apache PDFBox (see below) but I get an error saying that "SVG files are not supported". Any ideas how to create a PDF with an embedded SVG? It doesn't have to be with PDFBox, and I prefer not to convert the SVG to an image file.
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDImageXObject pdImage = PDImageXObject.createFromFile("C:/chart.svg", doc);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawImage(pdImage, 70, 250);
contentStream.close();
doc.save("C:/chart.pdf");
doc.close();
Exception in thread "main" java.lang.IllegalArgumentException: Image type not supported: chart.svg at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFileByExtension(PDImageXObject.java:257) at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFile(PDImageXObject.java:202) at testjava.TestPdfBox.main(TestPdfBox.java:18)
解决方案
推荐阅读
- ios - 如何检查有效的时间格式?
- spring-boot - 使用 JmsListener 存储接收/发送到队列的消息
- go - golang asm代码中的未知“ptr”变量
- powershell - 使用PowerShell杀死Windows上的子进程
- java - “在 char[] 中查找子字符串”得到意想不到的结果
- unity3d - 由于错误,DLL不会被加载
- azure - 如何使用 PowerBI API 或 Azure 门户提取所有 PowerBI 用户和工作区访问权限?
- android - 媒体播放器安卓问题
- monitoring - 如何使用 Graphite 监控 AWS SQS - Grafana
- android - 如何在 android firebase 聊天中显示打字指示器