如何使用PHP从pdf中提取特定文本

时间:2016-08-02 13:09:19

标签: php html pdf

我需要在mysql表中存储候选人的名字和他的id,我用pdfparser提取了文本

<?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('C:\Desktop\Data\ApplicationForm.pdf');

$text = $pdf->getText();
echo $text;

?>

现在它只是显示提取的文本,现在我需要从页面(运行上面的程序时出现的页面)中提取名称和id,其中填充了提取的文本,在点击查看页面源我找到了我需要的是

出现在: -

tr 1115 * 15 td.line-number 31 * 15和td.line-content:1084 * 15,行号值= 12

名称存在于: -

tr 1115 * 15 td.line-number 31 * 15和td.line-content:1084 * 15,行号值= 13

我现在迷路了,因为我不知道如何获得这些信息。请帮助我。

我有多个pdf,我需要的所有信息都在同一个地方(相同的地方我的意思是行号值= 13,tr 1115 * 15 td.line-number 31 * 15和td.line-content:1084 * 15,)我只想找到解决这个问题的方法,帮助我。

如果你有任何疑问,我会澄清,如果问题似乎不清楚,我会改进它。

1 个答案:

答案 0 :(得分:0)

我需要从pdf中提取候选人的名字和他的id,所以在使用pdfparser之后我提取了文本并使用php下载了html页面

<?php
$filename = 'filename.txt';
header('Content-disposition: attachment; filename=' . $filename);
header('Content-type: text');
// ... the rest of your file
?>
<?php

// Include Composer autoloader if not already done.
include 'C:\Users\Downloads\pdfparser-master (1)\pdfparser-master\vendor\autoload.php';

// Parse pdf file and build necessary objects.
$parser = new  \Smalot\PdfParser\Parser();
$pdf    = $parser->parseFile('C:\Users\Desktop\Data\ApplicationForm (3).pdf');

$text = $pdf->getText();
echo $text;


?>

我做了这个导致我需要的信息在视图源页面的第12行和第13行,这是我需要的所有pdf,所以在以文本文件的形式下载html页面后,我使用了代码下面从下载的文件中提取我需要的文本并将其存储在数据库中

<?php

$source = file("filename.txt");

$number =$source[12];
$name = $source[13];
$gslink = "https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=google+scholar+".$name;        
$dblplink = "https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=dblp+".$name ;
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "INSERT INTO faculty (candidate_no,candidate_name,gs_link,dblp_link)VALUES('$number','$name','$gslink','$dblplink')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>