Remove PDFont caching with Apache tika

五迷三道 提交于 2019-12-02 20:15:33

问题


I am trying to extract text only from a number of different coduments (rtf doc pdf). I naturally turned to Apache Tika because it can autodetect the document and extract text accordingly. I am only interested in the text and not formatting etc.

My application ends up with a big memory leak and on investigating it, this is coming from caching from PDFFont class from the PDFBox dependency. I am not interesting in caching Fontmetrics and other Font formatting issues from pdfs as I want to only extract the text.

I am using tika 1.12. Does anyone know how to get around this cahcing issue. This is how I am using Autodetect:

        AutoDetectParser parser = new AutoDetectParser();

        BodyContentHandler handler = new BodyContentHandler(-1);
        Metadata metadata = new Metadata();
        FileInputStream inputstream = new FileInputStream(new File(child.getPath()));
        ParseContext context = new ParseContext();              
        parser.parse(inputstream, handler, metadata, context);
        String s=null;
        s =handler.toString();
        handler=null;
        context=null;
        inputstream.close();
        PDFont.clearResources();

回答1:


So I fudged a workaround and just called System.gc(); everytime the file had finished being processed which works a treat but doesn't really answer the question.



来源:https://stackoverflow.com/questions/39910781/remove-pdfont-caching-with-apache-tika

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!