美文网首页
使用PDFParser解析PDF中的文字

使用PDFParser解析PDF中的文字

作者: 提莫小小队长 | 来源:发表于2018-05-22 14:43 被阅读0次

    官方文档(文档很清晰,建议直接看官方文档)
    https://www.pdfparser.org/documentation

    安装

    composer require smalot/pdfparser
    

    安装完成之后,在入口文件引入自动加载文件

    include 'vendor/autoload.php';  //根据自己入口文件的路径合理配置
    

    使用方法

    <?php
     
    // Include Composer autoloader if not already done.
    include 'vendor/autoload.php';
     
    // Parse pdf file and build necessary objects.
    $parser = new \Smalot\PdfParser\Parser();
    $pdf    = $parser->parseFile('document.pdf');
     
    $text = $pdf->getText();
    echo $text;
     
    ?>
    

    如何获取指定页的内容

    $parser = new \Smalot\PdfParser\Parser();       
    // 调用解析方法,参数为pdf文件路径,返回结果为Document类对象
    $document = $parser->parseFile('238.PDF');
    // 获取所有的页
    $pages = $document->getPages();
    //$pages[0]->getText();  //提取第一页的内容,想提取多页,可以按照下面的方法,用$key来控制要获取的页数
    // 逐页提取文本
    foreach($pages as $key=>$page){
        if($key === 0){
            //提取第一页的内容
            echo $pages[$key]->getText();  
        }
    }   
    
    

    相关文章

      网友评论

          本文标题:使用PDFParser解析PDF中的文字

          本文链接:https://www.haomeiwen.com/subject/riwhjftx.html