从Railo中的PDF中提取文本

时间:2016-02-22 03:46:34

标签: pdf indexing lucene cfml railo

刚接管Railo网站(Railo 3.3.4.003)的编码,我想索引大量的PDF。但是,cfindex似乎只是索引文本文档。我看到有<cfpdf action="extracttext">,但显然这在Railo中不受支持。任何人都可以确认或否则?如果不是org.apache.pdfbox的最佳选择?

1 个答案:

答案 0 :(得分:0)

PDFBox肯定会完成这项工作。 Railo类路径中包含旧版本,但我发现它有问题。相反,我会使用JavaLoader加载最新版本。

<强> pdfTextExtractor.cfc

/* The latest pre-built standalone PDFBox jar file and the javaloader package are assumed to be in the same folder as the following component */
component{

    function init( javaLoaderPath="javaloader.JavaLoader" ){
        if( !server.KeyExists( "_pdfBoxLoader" ) ){
            var paths=[];
            paths.append( GetDirectoryFromPath( GetCurrentTemplatePath() ) & "pdfbox-app-1.8.11.jar" );
            server._pdfBoxLoader=New "#javaLoaderPath#"( paths );
        }
        variables.reader=server._pdfBoxLoader.create( "org.apache.pdfbox.pdmodel.PDDocument" );
        variables.stripper=server._pdfBoxLoader.create( "org.apache.pdfbox.util.PDFTextStripper" );
        return this;
    }

    string function extractText( required string pdfPath, numeric startPage=0, numeric endPage=0 ){
        if( Val( startPage ) )
            stripper.setStartPage( startPage );
        if( Val( endPage ) )
            stripper.setEndPage( endPage );
        var pdf=reader.load( pdfPath );
        var text=stripper.getText( pdf );
        reader.close();
        return text;
    }

}

有关详细信息,请参阅http://blog.simplicityweb.co.uk/94/migrating-from-coldfusion-to-railo-part-7-pdfs

以上内容也适用于Railo的继任者Lucee,我强烈建议移民。