Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用pdftotext将PDF文件转换为文本不会产生干净的输出_Php_Linux_Pdf - Fatal编程技术网

在PHP中使用pdftotext将PDF文件转换为文本不会产生干净的输出

在PHP中使用pdftotext将PDF文件转换为文本不会产生干净的输出,php,linux,pdf,Php,Linux,Pdf,我尝试了我能找到的所有程序和脚本来将pdf文件转换成文本。最后我想试试xpdf的pdftotext。首先,让pdftotext工作起来非常容易。您只需将二进制文件复制到linux服务器并运行它。您不必安装它或具有任何系统权限。然后从PHP我做了以下工作 $command = "<file location>/pdftotext $file_pdf $file_text"; exec($command); pdftotext上有很多选项,但我找不到任何可以帮我清除垃圾的选项。有很

我尝试了我能找到的所有程序和脚本来将pdf文件转换成文本。最后我想试试xpdf的pdftotext。首先,让pdftotext工作起来非常容易。您只需将二进制文件复制到linux服务器并运行它。您不必安装它或具有任何系统权限。然后从PHP我做了以下工作

$command = "<file location>/pdftotext   $file_pdf $file_text";
exec($command);
pdftotext上有很多选项,但我找不到任何可以帮我清除垃圾的选项。有很多例子。

圣格"!Gmc?nTääL|3:ïBïL0aï7ÃE+Ú0Ú/Ä;7ù3\240;êc MÜcÜr%,EÛc×可能PDF被锁定或由无法识别为文本的图像组成,请尝试检查此项。

使用Tesseract!在Linux提示符下作为可执行文件工作。任何可以调用操作系统的语言都可以简单地调用它。PHP可以做到这一点。要安装Tesseract,您需要Leptonica,而Leptonica又需要各种库。我所经历的依赖性地狱已经融入到这段代码中,如果用最新版本替换文件名(这段代码从2014年1月10日起就可以正常工作),那么这段代码应该可以正常工作。您确实需要GCC来构建,可能还需要root/sudo:

# Home page: https://code.google.com/p/leptonica/
# Download page: http://www.leptonica.com/download.html
yum install libpng-devel
yum install libtiff-devel
wget http://www.leptonica.com/source/leptonica-1.69.tar.gz
tar xzf leptonica-1.69.tar.gz
./configure
make
make install
export LIBLEPT_HEADERSDIR=/usr/local/include # Add to .profile.

# Home page: http://code.google.com/p/tesseract-ocr/
yum install libtool
tar xzf tesseract-3.02.tar.gz # Makes tesseract-ocr directory, no version.
tar xzf tesseract-3.02.eng.tar.gz # Makes tesseract-ocr/tessdata directory.
cd tesseract-ocr
./autogen.sh
./configure --with-extra-libraries=/usr/local/lib
make
make install
export TESSDATA_PREFIX=/export/home/xxxxxx/build/tesseract-ocr # Add to .profile.
tesseract –v # Output will verify also that Leptonica is installed correctly.

我用这个解决方案取得了惊人的效果。

有问题的pdf有文本。我找到的最好的解决办法接近我所拥有的。我想记录是否有人发现了这一点,并有类似的问题。James Madison提出的解决方案非常好,如果我有图像,并且想从图像中提取文本,那么他的解决方案是+1,尽管我没有尝试过

我所做的是使用这一行创建一个文件

$command = "/<my location>/pdftotext -layout  $file_pdf $file_text";
exec($command);
$command=“//pdftotext-layout$file\u pdf$file\u text”;
exec($command);
然后用这个代码过滤它

$text = file_get_contents ($file_text);
$text = preg_replace('/[^ ]{14}[^ ]*/', '', $text);
$text = preg_replace('/[^a-zA-Z0-9\s]/', "", $text);
$text = preg_replace('/\n[\s]*/',"\n",$text); // remove all leading blanks]
$text = wordwrap($text,150);
$text = str_replace("\n", "\n<br>", $text);
$text = preg_replace('/<br>..?.?\n/',"",$text);// remove lines with 1,2, or 3 characters
$text=文件获取内容($file\u text);
$text=preg_replace('/[^]{14}[^]*/',''.$text);
$text=preg_replace('/[^a-zA-Z0-9\s]/','',$text);
$text=preg_replace('/\n[\s]*/',“\n”,$text);//删除所有前导空格]
$text=wordwrap($text,150);
$text=str\u replace(“\n”,“\n
”,$text); $text=preg_replace('/
。?\n/','',$text);//删除包含1、2或3个字符的行
能否使用Acrobat Pro从该PDF中复制合理的文本?如果没有,则缺少将此文件中使用的私有编码转换为“纯文本”所需的数据。如果是这样,你需要手动找出哪个代码代表哪个字符,然后自己翻译。我试过Acrobat Pro,你是对的。我得到了同样的结果,所以我想我做得很好,因为我可以用输入文件。我的过滤器能过滤掉大部分的坏东西,所以我猜就是这样。它有什么作用?它是否创建PDF图像并对图像进行OCR?还是像pdftotext那样提取文本?啊,很好。它是OCR的。不仅仅是提取文本。如果您只想从PDF中提取文本,请考虑:,我在这方面也取得了巨大成功。PDF box甚至可以做一些巧妙的事情,比如理解表结构和有页脚的页面边界等等。你有没有比较过PDFbox和XPDF中的pdftotext?我从来没有使用过pdftotext。谢谢你的指点。如果PDF框无法满足需要,我会。
$text = file_get_contents ($file_text);
$text = preg_replace('/[^ ]{14}[^ ]*/', '', $text);
$text = preg_replace('/[^a-zA-Z0-9\s]/', "", $text);
$text = preg_replace('/\n[\s]*/',"\n",$text); // remove all leading blanks]
$text = wordwrap($text,150);
$text = str_replace("\n", "\n<br>", $text);
$text = preg_replace('/<br>..?.?\n/',"",$text);// remove lines with 1,2, or 3 characters