使用 Apache Batik 将 SVG 转换为 PNG,然后使用 PDFBox 附加到

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


目前我有一个 Web 表单,其中包含带有可选部分的 SVG 图像。提交表单时,我采用 svg 的“html”部分,这意味着我将类似<svg bla bla> <path bla bla/></svg>字符串的内容保留在 Spring 中,然后 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 文件的情况下在运行时执行此操作?


POPMUISE
浏览 430回答 2
2回答

RISEBY

鉴于评论中的建议,我选择使用 ByteArrayOutputStream、ByteArrayInputStream、BufferedImage 和 LosslessFactory。它比保存慢一点(如果你在调试中通过它,看起来 BufferedImage 在创建图像之前先去度假)。我发现使用的来源是:How to convert SVG into PNG on-the-fly and Print byte[] to pdf using pdfboxbyte[] streamBytes = IOUtils.toByteArray(new ByteArrayInputStream(formData.getSvg().getBytes()));PNGTranscoder pngTranscoder = new PNGTranscoder();ByteArrayOutputStream os = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;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());&nbsp;contents.close();

有只小跳蛙

根据DVD提供的评论和链接,我也解决了这个问题。我只是想发布一个简单但完整的示例,供将来想要查看的任何人使用。public class App {&nbsp; &nbsp; private static String OUTPUT_PATH = "D:\\so52875145\\output\\PdfWithPngImage.pdf";&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // obtain the SVG source (hardcoded here, but the OP would obtain the String from form data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Converted svg to byte array...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // convert SVG into PNG image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PNGTranscoder pngTranscoder = new PNGTranscoder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream os = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pngTranscoder.transcode(new TranscoderInput(new ByteArrayInputStream(svgByteArray)), new TranscoderOutput(os));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Transcoded svg to png...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // create PDF, and add page to it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDDocument pdf = new PDDocument();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdf.addPage(new PDPage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // generate in-memory PDF image object, using the transcoded ByteArray stream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedImage bufferedImage = ImageIO.read( new ByteArrayInputStream(os.toByteArray()) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDImageXObject pdImage = LosslessFactory.createFromImage(pdf, bufferedImage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Created PDF image object...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write the in-memory PDF image object to the PDF page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDPageContentStream contents = new PDPageContentStream(pdf, pdf.getPage(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contents.drawImage(pdImage, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contents.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Wrote PDF image object to PDF...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdf.save(OUTPUT_PATH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdf.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Saved PDF to path=[" + OUTPUT_PATH + "]");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java