Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache Batik将SVG转换为PNG,然后使用PDFBox连接到PDF,而不保存图像_Java_Spring_Pdfbox_Batik - Fatal编程技术网

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

Java 使用Apache Batik将SVG转换为PNG,然后使用PDFBox连接到PDF,而不保存图像,java,spring,pdfbox,batik,Java,Spring,Pdfbox,Batik,因此,正如标题所说,我正在寻找一种方法,使用ApacheBatik将SVG转换为PNG,然后使用PDFBox将此图像附加到PDF文件,而无需在任何地方实际创建SVG和PNG 目前,我有一个web表单,其中包含SVG图像和可选部分。 提交表单时,我使用svg的“html”部分,这意味着我将类似于的东西保留在一个字符串中,然后Spring使用该字符串在给定文件夹中创建一个“.svg”文件,然后Batik在同一文件夹中创建一个PNG文件,然后PDFBox将其附加到PDF中-这很好(代码如下) 正如您所

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

目前,我有一个web表单,其中包含SVG图像和可选部分。 提交表单时,我使用svg的“html”部分,这意味着我将类似于
的东西保留在一个字符串中,然后Spring使用该字符串在给定文件夹中创建一个“.svg”文件,然后Batik在同一文件夹中创建一个PNG文件,然后PDFBox将其附加到PDF中-这很好(代码如下)


正如您所看到的,有很多文件和东西(需要稍微整理一下),但是不需要创建和不断获取svg和png文件,就可以在运行中完成这项工作吗?

考虑到我在评论中选择使用ByteArrayOutputStream、ByteArrayInputStream、BuffereImage和无损工厂的建议。它比保存要慢一点(如果您在调试中使用它,就像BuffereImage在创建映像之前先进入假日一样)。 我发现使用的来源是:和


根据提供的评论和链接,我也解决了这个问题。我只是想发布一个简单但完整的例子,供将来想复习的人参考

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();
        }
    }
}
公共类应用程序{
私有静态字符串输出\u PATH=“D:\\so52875145\\OUTPUT\\PdfWithPngImage.pdf”;
公共静态void main(字符串[]args){
试一试{
//获取SVG源代码(此处为硬编码,但OP将从表单数据获取字符串)
字节[]svgByteArray=“.getBytes();
System.out.println(“将svg转换为字节数组…”);
//将SVG转换为PNG图像
PNGTranscoder PNGTranscoder=新PNGTranscoder();
ByteArrayOutputStream os=新建ByteArrayOutputStream();
pngTranscoder.transcode(新的转码输入(新的ByteArrayInputStream(svgByteArray)),新的转码输出(os));
System.out.println(“将svg转换为png…”);
//创建PDF,并向其中添加页面
PDDocument pdf=新的PDDocument();
pdf.addPage(新的PDPage());
//使用转码的ByteArray流生成内存中的PDF图像对象
BuffereImage BuffereImage=ImageIO.read(新的ByteArrayInputStream(os.toByteArray());
PDImageXObject pdImage=LosslessFactory.createFromImage(pdf,BuffereImage);
System.out.println(“创建的PDF图像对象…”);
//将内存中的PDF图像对象写入PDF页面
PDPageContentStream contents=新的PDPageContentStream(pdf,pdf.getPage(0));
contents.drawImage(pdImage,0,0);
contents.close();
System.out.println(“将PDF图像对象写入PDF…”);
保存(输出路径);
pdf.close();
System.out.println(“将PDF保存到路径=[“+输出路径+”]);
}
捕获(例外e){
e、 printStackTrace();
}
}
}

蜡染可以创建缓冲图像吗?您可以将其与无损工厂一起使用。如果不使用,则使用ByteArrayInputStream和ByteArrayInputStream。另一种选择:这会更好,因为它可以保留矢量图形,因此在任何分辨率下都会非常好看。@Tilmahauser谢谢您的建议-我想我现在可以使用了
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();
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();
        }
    }
}