php借助Xpdf读取PDF中的内容

WBOY
发布: 2016-07-25 09:03:48
原创
2004 人浏览过
  1. [root@localhost ~]# mkdir -p /lcf/upan
  2. [root@localhost ~]# mkdir -p /lcf/cdrom
  3. [root@localhost ~]# mkdir -p /lcf/xpdf
  4. [root@localhost ~]# cd /lcf/upan/
  5. [root@localhost upan]# cp xpdf/* ../xpdf/ (下载的文件放入/lcf/xpdf目录)
  6. [root@localhost upan]# cd ../xpdf/
  7. [root@localhost xpdf]# tar -zxvf xpdfbin-linux-3.03.tar.gz
  8. [root@localhost xpdf]# cd xpdfbin-linux-3.03
  9. [root@localhost xpdfbin-linux-3.03]# cat INSTALL
  10. [root@localhost xpdfbin-linux-3.03]# cd bin32/
  11. [root@localhost bin32]# cp ./* /usr/local/bin/
  12. [root@localhost bin32]# cd ../doc/
  13. [root@localhost doc]# mkdir -p /usr/local/man/man1
  14. [root@localhost doc]# mkdir -p /usr/local/man/man5
  15. [root@localhost doc]# cp *.1 /usr/local/man/man1
  16. [root@localhost doc]# cp *.5 /usr/local/man/man5
复制代码

如果不需要读取中文的话,到这里就可以结束了,如果需要,那我们继续往后

  1. [root@localhost doc]# cp sample-xpdfrc /usr/local/etc/xpdfrc
  2. [root@localhost xpdf]# cd /lcf/xpdf
  3. [root@localhost xpdf]# tar -zxvf xpdf-chinese-simplified.tar.gz
  4. [root@localhost xpdf]# cd xpdf-chinese-simplified
  5. [root@localhost xpdf]# mkdir -p/usr/local/share/xpdf/chinese-simplified
  6. [root@localhost xpdf]# cd xpdf-chinese-simplified/
  7. [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

 and 
登录后复制
and prepends the meta headers. -enc encoding-name


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!