PHP經過PDFParser解析PDF文件

以前一直找到的資料都是教你怎麼生成pdf文檔,好比:TCPDFFPDFwkhtmltopdf。而我碰到的項目裏須要驗證從遠程獲取的pdf文件是否受損、文件內容是否一致這些問題,這些都不能直接提供給我讀取pdf的功能,碰巧找到了一個能夠讀取並解析PDF文檔的第三方類庫PDFParser,該類庫很是簡單好用,能夠直奔官網瞭解。php

1、安裝步驟

我這裏用的是CI框架,但均可用composer包管理方式安裝到項目中,進行開發調用html

  • 將PDFParser加入composer.json文件中

  • 項目根目錄下打開命令行並執行:composer update smalot/pdfparserjson

    若是發現update不下來,能夠修改當前項目的 composer.json 配置文件,打開命令行窗口(windows用戶)或控制檯(Linux、Mac 用戶),進入你的項目的根目錄(也就是 composer.json 文件所在目錄),執行以下命令:composer config repo.packagist composer https://packagist.phpcomposer.com,再重試。固然,若是本地沒安裝composer,請前往composer官網自行安裝。segmentfault

2、示例代碼

  • 讀取pdf文件內容、獲取文件詳情
//引入pdf解析第三方類庫
        $vendorAutoloadFile = APPPATH.'..'.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';
        require_once($vendorAutoloadFile);

        $pdfFile = '/temp/label/HnEms/LS955518275CN.pdf';
        //讀取pdf,驗證跟蹤號是否一致
        // Parse pdf file and build necessary objects.
        $parser = new \Smalot\PdfParser\Parser();
        $pdfPath = $_SERVER['DOCUMENT_ROOT'].$pdfFile;
        $pdf = $parser->parseFile($pdfPath);
        $text = $pdf->getText();
        echo $text;
        echo '<hr>';

        // Retrieve all details from the pdf file.
        $details  = $pdf->getDetails();
        var_dump($details);
        die;

執行結果展現:
windows

3、實踐應用

這裏驗證pdf文檔中跟蹤號是否和提供的一致,不一致返回假,若pdf是損壞的則返回false。函數也提供了返回異常消息。composer

$trackingNumber = 'LS955518275CN';
$pdfFile = '/temp/label/Chukou1/3559675.pdf';

//僅驗證pdf文件是否有效
//$result = verifyValidLabelPdf($trackingNumber, $pdfFile);
//驗證pdf是否有效,無效則返回無效的緣由
$result = verifyValidLabelPdf($trackingNumber, $pdfFile, true);
var_dump($result);

/**
 * 驗證面單pdf文件是否完整(文件不存在、損壞和跟蹤號不一致等狀況)
 * @param string $trackingNumber 跟蹤號
 *              eg. $trackingNumber = 'LS955518275CN';
 * @param string $pdfFile  pdf文件路徑
 * @param bool $showExceptionMessage 默認爲false,不返回異常消息,爲true時,出現異常會返回異常消息
 * @return bool  true pdf有效,false pdf無效
 *
 * Attention please : 該方法異常處理千萬不要去掉,第三方類庫PdfParser解析PDF出錯時會拋異常,
 *                     這裏的異常處理也能夠接收PdfParser拋出來的異常信息,進行友好提示
 */
function verifyValidLabelPdf($trackingNumber, $pdfFile, $showExceptionMessage = false)
{
    try{
        $pdfPath = $_SERVER['DOCUMENT_ROOT'].$pdfFile;
        //驗證文件是否存在
        if (!file_exists($pdfPath) || !is_file($pdfPath)){
            throw new Exception('pdf文件不存在');
        }

        //引入PdfParser第三方類庫
        $vendorAutoloadFile = APPPATH.'..'.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';
        require_once($vendorAutoloadFile);

        //讀取pdf,驗證跟蹤號是否一致
        // Parse pdf file and build necessary objects.
        $parser = new \Smalot\PdfParser\Parser();
        $pdf = $parser->parseFile($pdfPath);
        $text = $pdf->getText();

        //驗證跟蹤號是否一致
        if (strpos($text, $trackingNumber) === false){
            throw new Exception('跟蹤號不一致');
        }

        return true;
    }catch (Exception $ex){
        //獲取錯誤類型  pdf文件可能不存在、損壞等沒法加載
        if ($showExceptionMessage === true){
            //接收異常提示消息並返回
            $message = $ex->getMessage();
            return $message;
        }

        return false;
    }
}

4、參考資料

相關文章
相關標籤/搜索