Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox: Convert PDF to Image is slower than expected

I am currently using this code to convert a pdf to an image:

@SuppressWarnings("unchecked")
public static Image convertPDFtoImage(ByteArrayInputStream bais) {

    Image convertedImage = null;

    try {

        PDDocument document = PDDocument.load(bais);
        List<PDPage> list = document.getDocumentCatalog().getAllPages();
        PDPage page = list.get(0);

        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 64);
        convertedImage = SwingFXUtils.toFXImage(image, null);

        document.close();

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

    return convertedImage;
}

Then, I show the convertedImage in an JavaFX ImageView.

Further, I need to import these two packages, while I am not using them:

import org.apache.commons.logging.LogFactory;
import org.apache.fontbox.afm.AFMParser;

Two questions:

  • Does it normally take two to three seconds to convert a simple one page PDF to Image where the DPI is set on 64 (which is not that high in my opinion)? It seems to be a bit slow.
  • Why do I need those two imports while I am not using them? If I don't import them, I get a lot of errors and the conversion does not work.

I would like to show a PDF quickly in JavaFX, and two to three seconds is just too long. Any other ways of showing a PDF in JavaFX (other than convert it to an image) are very welcome.

Any help is greatly appreciated!

like image 711
bashoogzaad Avatar asked Nov 10 '22 23:11

bashoogzaad


1 Answers

  1. I also had similar problem while converting pdf into images, I solved this by upgrading PDFBox from 1.8 to 2.0. This improved my performance by 50%. Previously my app is taking around 10 seconds to convert pdf into images and now it is taking 5 seconds. Please use following link as reference while upgrading PDFBox -
    https://pdfbox.apache.org/2.0/migration.html

  2. Additional imports are not required for PDFBox.

Regards,
Yogesh

like image 74
Yogesh Kulkarni Avatar answered Nov 14 '22 22:11

Yogesh Kulkarni