Total Pageviews

2016/07/03

[PDFBox] Warning: You did not close a PDF Document

Problem
I am using PDFBox to covert a PDF file to TIF file. 


But getting warning message as I do PDF to TIF conversion:
1
[WARN][Finalizer][Line:481][org.apache.pdfbox.cos.COSDocument.finalize]Warning: You did not close a PDF Document


Code snippet looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 // 產TIF
 private List<String> generateTifFile(TifConversionParam params, String filePath)
          throws IOException {

      List<String> rs = null;
      PDDocument document = null;
      try {
          document = PDDocument.load(new File(params.getFile()));
          String policyNumber = params.getPolicyNumber();
          rs = writeImage(document, filePath + policyNumber, ImageType.BINARY, DPI, policyNumber);
      } catch (IOException e) {
          throw new RuntimeException(e);
      } 

      return rs;
  }

  private List<String> writeImage(PDDocument document, String outputPrefix, ImageType imageType,
          float dpi, String fileName) throws IOException {
      List<String> tifNames = new ArrayList<String>();
      PDFRenderer renderer = new PDFRenderer(document);

      for (int i = 0; i < document.getNumberOfPages(); i++) {
          BufferedImage image = renderer.renderImageWithDPI(i, dpi, imageType);

          String outputPostfix = "_" + String.format("%02d", i + 1); // 01開始
          String outputFileName = outputPrefix + outputPostfix;// 檔名: 保單號碼_0X

          tifNames.add(fileName + outputPostfix); // 回傳產了那些檔名

          ImageIOUtil.writeImage(image, outputFileName + "." + "tif", Math.round(dpi));
          image.flush();
      }
      return tifNames;
  }


Solution
You need to call close() on the PDDocument inside the finally block, if you don't then the document will not be closed properly. Also, you must close all PDDocument objects that get created. 


Updated code snippet looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 產TIF
 private List<String> generateTifFile(TifConversionParam params, String filePath)
          throws IOException {

      List<String> rs = null;
      PDDocument document = null;
      try {
          document = PDDocument.load(new File(params.getFile()));
          String policyNumber = params.getPolicyNumber();
          rs = writeImage(document, filePath + policyNumber, ImageType.BINARY, DPI, policyNumber);
      } catch (IOException e) {
          throw new RuntimeException(e);
      } finally {
          if (document != null) {
              document.close();
          }
      }

      return rs;
  }

  private List<String> writeImage(PDDocument document, String outputPrefix, ImageType imageType,
          float dpi, String fileName) throws IOException {
      List<String> tifNames = new ArrayList<String>();
      PDFRenderer renderer = new PDFRenderer(document);

      for (int i = 0; i < document.getNumberOfPages(); i++) {
          BufferedImage image = renderer.renderImageWithDPI(i, dpi, imageType);

          String outputPostfix = "_" + String.format("%02d", i + 1); // 01開始
          String outputFileName = outputPrefix + outputPostfix;// 檔名: 保單號碼_0X

          tifNames.add(fileName + outputPostfix); // 回傳產了那些檔名

          ImageIOUtil.writeImage(image, outputFileName + "." + "tif", Math.round(dpi));
          image.flush();
      }
      return tifNames;
  }  


Reference
[1] https://pdfbox.apache.org/1.8/faq.html#notclosed

No comments: