怎么在PHP中利用Xpdf读取PDF的内容

发布时间:2020-12-25 16:16:55 作者:Leah
来源:亿速云 阅读:446

这篇文章给大家介绍怎么在PHP中利用Xpdf读取PDF的内容,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一.下载
首先,我们先把资料下下来先。
如果不需要转中文的话,只需要下载它就可以:xpdf-bin-linux-3.03.tar,如果需要转中文,那你就还需要它了:xpdf-chinese-simplified.tar
二.安装
现在,下载完毕了吧,我们可以进行安装了。
[root@localhost ~]# mkdir -p /lcf/upan
[root@localhost ~]# mkdir -p /lcf/cdrom
[root@localhost ~]# mkdir -p /lcf/xpdf
[root@localhost ~]# cd /lcf/upan/
[root@localhost upan]# cp xpdf/* ../xpdf/ (下载的文件放入/lcf/xpdf目录)
[root@localhost upan]# cd ../xpdf/
[root@localhost xpdf]# tar -zxvf xpdfbin-linux-3.03.tar.gz
[root@localhost xpdf]# cd xpdfbin-linux-3.03
[root@localhost xpdfbin-linux-3.03]# cat INSTALL
[root@localhost xpdfbin-linux-3.03]# cd bin32/
[root@localhost bin32]# cp ./* /usr/local/bin/
[root@localhost bin32]# cd ../doc/
[root@localhost doc]# mkdir -p /usr/local/man/man1
[root@localhost doc]# mkdir -p /usr/local/man/man5
[root@localhost doc]# cp *.1 /usr/local/man/man1
[root@localhost doc]# cp *.5 /usr/local/man/man5
如果不需要读取中文的话,到这里就可以结束了,如果需要,那我们继续往后
[root@localhost doc]# cp sample-xpdfrc /usr/local/etc/xpdfrc
[root@localhost xpdf]# cd /lcf/xpdf
[root@localhost xpdf]# tar -zxvf xpdf-chinese-simplified.tar.gz
[root@localhost xpdf]# cd xpdf-chinese-simplified
[root@localhost xpdf]# mkdir -p/usr/local/share/xpdf/chinese-simplified
[root@localhost xpdf]# cd xpdf-chinese-simplified/
[root@localhost xpdf-chinese-simplified]# cp Adobe-GB1.cidToUnicode ISO-2022-CN.unicodeMap EUC-CN.unicodeMap GBK.unicodeMap CMAP /usr/local/share/xpdf/chinese-simplified/
把chinese-simplified里面文件add-to-xpdfrc 的内容复制到/usr/local/etc/xpdfrc文件中。记得里面的路径要正确。(注意,这里面的简体中文包包括以下三种格式:ISO-2022-CN,EUC-CN,GBK ,看清楚哦,不支持UTF-8,可以先转为GBK,然后进行转义)

三.功能实现
至此,所有的配置完毕,我们要开始使用它了。
如果是简单的PDF读取,那么直接用下面的语句就OK了。
$content = shell_exec('/usr/local/bin/pdftotext '.$filename.' -');
如果需要转中文,如此这般,加上参数。
$content = shell_exec('/usr/local/bin/pdftotext -layout -enc GBK '.$filename.' -');
当然,加了参数之后依然是不影响英文的转换的,所以,放心使用吧。需要注意的是,这里转出来的是GBK编码的哦,现在网站很多用的是UTF-8,想要不显示乱码的话,需要再次转义一下哦。
$content = mb_convert_encoding($content, 'UTF-8','GBK');
至此,就大功告成了。读取出来的内容,你想如何使用,再写代码处理吧。
最后加一下pdftotext 的参数说明给大家。

主要参数如下:
OPTIONS
Many of the following options can be set with configuration file com-
mands. These are listed in square brackets with the description of the
corresponding command line option.
-f number
Specifies the first page to convert.
-l number
Specifies the last page to convert.
-layout
Maintain (as best as possible) the original physical layout of
the text. The default is to 'undo' physical layout (columns,
hyphenation, etc.) and output the text in reading order.
-fixed number
Assume fixed-pitch (or tabular) text, with the specified charac-
ter width (in points). This forces physical layout mode.
-raw Keep the text in content stream order. This is a hack which
often "undoes" column formatting, etc. Use of raw mode is no
longer recommended.
-htmlmeta
Generate a simple HTML file, including the meta information.
This simply wraps the text in <pre> and </pre> and prepends the
meta headers.
-enc encoding-name

关于怎么在PHP中利用Xpdf读取PDF的内容就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Unity3D读取PDF文件内容
  2. ​ php中读取文件内容的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php pdf xpdf

上一篇:使用set_include_path与get_include_path时需要注意哪些问题

下一篇:AcceptPathInfo指令怎么在APACHE中使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》