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


Posted in PHP onNovember 24, 2012

一.下载
首先,我们先把资料下下来先。
如果不需要转中文的话,只需要下载它就可以: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 正则 过滤html 的超链接
Jun 02 PHP
discuz免激活同步登入代码修改方法(discuz同步登录)
Dec 24 PHP
php获取网页请求状态程序示例
Jun 17 PHP
php+mysqli使用面向对象方式查询数据库实例
Jan 29 PHP
基于CakePHP实现的简单博客系统实例
Jun 28 PHP
php基于双向循环队列实现历史记录的前进后退等功能
Aug 08 PHP
简单的自定义php模板引擎
Aug 26 PHP
php加密解密字符串示例
Oct 13 PHP
php性能分析之php-fpm慢执行日志slow log用法浅析
Oct 17 PHP
PHP中让json_encode不自动转义斜杠“/”的方法
Feb 28 PHP
PHP截取发动短信内容的方法
Jul 04 PHP
七种PHP开发环境搭建工具
Jun 28 PHP
PHP得到mssql的存储过程的输出参数功能实现
Nov 23 #PHP
php实现文件下载更能介绍
Nov 23 #PHP
php 定义404页面的实现代码
Nov 19 #PHP
php中的注释、变量、数组、常量、函数应用介绍
Nov 16 #PHP
apache php模块整合操作指南
Nov 16 #PHP
php获取用户IPv4或IPv6地址的代码
Nov 15 #PHP
屏蔽机器人从你的网站搜取email地址的php代码
Nov 14 #PHP
You might like
PHP的FTP学习(一)
2006/10/09 PHP
PHP 计算代码执行耗时的代码修正网上普遍错误
2011/05/14 PHP
php对二维数组按指定键值key排序示例代码
2013/11/26 PHP
php数组合并的二种方法
2014/03/21 PHP
php图形jpgraph操作实例分析
2017/02/22 PHP
实例讲解通过​PHP创建数据库
2019/01/20 PHP
php计数排序算法的实现代码(附四个实例代码)
2020/03/31 PHP
总结一些js自定义的函数
2006/08/05 Javascript
javascript自执行函数之伪命名空间封装法
2010/12/25 Javascript
node.js中的http.get方法使用说明
2014/12/14 Javascript
使用requestAnimationFrame实现js动画性能好
2015/08/06 Javascript
jquery动态切换背景图片的简单实现方法
2016/05/14 Javascript
jQuery弹出div层过2秒自动消失
2016/11/29 Javascript
解决layui的radio属性或别的属性没显示出来的问题
2019/09/26 Javascript
vue路由传参三种基本方式详解
2019/12/09 Javascript
vue中利用three.js实现全景图的完整示例
2020/12/07 Vue.js
js仿淘宝放大镜效果
2020/12/28 Javascript
python中map()函数的使用方法示例
2017/09/29 Python
在python中使用正则表达式查找可嵌套字符串组
2017/10/24 Python
django 发送邮件和缓存的实现代码
2018/07/18 Python
TensorFlow实现模型评估
2018/09/07 Python
django数据模型on_delete, db_constraint的使用详解
2019/12/24 Python
如何基于Python实现数字类型转换
2020/02/07 Python
Python表达式的优先级详解
2020/02/18 Python
OpenCV4.1.0+VS2017环境配置的方法步骤
2020/07/09 Python
Python 使用生成器代替线程的方法
2020/08/04 Python
Python3 ffmpeg视频转换工具使用方法解析
2020/08/10 Python
美国现代家具网站:Design Within Reach
2018/07/19 全球购物
家长给老师的道歉信
2014/01/13 职场文书
文秘档案管理岗位职责
2014/03/06 职场文书
学校运动会广播稿100条
2014/09/14 职场文书
领导干部“四风”查摆问题个人整改措施
2014/10/28 职场文书
2016优秀班主任个人先进事迹材料
2016/02/26 职场文书
Vue全家桶入门基础教程
2021/05/14 Vue.js
Navicat连接MySQL错误描述分析
2021/06/02 MySQL
windows11选中自动复制怎么开启? Win11自动复制所选内容的方法
2022/07/23 数码科技