欢迎光临
我们一直在努力

PHP使用PDFParser解析PDF中的文字

安装

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();  
    }
}   
分享到:更多 ()