Takes very long time to convert pdf to image


#1
try {
          Document pdfAsposeDocument;
          Resolution resolution;
          JpegDevice jpegDevice;
          pdfAsposeDocument = new Document("C://test.pdf");
          Page pageForAsposePdf = pdfAsposeDocument.getPages().get_Item(1);
          resolution = new Resolution(162);
          jpegDevice = new JpegDevice(resolution, 162);
          OutputStream output = new FileOutputStream(new File("C://temp//out.svg"));
          jpegDevice.process(pageForAsposePdf, output);
          // Close the stream
          output.close();
       } catch (IOException e) {
         //handling
       }
   }

test.pdf (580.3 KB)
test.zip (536.6 KB)


#2

@ruhongcai

Thank you for contacting support.

We have noticed that you are using JpegDevice but saving the image as SVG which may be causing the trouble for you. Please try using below code snippet in your environment and then share your kind feedback with us. It takes about 8 seconds after loading the PDF document, with Aspose.PDF for Java 18.11 in our environment.

// load PDF document
Document doc = new Document("D:\\test.pdf");
long start = System.currentTimeMillis();
// instantiate an object of SvgSaveOptions
SvgSaveOptions saveOptions = new SvgSaveOptions();
// do not compress SVG image to Zip archive
saveOptions.CompressOutputToZipArchive = false;
// resultant file name
String outFileName = "D:\\Output.svg";
// save the output in SVG files
doc.save(outFileName, saveOptions);
long completed = System.currentTimeMillis();
System.out.println("Elapsed time: " + ((completed - start) / 1000) + " seconds.");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#3

@ruhongcai

Our company also facing same problem when we upload 40 pages of pdf. We have to use png type conversion instead of svg. Please give us solution for PNG file conversion.

    // Convert
    String outputFileNamePrefix = getRandomFileName();
    for(int pageIndex = 0; pageIndex < this.documentPageCount; pageIndex++) {
        // Random file
        File randomFile = getRandomFile(".png", outputFileNamePrefix, pageIndex);
        FileOutputStream randomFileOutputStream = null;
        try {
            randomFileOutputStream = new FileOutputStream(randomFile);
        }
        catch (Exception e) {
            log.error(getWorkerIdLogPrefix(), e);
            return postToWorkerWithError("Failed to open randomFile for pdf");
        }
        // Write
        Resolution resolution = new Resolution(150);
        PngDevice pngDevice = new PngDevice(resolution);
        Page page = document.getPages().get_Item(pageIndex + 1); // get_Item is 1...n
        pngDevice.process(page, randomFileOutputStream);
        BufferedImage bufferedImage;
        try {
            bufferedImage = ImageIO.read(randomFile);
        } catch (IOException e) {
            return postToWorkerWithError("Failed to get bufferedImage from randomFile");
        }

        try {
            randomFileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return postToWorkerWithError("Failed to close pdf output stream");
        }

        this.outputResult.appendDimensionToPage(pageIndex, bufferedImage.getWidth(), bufferedImage.getHeight());
    }

#4

@krishnasrihari

Thank you for contacting support.

Please visit Convert PDF Pages to PNG Images for your kind reference. If you still face any problem then share your PDF document with us so that we may investigate further to help you out.


#5

Hi Farthan,

Thanks for the sample code, it solved the problem. Thanks!


#6

Hi Farthan,

Your solution was for “svg” format, we also need to support “png” format , the
same test file and the following code run “long” time … Thanks for help!

         // load PDF document
           Document doc = new Document("C:\\temp\\test.pdf");
           long start = System.currentTimeMillis();

           Resolution  resolution = new  Resolution(300);

           java.io.OutputStream imageStream = new 
           java.io.FileOutputStream("C:\\temp\\out.png");


           // Create PngDevice object with particular resolution
           PngDevice pngDevice = new PngDevice(resolution);
           // Convert a particular page and save the image to stream
           pngDevice.process(doc.getPages().get_Item(1), imageStream);

           long completed = System.currentTimeMillis();
           System.out.println("Elapsed time: " + ((completed - start) / 1000) + " seconds.");

Thanks!

Ruhongtest.zip (536.6 KB)


#7

@ruhongcai

We have been able to reproduce the issue in our environment. A ticket with ID PDFJAVA-38236 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.