如何获取文本表单复制受保护的PDF文件或具有不同的字体



我正在使用pdfparser从PDF文件中复制文本,但是某些PDF文件受到复制或具有不同的字体,因此PDFPARSER不起作用,是否可以从复制保护的PDF中获得文本?

这是我的代码:

// Include Composer autoloader if not already done.
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new SmalotPdfParserParser();
$pdf    = $parser->parseFile('tests.pdf');
// Retrieve all pages from the pdf file.
$pages  = $pdf->getPages();
// Loop over each page to extract text.
foreach ($pages as $page) {
    echo utf8_encode($page->getText());
}
?>

尝试此代码后,我不会遇到任何错误或警告。此代码仅显示空白。我也尝试了UTF-8编码,但仍然无法正常工作?

如果PDF的作者将文档的权限标志指定为不是允许复制或提取文本和图形,则应考虑这一点。并非所有PDF软件都尊重此类限制。

smalot pdfparser 无法提取密码受保护的文件。

我找到了一个更好的解决方案(提供您的PHP服务在Linux服务器上运行(:使用命令行工具" pdftotext" (包含在" Poppler中("例如,debian或ubuntu(

它完美处理密码受保护文件(如果需要,可以选择密码(

与这样的东西一起使用,在Linux服务器上的Web服务器下方的PHP脚本内,PDF文件通过Web表单提交:

// $filepath is the full file path properly extracted from the $_FILES variable 
// after form submission.
// Expected running under Linux+Apache+PHP; if not, you may have to find your way.
if (! file_exists($filepath)) {
    // In case systemd private temporary directory feature is active.
    $filepath = '/proc/'.posix_getppid().'/root'.$filepath;
}
$cwdt = 4;  // may be better fine tuned for better column alignment
// “sudo” is necessary mostly with systemd private temporary directory
// feature. Needs proper sudoers configuration, of course.
$cmd = "sudo /usr/bin/pdftotext -nopgbrk -fixed {$cwdt} {$filepath} -";
exec($cmd, $output, $res);
print_r($output);

我不知道这是对"或具有不同字体"要求的答案,但是。

最新更新