You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Kristijan Milanović <kr...@gmail.com> on 2023/05/25 11:24:05 UTC

PDF to JPEG not working as expected

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.

Re: PDF to JPEG not working as expected

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,

Please check all the intermediate steps:
- is pdfData really a PDF? Save it into a file to check
- is the rendered image really an image? Save it into a file to check
Also look at log messages, and make sure you have the latest PDFBox 
version with all the dependencies (jbig2 decoder)
Tilman

On 25.05.2023 13:24, Kristijan Milanović wrote:
> 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.
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org