如何使用PHP从PDF文件中提取突出显示的文本?

时间:2015-12-10 19:58:53

标签: php pdf pdf-generation fpdf pdflib

我想创建一个Web应用程序来从PDF文件中提取突出显示的文本。我已将fpdf和PDFlib用于多种用途,但我发现它们对此没有帮助。请告诉我如何做到这一点。或者至少告诉我哪些PHP库或框架可以支持它。即使有任何我可以用于此目的的API,我也想知道。我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用SetaPDF-Extractor组件(我们的商业产品!)执行此操作。它允许您访问突出显示注释,您可以使用该注释为提取过程创建特定过滤器。一个简单的示例脚本可能如下所示:

<?php
// load and register the autoload function
require_once('library/SetaPDF/Autoload.php');

// create a document instance
$document = SetaPDF_Core_Document::loadByFilename('path/to/the/highligted.pdf');
// initate an extractor instance
$extractor = new SetaPDF_Extractor($document);

// get page documents pages object
$pages = $document->getCatalog()->getPages();

// we are going to save the results in this variable
$results = array();

// iterate over all pages
for ($pageNo = 1, $pageCount = $pages->count(); $pageNo <= $pageCount; $pageNo++) {
    // get the page object
    $page = $pages->getPage($pageNo);
    // get the highlight annotations
    $annotations = $page->getAnnotations()->getAll(SetaPDF_Core_Document_Page_Annotation::TYPE_HIGHLIGHT);

    // create a strategy instance
    $strategy = new SetaPDF_Extractor_Strategy_Word();
    // create a multi filter instance
    $filter = new SetaPDF_Extractor_Filter_Multi();
    // and pass it to the strategy
    $strategy->setFilter($filter);

    // iterate over all highlight annotations
    foreach ($annotations AS $annotation) {
        /**
         * @var SetaPDF_Core_Document_Page_Annotation_Highlight $annotation
         */
        $name = $annotation->getName();

        // iterate over the quad points to setup our filter instances
        $quadpoints = $annotation->getQuadPoints();
        for ($pos = 0, $c = count($quadpoints); $pos < $c; $pos += 8) {
            $llx = min($quadpoints[$pos + 0], $quadpoints[$pos + 2], $quadpoints[$pos + 4], $quadpoints[$pos + 6]);
            $urx = max($quadpoints[$pos + 0], $quadpoints[$pos + 2], $quadpoints[$pos + 4], $quadpoints[$pos + 6]);
            $lly = min($quadpoints[$pos + 1], $quadpoints[$pos + 3], $quadpoints[$pos + 5], $quadpoints[$pos + 7]);
            $ury = max($quadpoints[$pos + 1], $quadpoints[$pos + 3], $quadpoints[$pos + 5], $quadpoints[$pos + 7]);

            // Add a new rectangle filter to the multi filter instance
            $filter->addFilter(
                new SetaPDF_Extractor_Filter_Rectangle(
                    new SetaPDF_Core_Geometry_Rectangle($llx, $lly, $urx, $ury),
                    SetaPDF_Extractor_Filter_Rectangle::MODE_CONTACT,
                    $name
                )
            );
        }
    }

    // if no filters for this page defined, ignore it
    if (0 === count($filter->getFilters())) {
        continue;
    }

    // pass the strategy to the extractor instance
    $extractor->setStrategy($strategy);
    // and get the results by the current page number
    $pageResult = $extractor->getResultByPageNumber($pageNo);

    // group the resulting words in an result array
    foreach ($pageResult AS $word) {
        $results[$pageNo][$word->getFilterId()][] = $word->getString();
    }
}

// debug output
echo '<pre>';
foreach ($results AS $pageNo => $annotationResults) {
    echo 'Page No #' . $pageNo . "\n";
    foreach ($annotationResults AS $name => $words) {
        echo '  Annotation name: ' . $name . "\n";
        echo '    Result: ' . join(' ', $words). "\n";
        echo '<br />';
    }
}
echo '</pre>';

输出是对每个高亮注释的所有找到的单词的简单转储。