解析PDF并获取标题部分信息

时间:2019-07-11 11:13:20

标签: php parsing pdf pdfparser

正在尝试解析PDF的内容。基本上,它们是科学研究论文。

这是我要抓住的部分:

enter image description here

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

我使用的是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作为服务器端语言。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用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)
}
相关问题