PHP读取PDF内容配合Xpdf的使用

 更新时间:2012年11月24日 14:20:27   作者:  
最近领导拍脑袋想出了一个需求,要我读取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实现的短网址算法分享

    php实现的短网址算法分享

    这篇文章主要介绍了php实现的短网址算法,理论上支持1,073,741,824个短网址,个人使用足够了,需要的朋友可以参考下
    2014-06-06
  • PHP中token的生成案例

    PHP中token的生成案例

    这篇文章主要介绍了PHP中token的生成案例,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • CI框架实现优化文件上传及多文件上传的方法

    CI框架实现优化文件上传及多文件上传的方法

    这篇文章主要介绍了CI框架实现优化文件上传及多文件上传的方法,结合实例形式详细分析了CI框架优化文件上传及多文件上传的实现思路与具体操作步骤,需要的朋友可以参考下
    2017-01-01
  • PHP模板引擎Smarty内建函数详解

    PHP模板引擎Smarty内建函数详解

    这篇文章主要介绍了PHP模板引擎Smarty内建函数用法,结合实例形式分析了smarty中常见的内建函数功能,定义与使用方法,需要的朋友可以参考下
    2016-04-04
  • PHP文件缓存smarty模板应用实例分析

    PHP文件缓存smarty模板应用实例分析

    这篇文章主要介绍了PHP文件缓存smarty模板应用方法,结合实例形式较为详细的分析了smarty模板缓存的相关使用技巧,需要的朋友可以参考下
    2016-02-02
  • laravel框架 laravel-admin上传图片到oss的方法

    laravel框架 laravel-admin上传图片到oss的方法

    今天小编就为大家分享一篇laravel框架 laravel-admin上传图片到oss的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • PHP如何使用JWT做Api接口身份认证的实现

    PHP如何使用JWT做Api接口身份认证的实现

    这篇文章主要介绍了PHP如何使用JWT做Api接口身份认证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • php设计模式之正面模式实例分析【星际争霸游戏案例】

    php设计模式之正面模式实例分析【星际争霸游戏案例】

    这篇文章主要介绍了php设计模式之正面模式,结合星际争霸游戏案例形式分析了php正面模式相关原理、使用技巧与操作注意事项,需要的朋友可以参考下
    2020-03-03
  • php分页函数示例代码分享

    php分页函数示例代码分享

    这篇文章主要介绍了php分页函数示例代码,需要的朋友可以参考下
    2014-02-02
  • Thinkphp3.2简单解决多文件上传只上传一张的问题

    Thinkphp3.2简单解决多文件上传只上传一张的问题

    下面小编就为大家带来一篇Thinkphp3.2简单解决多文件上传只上传一张的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论