【发布时间】:2016-01-03 08:50:51
【问题描述】:
我有一个应用程序,用户可以在其中上传pdf,然后将其转换为text 以进行进一步处理。
问题是一些上传的文件是图像pdf,所以转换它不起作用。我宁愿只发送那些被证明或检测为图像的文件,而不是发送所有 pdf 以拆分成图像然后 ocr 987654324@
更新
在寻找最终解决方案时,我遵循@Andrew的建议,计算生成的txt文件中的字数,如果小于10字则进行下一步:pdf to images for later ocr recognition,这是什么我现在正在努力...
// convert any file with pdf extension to text
$cmd = "pdftotext -eol unix '$uploadedfile'";
shell_exec($cmd);
// save original file at the orig directory
rename("$uploadedfile", "orig/$uploadedfile");
// pdftotext renames files to txt so I need the file name with txt extension
$textfile = preg_replace('"\.(pdf|PDF)$"', '.txt', $uploadedfile);
// count words on the generated txt file
$cmd = "wc -w '$textfile' | cut -f1 -d' '";
$wc = shell_exec($cmd);
// proceed if words are less than 10
if ($wc < 10)
{
//take out the pdf extension for directory creation
$imgdir = preg_replace('"\.(pdf|PDF)$"', '', $uploadedfile);
$cmd = "mkdir '$imgdir'";
shell_exec($cmd);
//change pdf extension to jpg for images creation
$imgfile = preg_replace('"\.(pdf|PDF)$"', '.jpg', $uploadedfile);
//convert pdf to images
$cmd = "convert 'orig/$uploadedfile' '$imgdir/$imgfile'";
然后就会出现ocr...
更新2 感谢@Mark-Setchell 的建议,我对代码做了一点改动,现在最后一部分是这样的:
//take out the pdf extension for directory creation
$imgdir = preg_replace('"\.(pdf|PDF)$"', '', $uploadedfile);
$cmd = "mkdir '$imgdir'";
shell_exec($cmd);
//convert pdf to images
$cmd = "pdfimages 'orig/$uploadedfile' '$imgdir/$imgdir'";
【问题讨论】:
-
好吧,尝试获取文本。如果您的尝试失败,则发送到 OCR
-
好的,所以我应该有办法检查输出文件中是否有文本或足够的文本?...任何建议...谢谢
-
您没有问题中所述的“将 PDF 转换为文本”的应用程序吗?
-
是的,pdftotext,但是这个只有当pdf内容是文本时才会转换,而不是当内容是文本图像时。