使用php将pdf文件转换为txt文件

发布于 2024-07-09 16:04:33 字数 846 浏览 5 评论 0原文

有一个程序,pdftotext,可以将 pdf 文件转换为文本文件。 直接在 Linux 控制台上使用它:

pdftotext file.pdf

这将在与 pdf 文件相同的目录中生成 file.txt。 我正在寻找一种从 php 程序内部执行此操作的方法,经过一番谷歌搜索后,我得到了两个应该对我有用的命令:system()exec() >。 所以我用这个创建了一个 php 文件:

<?php
    system('pdftotext file.pdf');
?>

但是当我运行这段代码时,它不起作用。 没有创建 txt 文件。 所以我尝试用另一个命令创建一个测试文件:

<?php
    system('touch test.txt');
?>

这工作得很好。 我也使用了 exec() 并且结果是相同的。 为什么不起作用?

编辑:按照 RoBorg 的建议,我在命令中添加了 2>&1 参数,因此:

<?php
    system('pdftotext file.pdf 2>&1');
?>

它打印了一条错误消息:

pdftotext:加载共享时出错 库:libfontconfig.so.1:不能 打开共享对象文件:没有这样的文件 或目录

服务器上似乎缺少某些内容。

There's this program, pdftotext, that can convert a pdf file to a text file. To use it directly on the linux console:

pdftotext file.pdf

That will generate a file.txt on the same directory as the pdf file. I was looking for a way to do it from inside a php program, and after some googling I ended with two commands that should work for me: system() and exec(). So I made a php file with this:

<?php
    system('pdftotext file.pdf');
?>

But when I run this code, it doesn't work. No txt file is created.
So I tried to create a test file with another command:

<?php
    system('touch test.txt');
?>

This worked fine. I've also used exec() and the results were the same. Why doesn't it work?

EDIT: following RoBorg advice, i added the 2>&1 argument to the command, so:

<?php
    system('pdftotext file.pdf 2>&1');
?>

it printed a error message:

pdftotext: error while loading shared
libraries: libfontconfig.so.1: cannot
open shared object file: No such file
or directory

Seems like something is missing on the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿透光 2024-07-16 16:04:33

这可能是一个权限问题,但请尝试以下操作:

<?php
    system('pdftotext file.pdf 2>&1');
?>

2>&1 将 stderr 重定向到 stdout,因此将打印任何错误消息。 从那时起,修复起来应该很容易。

It's probably a permissions issue, but try this instead:

<?php
    system('pdftotext file.pdf 2>&1');
?>

The 2>&1 redirects stderr to stdout, so any error messages will be printed. It should be pretty easy to fix from then on.

累赘 2024-07-16 16:04:33

安装这个。 它为我解决了问题。

http://www.ssforge.com/ssforge-standard/onlinehelp /help/faq/libstdc.html

现在,pdftotext 工作得很好。

install this. it solved the problem for me.

http://www.ssforge.com/ssforge-standard/onlinehelp/help/faq/libstdc.html

now, pdftotext works great.

满天都是小星星 2024-07-16 16:04:33

PHP 有一个内置的 PDF 函数库,应该能够满足您的需要:
https://www.php.net/pdf

PHP has a build in PDF function library, that should be able to give you what you need:
https://www.php.net/pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文