[ 
https://issues.apache.org/jira/browse/PDFBOX-1677?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13725297#comment-13725297
 ] 

Maruan Sahyoun commented on PDFBOX-1677:
----------------------------------------

Hi

# can you close PDDocument before you close the input stream
# can you do a save copy of the inputPDF byte[] in the method
# can you use a RandomAccessFile in the PDDocument.load call to lower the 
memory consumption


When the issue occurs are there multiple threads running? If there are single 
threads only is the issue still happening?

BR
Maruan
                
> PDPage.convertToImage() throw java.lang.IndexOutOfBoundsException
> -----------------------------------------------------------------
>
>                 Key: PDFBOX-1677
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-1677
>             Project: PDFBox
>          Issue Type: Bug
>          Components: PDFReader, PDModel, Utilities
>    Affects Versions: 1.8.1, 1.8.2
>         Environment: JSF1.1+ PdfBox1.8.2+Tomcat (and Weblogic)
>            Reporter: Javier García Sánchez
>              Labels: PDPage,, convertToImage(), 
> java.lang.IndexOutOfBoundsException
>   Original Estimate: 48h
>  Remaining Estimate: 48h
>
> Sometimes, a random times, the method PDPage.convertToImage() throw an 
> exception like java.lang.IndexOutOfBoundsException. 
> I said sometimes, because is a ramdom process which i cannot understand.
> This is my code:
> public byte[] getImageOutputStreamFromPdfFile (byte[] inputPDF) throws 
> IOException,Exception{
>                       logger.info("Utils.getImageOutputStreamFromPdfFile 
> (byte[] inputPDF)");
>                       //resultado
>                       byte[] result=null;
>                       
>                       if (inputPDF!=null && inputPDF.length>0){
>                               long time = System.currentTimeMillis();
>                               
>                               InputStream isPDF = null;
>                               
>                               
>                               //imagen resultado
>                               ByteArrayOutputStream imageOutputStream = null;
>                               
>                               try{
>                                       isPDF = new 
> ByteArrayInputStream(inputPDF);
>                                       imageOutputStream = new 
> ByteArrayOutputStream();
>                                       
>                                       //pdf
>                                       PDDocument document = 
> PDDocument.load(isPDF, true);
>                                       @SuppressWarnings("unchecked")
>                                       List<PDPage> pages = 
> document.getDocumentCatalog().getAllPages();
>                                       List<BufferedImage> imagesList = new 
> ArrayList<BufferedImage>(
>                                                       pages.size());
>               
>                                       PDPage pDPage = null;
>                                       BufferedImage auxImage = null;
>                                       // width y height final image 
>                                       int maxWidth = 0;
>                                       int totalHeight = 0;
>                                       for (int i = 0; i < pages.size(); i++) {
>                                               pDPage = pages.get(i);
>               
>                                               // get Image Page
>                                               auxImage = 
> pDPage.convertToImage();
>                                               
>                                               auxImage.flush();
>                                               // maxWidth
>                                               if (auxImage.getWidth() > 
> maxWidth) {
>                                                       maxWidth = 
> auxImage.getWidth();
>                                               }
>               
>                                               // totalHeight
>                                               totalHeight += 
> auxImage.getHeight();
>               
>                                               // add image to the list
>                                               imagesList.add(i, auxImage);
>                                       }
>               
>                                       BufferedImage bImageFromConvert = new 
> BufferedImage(maxWidth,
>                                                       totalHeight, 
> BufferedImage.TYPE_INT_ARGB);
>               
>                                       Graphics g = 
> bImageFromConvert.getGraphics();
>               
>                                       int auxHeightDraw = 0;
>                                       for (BufferedImage img : imagesList) {
>                                               g.drawImage(img, 0, 
> auxHeightDraw, null);
>                                               auxHeightDraw += 
> img.getHeight();
>                                       }
>               
>                                       imagesList.clear();
>                                       
>                                       ImageIO.write(bImageFromConvert, 
> ConstantesProperties.EXTENSION_IMAGE, imageOutputStream);
>                                       
>                                                                               
>                                       result=imageOutputStream.toByteArray();
>                                       
>                                                                       
>                                       
> logger.info("Utils.getImageOutputStreamFromPdfFile --> OK: Generation 
> time:"+(System.currentTimeMillis()-time));
>                                       
>                               }catch (IOException e) {
>                                       
>                                       
> logger.error("Utils.getImageOutputStreamFromPdfFile-->"+Utils.stackTraceToString(e));
>                                       result=null;
>                                       throw e;
>                                       
>                               }catch (Exception e) {
>                                       
>                                       
> logger.error("Utils.getImageOutputStreamFromPdfFile-->"+Utils.stackTraceToString(e));
>                                       result=null;
>                                       throw e;
>                                       
>                               }finally{
>                                       
>                                       if (isPDF!=null){
>                                               isPDF.close();
>                                       }
>                                       if (imageOutputStream!=null){
>                                               imageOutputStream.close();
>                                       }
>                                       
>                               }//end finally
>                       }//end if
>                       return result;
>               }//end getImageOutputStreamFromPdfFile

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to