使用Apache Batik将SVG转换为PNG,然后使用PDFBox附加到PDF而不保存图像

问题描述 投票:1回答:2

因此,标题说我正在寻找一种方法,使用Apache Batik将SVG转换为PNG,然后使用PDFBox将此图像附加到PDF文件,而无需在任何地方实际创建svg和png。

目前我有一个Web表单,其中包含SVG图像及其可选部分。当提交表单时,我采用svg的“html”部分,这意味着我将一些像<svg bla bla> <path bla bla/></svg>这样的字符串保存在一个字符串中,然后Spring用它在给定的文件夹中创建一个“.svg”文件,然后Batik创建一个PNG文件文件夹,然后PDFBox将其附加到PDF - 这工作正常(下面的代码)。

//Get the svg data from the Form and Create the svg file
String svg = formData.getSvg();
File svgFile = new File("image.svg");
BufferedWriter writer = new BufferedWriter(new FileWriter(svgFile));
writer.write(svg);
writer.close(); 
// Send to Batik to turn to PNG
PNGTranscoder pngTranscode = new PNGTranscoder();
File svgFile = new File("image.svg");
InputStream in = new FileInputStream(svgFile);
TranscoderInput tIn = new TranscoderInput(in);
OutputStream os = new FileOutputStream("image.png");
TranscoderOutput tOut = new TranscoderOutput(os)
pngTranscode .transcode(tIn , tOut);
os.flush();
os.close();
//Send to PDFBox to attach to pdf
File pngfile = new File("image.png");
String path = pngfile.getAbsolutePath();                    
PDImageXObject pdImage = PDImageXObject.createFromFile(path, pdf);
PDPageContentStream contents = new PDPageContentStream(pdf, pdf.getPage(1));
contents.drawImage(pdImage, 0, pdf.getPage(1).getMediaBox().getHeight() - pdImage.getHeight()); 
contents.close();

正如你所看到的那样,有很多文件和东西(需要整理一下),但是在运行时是否可以在不创建和不断获取svg和png文件的情况下执行此操作?

java spring pdfbox batik
2个回答
1
投票

根据评论中的建议,我选择使用ByteArrayOutputStream,ByteArrayInputStream,BufferedImage和LosslessFactory。它比保存慢一点(如果你在调试中经历它似乎BufferedImage在创建图像之前首先去度假)。我发现使用的来源是:How to convert SVG into PNG on-the-flyPrint byte[] to pdf using pdfbox

byte[] streamBytes = IOUtils.toByteArray(new ByteArrayInputStream(formData.getSvg().getBytes()));
PNGTranscoder pngTranscoder = new PNGTranscoder();
ByteArrayOutputStream os = new ByteArrayOutputStream();                  
pngTranscoder.transcode(new TranscoderInput(new ByteArrayInputStream(streamBytes)), new TranscoderOutput(os));
InputStream is = new ByteArrayInputStream(os.toByteArray());
BufferedImage bim = ImageIO.read(is);
PDImageXObject pdImage = LosslessFactory.createFromImage(pdf, bim);
PDPageContentStream contents = new PDPageContentStream(pdf, pdf.getPage(1));
contents.drawImage(pdImage, 0, pdf.getPage(1).getMediaBox().getHeight() - pdImage.getHeight()); 
contents.close();

0
投票

根据D.V.D.提供的评论和链接,我也解决了这个问题。我只想发布一个简单但完整的例子,对于想要在将来进行审核的任何人。

public class App {

    private static String OUTPUT_PATH = "D:\\so52875145\\output\\PdfWithPngImage.pdf";

    public static void main(String[] args) {

        try {
            // obtain the SVG source (hardcoded here, but the OP would obtain the String from form data)
            byte[] svgByteArray = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><polygon points=\"200,10 250,190 160,210\" style=\"fill:lime;stroke:purple;stroke-width:1\" /></svg>".getBytes();
            System.out.println("Converted svg to byte array...");

            // convert SVG into PNG image
            PNGTranscoder pngTranscoder = new PNGTranscoder();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            pngTranscoder.transcode(new TranscoderInput(new ByteArrayInputStream(svgByteArray)), new TranscoderOutput(os));
            System.out.println("Transcoded svg to png...");

            // create PDF, and add page to it
            PDDocument pdf = new PDDocument();
            pdf.addPage(new PDPage());

            // generate in-memory PDF image object, using the transcoded ByteArray stream
            BufferedImage bufferedImage = ImageIO.read( new ByteArrayInputStream(os.toByteArray()) );
            PDImageXObject pdImage = LosslessFactory.createFromImage(pdf, bufferedImage);
            System.out.println("Created PDF image object...");

            // write the in-memory PDF image object to the PDF page
            PDPageContentStream contents = new PDPageContentStream(pdf, pdf.getPage(0));
            contents.drawImage(pdImage, 0, 0);
            contents.close();
            System.out.println("Wrote PDF image object to PDF...");

            pdf.save(OUTPUT_PATH);
            pdf.close();
            System.out.println("Saved PDF to path=[" + OUTPUT_PATH + "]");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.