【问题标题】:how to extract text from pdf using ghostscript in php如何使用php中的ghostscript从pdf中提取文本
【发布时间】:2017-03-17 17:15:52
【问题描述】:

我正在尝试使用以下命令从 pdf 中提取文本,但它不起作用并返回 null。

$text = shell_exec(gs -q -sDEVICE=txtwrite -dBATCH -dNOPAUSE -dFirstPage='.(int)$page_number.' -dLastPage='.(int)($page_number+1).' -sOutputFile=textfilename.txt exemple.pdf');

【问题讨论】:

    标签: php pdf ghostscript


    【解决方案1】:

    您没有字符串作为参数,您需要在将值传递给命令之前对其进行转义。最后,您需要指定一个输出文件,在这种情况下,您希望数据转到STDOUT 以供 PHP 访问。

    $first_page = escapeshellarg((int)$page_number);
    $last_page = escapeshellarg($page_number + 1);
    $text = shell_exec("gs -q -sDEVICE=txtwrite -dBATCH -dNOPAUSE -dFirstPage=$first_page -dLastPage=$last_page -sOutputFile=%stdout exemple.pdf");
    

    【讨论】:

    • 您能告诉我如何阅读 PDF 吗?或者告诉我从pdf中提取文本的方法?
    • 没有。我想有资源可以回答这个问题。
    • 原帖中的命令行是读取 PDF (ememple.pdf) 并将其发送到 txtwrite 设备,该设备提取文本并将结果写入“OutputFile”。所以它做OP想要的。问题是 mikem32 不小心将 OutputFile 开关和输入文件名折叠为 -sOutputFile 的单个参数。你需要“-sOutputFile=textfilename.txt exemple.pdf”
    • 谢谢@KenS。基于on documentation,我使用了文件名%stdout,它应该直接输出文本供PHP使用。
    • 谢谢大家。我得到了输出,但有一个问题。我收到中文文本:(
    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多