2011-01-24 112 views
8

如何读取PDF文件并将内容转换为字符串?使用PHP语言。将PDF转换为字符串

+1

使用[`file_get_contents`(http://de3.php.net/manual/en/function.file-get-contents.php)如果你需要原始二进制数据或更新您的问题,并告诉我们您真正想要的。 – 2011-01-24 10:03:08

+0

我需要从pdf文件中获得干净的文本。当我从pdf文件中获取文本时,我需要在数据库中插入此文本。 – lolalola 2011-01-24 11:09:14

回答

7

您可以使用类似pdftotext附带Linux上的xpdf的包。然后POPEN命令可用于管道pdftotext的输出转换成字符串:

$mystring = ""; 
$fd = popen("/usr/bin/pdftotext blah.pdf","r"); 
if ($fd) { 
    while (($myline = fgets($fd)) !== false) { 
     $mystring .= $myline; 
    } 
} 
0

在您的服务器上安装APACHE-TIKA。 APACHE-TIKA支持更多pdf文件。 安装指南: http://www.acquia.com/blog/use-apache-solr-search-files

和最终的代码很简单:

$string = ""; 
$fd = popen("java -jar yourpathtotika/tika-app-1.3.jar -t yourpathtopdf/sample.pdf","r"); 
while (!feof($fd)) { 
$buffer = fgets($fd, 4096); 
$string .= $buffer; 
} 
echo $string; 
0

您可以使用PHP类,可以在这里找到:

http://www.pdftotext.eu

这是一个公共领域的PDF文本提取器完全用纯PHP编写,这意味着您不需要依赖外部命令。它提供了一个简单的界面,检索文字:

include ('PdfToText.phpclass') ; 
$pdf = new PdfToText ('mysample.pdf') ; 
echo "PDF contents are : " . $pdf -> Text . "\n" ;