java 将PDF转换为JPEG

k5ifujac  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(189)

我试图创建一个转换过程的base64的pdf到base64的jpeg图像。我的问题是,如果PDF扫描小于600dpi,图像无法正确渲染,它是没有任何内容的白色图像。
这是我的代码:

public String convertPDFtoJPEG(byte[] pdfData) {
    try {
      PDDocument document = PDDocument.load(pdfData);
      PDFRenderer renderer = new PDFRenderer(document);

      for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) {
        BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", baos);

        baos.flush();

        String base64JPEG = Base64.getEncoder().encodeToString(baos.toByteArray());

        baos.close();
      }

      document.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return base64JPEG;

  }

 public byte[] readBase64FromFile(String filePath) {
    File file = new File(filePath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
      String line;
      while ((line = reader.readLine()) != null) {
        baos.write(line.getBytes());
      }
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return Base64.getDecoder().decode(baos.toByteArray());
  }

这就是我调用这些方法的方式:

String filepath = "path:\\to\\file.txt"
byte[] pdfData = readBase64FromFile(filePath);

convertPDFtoJPEG(pdfData);

在file.txt中是pdf的base64。

juud5qan

juud5qan1#

您的代码存在几个问题:
1.您没有正确解码base64数据。您逐行读取文件并将(每行)转换为字节。然后在最后,你把整个东西传递给Base64解码器,把它转换回字符串。您应该将方法更改为:

public static byte[] readBase64FromFile(String filePath) {
 File file = new File(filePath);
 StringBuilder base64StringBuilder = new StringBuilder();

 try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
     String line;
     while ((line = reader.readLine()) != null) {
         base64StringBuilder.append(line.getBytes());
     }
 } catch (IOException e) {
     e.printStackTrace();
 }
 return Base64.getDecoder().decode(base64StringBuilder.toString());
}

或者将整个代码作为二进制代码读取:

public static byte[] readBinaryFromFile(String filePath) {

 Path path2Pdf = Paths.get(filePath);

 byte[] pdf = new byte[]{};
 try {
     pdf = Files.readAllBytes(path2Pdf);
 } catch (IOException e) {
     e.printStackTrace();
 }

 return pdf;
}

1.你的convertPDFtoJPEG方法没有意义。您正在阅读整个文件,但只返回最后一页(甚至可能是白色页)。你应该返回一个base64 jpeg页面的列表:

public static List<String> convertPDFtoJPEG(byte[] pdfData) {
 List<String> listOfBase64Images = new ArrayList<>();

 try (PDDocument document = PDDocument.load(pdfData)){
     PDFRenderer renderer = new PDFRenderer(document);

     for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) {
         BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ImageIO.write(image, "JPEG", baos);
         listOfBase64Images.add(Base64.getEncoder().encodeToString(baos.toByteArray()));
     }
 } 
 catch (IOException e) {
     e.printStackTrace();
 }
 return listOfBase64Images;

}
仅供参考:在ByteArrayOutputStream上调用close()和flush()不会做任何事情。

相关问题