Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot process large pdf file (100M plus) when using jMagick in java

I am using jMagick for java to process uploaded files (images and pdf files). Most images work fine but when I tried to convert large multiple-page pdf files to different size of images, it consumes very large amount of system memory (about 20G physical memory for a 200M pdf files), and failed eventually. I am setting the density to 200 since otherwise the image quality is very bad that I cannot even read the words in the output images. Below is the code I have:

  ImageInfo info = new ImageInfo(inputFilePath);
  info.setDensity(200);
  MagickImage image = new MagickImage(info);
  MagickImage[] imageFrames = image.breakFrames();
  for (MagickImage frame : imageFrames) {
    ImageInfo frameInfo = new ImageInfo();
    MagickImage frameDisplay = frame;
    frameDisplay.setFileName(outputFile);
    frameDisplay.writeImage(frameInfo);
  }

There is no error or exception in my java log, I just see the process died. I have tried to use policy.xml in /usr/share/ImageMagick-6.5.4/config/ to limit the memory usage as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
<!ELEMENT policymap (policy)+>
<!ELEMENT policy (#PCDATA)>
<!ATTLIST policy domain (delegate|coder|filter|path|resource) #IMPLIED>
<!ATTLIST policy name CDATA #IMPLIED>
<!ATTLIST policy rights CDATA #IMPLIED>
<!ATTLIST policy pattern CDATA #IMPLIED>
<!ATTLIST policy value CDATA #IMPLIED>
]>

<policymap>
    <policy domain="resource" name="memory" value="256MB"/>
</policymap>

But somehow it does not seem to work. I am using ImageMagick-6.5.4. Really appreciate any advice!

like image 727
Roy Cheng Avatar asked Dec 19 '25 13:12

Roy Cheng


1 Answers

<policymap>
    <policy domain="resource" name="memory" value="256MB"/>
</policymap>

Try updating this section to:

<policymap>
    <policy domain="resource" name="memory" value="1000MB"/>
</policymap>

and try again. I'm presuming when you said 'The JVM restarted', you mean that it crashed. Probably an OutOfMemory exception. No one can say for sure with the information you've provided.

like image 174
Amir Afghani Avatar answered Dec 22 '25 04:12

Amir Afghani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!