首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php pdfparser不适用于pdf版本1.7

php pdfparser不适用于pdf版本1.7
EN

Stack Overflow用户
提问于 2016-10-13 16:48:23
回答 1查看 1.5K关注 0票数 3

我正在使用pdfparser来解析pdf文件中的文本。对于旧版本的pdf文件,它可以工作,但对于新版本的pdf文件,这个解析器不能工作。我的pdf版本是1.7

代码语言:javascript
复制
<?php
  include 'vendor/autoload.php'; 
  // Parse pdf file and build necessary objects.
  $parser = new Smalot\PdfParser\Parser();
  $pdf    = $parser->parseFile('sample.pdf'); 
  // Retrieve all pages from the pdf file.
  $pages  = $pdf->getPages(); 
  // Loop over each page to extract text.
  $content=array();
  foreach ($pages as $page) {
      $content[]= $page->getTextArray();  
    echo"<pre>";
    print_r($content);

  }
EN

回答 1

Stack Overflow用户

发布于 2019-05-25 18:34:33

我也经历过同样的行为!

现在,我使用一个工具检查pdf版本,然后再尝试解析它。如果不是1.4,我将其转换为1.4,然后进行解析。如果需要,这里有一个php库:https://github.com/xthiago/pdf-version-converter

代码示例:

代码语言:javascript
复制
function searchablePdfParser($systemPath) {
    //we save the file to a temporay file because we might need to convert it.
    $tempPath = getPathWithIdAndTimestamp($systemPath) . 'tmp.pdf';
    copy($systemPath, $tempPath);
    //check whether it needs to be converted and convert it if required
    $guesser = new RegexGuesser();
    $pdfVersion = $guesser->guess($tempPath); // will print something like '1.4'
    if ( $pdfVersion != '1.4' ) {
        $command = new GhostscriptConverterCommand();
        $filesystem = new Filesystem();
        $converter = new GhostscriptConverter($command, $filesystem);
        $converter->convert($tempPath, '1.4');
    }
    //parse the original file or the converted file if it hadn't been a pdf 1.4 version
    $parser = new \Smalot\PdfParser\Parser();
    $pdf = $parser->parseFile($tempPath);
    $text = $pdf->getText();  
    unlink($tempPath);
    if ( strlen($text) < 30 ) {
        return '';
    }
    return $text;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40016313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档