【问题标题】:PHP Smalot PdfParser get certain sectionPHP Smalot PdfParser 获取特定部分
【发布时间】:2023-02-10 03:51:24
【问题描述】:

我需要有关 PHP Smalot\PdfParser 的帮助。 https://github.com/smalot/pdfparser

有谁知道如何获取或访问某些部分。

例子。发票并希望以对象/数组的形式访问项目/产品对象部分

getText 方法有效,但会检索发票上的所有文本。

多谢!

【问题讨论】:

  • PDF 没有“部分”,它们只有页面和文本。 usage docs 展示了如何获得两者,对于后者,您还可以获得文本运行的 x,y 坐标。对于您自己定义的“部分”,由您决定一段文本是否与另一段文本足够接近。

标签: php pdf


【解决方案1】:

你可以像这样使用一些循环:

$metaData = $pdf->getDetails(); //Gets PDF metadata
$xtargetTextCoordinate = "12.345" ///////////USE YOUR OWN
$ytargetTextCoordinate = "678.90" ///////////USE YOUR OWN

//Going through each PDF's page...

for ($x=0 ; $x < $metaData['Pages']; $x++ ){ 
    //Reset variables
    $streamOfThisPage = [];
    $streamOfThisPage = $pdf->getPages()[$x]->getDataTm(); 
    $targetText= "";

    //Going through each key element of this page...
    foreach($streamOfThisPage as $arrayEle){  
        if( ($arrayEle[0][4] == $xtargetTextCoordinate ) && ($arrayEle[0][5] == $ytargetTextCoordinate ) ){  //X & Y depend of your document structure...
          $targetText = "";

          //Remove unecessary data if any
          $thisRowWords = explode(" " , $arrayEle[1] );
          $referencePeriod = $thisRowWords[0];
          foreach($thisRowWords as $position => $word){
              $targetText = $targetText . $word . " ";
          }
    }
 }
 echo $targetText;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多