0

I use the PDFBox function, such as convertToImage, everything works fine, but PDFBox does not clear the temporary files after the conversion. In my system in the directory for temporary files "/tmp" there are many files such us +~JF132216249314633400.tmp, they are deleted only after restarting my application, but when the application continues to work, temporary files are not deleted.

PDFBox version - 1.8.15

when I use this

page.convertToImage(BufferedImage.TYPE_INT_RGB, 300)

the PDFbox library creates tmp files such as "+~JF132216249314633400.tmp"

my method:

def splitPdfToImages(file: File): List[File] = {
    val document = PDDocument.load(file)
    val pages = (for (i <- 0 until document.getNumberOfPages)
      yield document.getDocumentCatalog.getAllPages.get(i).asInstanceOf[PDPage]).toList
    val imgFiles = pages.zipWithIndex.map { case (page, i) =>
      val baos = IOUtils.createBAOS
      ImageIO.write(page.convertToImage(BufferedImage.TYPE_INT_RGB, 300), "jpg", baos)
      val bais = IOUtils.createBAIS(baos.toByteArray)
      try {
        val img = Image.fromStream(bais)
        implicit val writer = JpegWriter().withCompression(100)
        val tmpFile = File.createTempFile(s"""${file.getName.split("\\.").head}_$i""", file.getName.split("\\.").last)
        img.output(tmpFile)
      } finally {
        baos.close()
        bais.close()
      }
    }
    document.close()
    imgFiles
  }

Please help me to solve this issue.

13
  • 1
    Isn't it you who is creating files in the temp directory? File.createTempFile Oct 10, 2018 at 12:10
  • @TilmanHausherr I have successfully delete this files in the method above, because I know file name, but it is not the files that I need to delete. I need to delete files which created in page.convertToImage(BufferedImage.TYPE_INT_RGB, 300) I not know what file name is created and I can not to delete its.
    – proton
    Oct 11, 2018 at 13:28
  • Could you please look inside such a file with an editor like NOTEPAD++ and tell how it looks? I'm wondering whether it can be proven or disproven that this file is created by pdfbox. Oct 11, 2018 at 13:50
  • @TilmanHausherr it look like this: @BASEe]½FDSIGlÚÉ»ë(äGDEFÒêÕ¾\~GPOSÿ§¡ÜXGSUB#Iãµ4OS/2¼×òÈSVG Ôòû÷ÎÈcmapjÀg¾d5,cvt ¿qQH"fpgmY7OsgaspÿÿglyfÓiÙ]ñ¼headú7L6hhea3$hmtx1=ò(<loca÷ÄQl maxp4w¨ name¤»OHtpostæKf¼8Nprepú6ÀÄQBpÇ,ð_<õ èÑÁÇJÑÂ)»ÿ?þp$ ØþïXÿ?ÿ?r file example
    – proton
    Oct 12, 2018 at 14:18
  • 1
    You can try with a snapshot: repository.apache.org/content/groups/snapshots/org/apache/… I was able to delete most, but not all JF files while the program was running. Dec 13, 2018 at 13:23

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.