java实现将pdf变成一张图片在页面显示

1.需求:

由于pdf传输到前台显示不了,因此要在后台将pdf转成图片,然后输出到页面上

使用的是pdfbox

2.在pom.xml中添加如下依赖:

<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

2.接口

// 设定输出的类型
  private static final String JPG = "image/jpeg;charset=GB2312";

public static final float DEFAULT_DPI = 105;

/**
   * 查看合同附件
   * 
   * @param fileName
   * @param request
   * @param response
   * @throws Exception
   */
  @RequestMapping(value = { "/viewPdf/{fileName:.+}" }, method = RequestMethod.GET)
  public void viewPdf(@PathVariable String fileName, final HttpServletResponse request,
      final HttpServletResponse response) throws Exception {
    // String ext = StringUtils.substringAfterLast(fileName, ".");
    String path = saveUploadContractAttachPath + fileName;
    logger.debug(path);
    File file = new File(path);
    if (!file.exists()) {
      return;
    }
    response.reset(); // 非常重要
    response.setContentType(JPG);
    // if ("pdf".equalsIgnoreCase(ext)) {
    // response.setContentType(PDF);
    // }
    OutputStream out = response.getOutputStream();
    // IOUtils.write(FileUtils.readFileToByteArray(file), out);
    // 图像合并使用参数
    int width = 0; // 总宽度
    int[] singleImgRGB; // 保存一张图片中的RGB数据
    int shiftHeight = 0;
    BufferedImage imageResult = null;// 保存每张图片的像素值
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    int pageCount = doc.getNumberOfPages();
    // BufferedImage image = null;
    for (int i = 0, len = pageCount; i < pageCount; i++) {
      // image = renderer.renderImageWithDPI(i, 144);
      // ImageIO.write(image, "jpg", file);
      BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
      int imageHeight = image.getHeight();
      int imageWidth = image.getWidth();
      if (i == 0) {// 计算高度和偏移量
        width = imageWidth;// 使用第一张图片宽度;
        // 保存每页图片的像素值
        imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
      } else {
        shiftHeight += imageHeight; // 计算偏移高度
      }
      singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
      imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写入流中
    }
    doc.close();
    ImageIO.write(imageResult, "jpg", out);// 写图片

  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值