【发布时间】:2018-10-18 13:26:18
【问题描述】:
正如标题所说,我正在寻找一种使用 Apache Batik 将 SVG 转换为 PNG 的方法,然后使用 PDFBox 将此图像附加到 PDF 文件,而无需在任何地方实际创建 svg 和 png。
目前我有一个包含 SVG 图像的 Web 表单,其中包含可选部分。
提交表单后,我会使用 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 文件的情况下在运行时执行此操作?
【问题讨论】:
-
BATIK 可以创建 BufferedImage 吗?您可以将它与 LosslessFactory 一起使用。
-
如果没有,则使用 ByteArrayOutputStream 和 ByteArrayInputStream。
-
另一种选择:stackoverflow.com/questions/31718075/… 那会更好,因为它会保留矢量图形,因此在任何分辨率下看起来都很棒。
-
@TilmanHausherr 感谢您的建议 - 认为我现在可以正常工作了