首页 > 解决方案 > 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)

标签: javapdfbox

解决方案


推荐阅读