使用Spring/Java从base64字符串显示PDF [英] Displaying PDF from base64 string with Spring/Java

查看:1013
本文介绍了使用Spring/Java从base64字符串显示PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中将PDF保存为base 64 CLOB.

I have a PDF saved as a base 64 CLOB in a database.

作为一项功能测试,我只是试图使其显示在我的浏览器中.我在控制器中创建了一个新端点,然后将base64字符串放入控制器中,甚至没有从数据库中获取PDF,如下所示:

As a functional test, I'm just trying to get it to display in my browser. I made a new endpoint in my controller and just put the base64 String into the controller, without even getting the PDF from the database, that looks like this:

@RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf")
public void makePDF(HttpServletResponse response) throws Exception {
    String value = "R04jArrrw45jNH6bV02="; //<--This is longer, but I shortened it for this question
    byte[] imageByte = value.getBytes();        
    response.setContentType("application/pdf");
    response.setContentLength(imageBytes.length);
    response.getOutputStream().write(imageBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

每当我碰到端点时,都会收到一条Failed to load PDF document消息.我不知道为什么.

Whenever I hit the endpoint, I get a Failed to load PDF document message. I can't figure out why.

我对此很陌生,因此我很难弄清楚下一步是什么.如何获取PDF在网络浏览器中显示?

I'm pretty new to this, so I'm having trouble figuring out what my next steps are. How do I get the PDF to display in the web browser?

编辑

通过将我的方法修改为以下内容,我能够使它工作:

I was able to get this working, by using modifying my method to the following:

@RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf")
public void makePDF(HttpServletResponse response) throws Exception {
    try {
      String value = "R04jArrrw45jNH6bV02="; //<--This is longer, but I shortened it for this question
      byte[] image = Base64.decodeBase64(value.getBytes());

      Document document = new Document();
      document.setPageSize(PageSize.LETTER);
      PdfWriter.getInstance(document, response.getOutputStream());

      Image labelImage = Image.getInstance(image);
      labelImage.setAlignment(Image.TOP);
      labelImage.scalePercent(new Float("35"));

      document.open();
      document.add(labelImage);

      response.setContentType("application/pdf");
      response.setContentLength(imageBytes.length);
      response.getOutputStream().write(image);

      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
}

试图准确地了解我在这里所做的事情以及它为什么起作用.显然与Base64解码和使用Document对象有关.

Trying to understand exactly what I'm doing here, and why it worked. Obviously has something to do with Base64 decoding, and using the Document object.

推荐答案

堆栈溢出帖子不应将二进制数据存储在Clobs中

Stack Overflow post Blob vs Clob and why you should not store binary data in Clobs

PDF具有基于文本的常规结构.但是,PDF文件可以包含非ASCII(二进制")数据,并且应始终被视为二进制文件.-很抱歉,无法为此找到真相链接.

PDF has a general text-based structure. However, PDF files can contain non-ASCII ("binary") data and should always be considered binary files, - sorry unable to find a source of truth link for this.

在这种情况下,有可能对数据进行有损编码,并对解码的Base-64进行解码.

There is potential for a lossy encoding of data, and decoding encoded Base-64 in that case.

在流式传输之前使用Base64进行解码

Decode using Base64 before you stream it out

Base64.decode(base64String, Base64.NO_WRAP)

这篇关于使用Spring/Java从base64字符串显示PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆