Hi. I'm trying to convert base64 of PDF to base64 of JPEG.

These are my methods:

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());
  }

This is how I'm calling this methods:


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

convertPDFtoJPEG(pdfData);


In file.txt is base64 of pdf that I want to convert.

Problem that I have is if a document is scanned with less than 600dpi,
that pdf doesn't convert correctly to image. Image is white without
content.

Reply via email to