【问题标题】:Parsing PDF and getting the header portion information解析 PDF 并获取页眉部分信息
【发布时间】:2019-07-11 11:13:20
【问题描述】:

我正在尝试解析 PDF 的内容。基本上都是科研论文。

这是我想要抓取的部分:

我只需要论文标题和作者姓名。

我使用的是PDF Parser Library。而且我能够使用以下代码获取标题部分文本:

function get_pdf_prop( $file )
{
    $parser = new \Smalot\PdfParser\Parser();
    $pdf    = $parser->parseFile( $file );

    $details  = $pdf->getDetails();

    $page = $pdf->getPages()[0];

    //-- Extract the text of the first page
    $text = $page->getText();
    $text = explode( 'ABSTRACT', $text, 2 );    //-- get the text before the "ABSTRACT"
    $text = $text[0];

    //-- split the lines
    $lines = explode( "\n", $text );

    return array(
        'total_pages'   => $details['Pages'],
        'paper_title'   => $lines[0] . $lines[1],
        'author'        => $lines[2]
    );
}

我所做的是,解析第一页的全文,然后它将以纯格式返回整个文本。由于需要的内容是在ABSTRACT这个词之前,所以我尝试拆分文本,然后拆分行。

我假设前两行是标题,第三行是作者姓名。到目前为止,我在上面的屏幕截图中显示的论文给出了正确的结果。

但在以下情况下会出现问题:

  1. 如果论文标题是单行,我事先不知道。所以我的代码总是将前两行作为纸片返回。这可能会将标题和作者姓名都指定为paper_title

  2. 如果论文标题是三行,同样会出现问题。

  3. 如果作者超过 1 位,那么我的代码将不会返回正确的数据。

那么对于如何有效地从 PDF 科学论文中获取论文标题和作者姓名等数据有什么建议吗?确信他们在使用 LateX 工具创建 PDF 时都遵循相同的模式。有没有更好的解决方案或线索?

请注意,我正在尝试在上传到我网站的论文上执行此操作。我使用 PHP 作为服务器端语言。

谢谢

【问题讨论】:

  • 标题和作者详细信息之间总是有一个空行吗?寻找它可以让您处理不同数量的标题行。
  • @droopsnoot 查看代码我认为$page->getText(); 没有返回空行。本来不错的。
  • 我认为使用$page->getText();时无法解决这个问题,返回纯文本。
  • 您是否尝试过检索文档元数据 - 来自PDF Parser documentation 的示例代码块。
  • @KIKOSoftware,是的,它返回纯文本。也正因为如此,我不得不用这种拆分和猜测的方法来获取信息!

标签: php parsing pdf pdfparser


【解决方案1】:

您可以尝试使用 PDF 元数据来检索您需要的“字段”(作者、标题、其他...)。我随机尝试了几篇科学论文,它们都有(至少)页面、作者和标题的元数据。

PDF Parser docs 展示如何做到这一点:

<?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');

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

// Loop over each property to extract values (string or array).
foreach ($details as $property => $value) {
    if (is_array($value)) {
        $value = implode(', ', $value);
    }
    echo $property . ' => ' . $value . "\n";
}

?>

随机抽取纸张的样本输出 (var_dump($details)):

array(7) {
  ["Author"]=>
  string(18) "Chris Fraley et al"
  ["CreationDate"]=>
  string(25) "2011-06-23T19:20:24+01:00"
  ["Creator"]=>
  string(26) "pdftk 1.41 - www.pdftk.com"
  ["ModDate"]=>
  string(25) "2019-07-11T14:56:29+02:00"
  ["Producer"]=>
  string(45) "itext-paulo-155 (itextpdf.sf.net-lowagie.com)"
  ["Title"]=>
  string(38) "Probabilistic Weather Forecasting in R"
  ["Pages"]=>
  int(9)
}

【讨论】:

  • 谢谢。但这只有在 PDF 文件具有这些元信息时才能完成。但是我尝试过的所有这些文件都没有填写这些文件!我的意思是它在这些字段中有空数据。仅供参考,我正在从这个网站下载和测试论文:ceur-ws.org
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 2019-09-30
  • 1970-01-01
  • 2015-12-05
相关资源
最近更新 更多