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在XP下IIS和Apache2服务器上的安装
Sep 05 PHP
PHP 和 MySQL 开发的 8 个技巧
Oct 09 PHP
PHP 远程文件管理,可以给表格排序,遍历目录,时间排序
Aug 07 PHP
php一维二维数组键排序方法实例总结
Nov 13 PHP
php实现在线通讯录功能(附源码)
May 13 PHP
Yii数据读取与跳转参数传递用法实例分析
Jul 12 PHP
php实现数组纵向转横向并过滤重复值的方法分析
May 29 PHP
php利用ffmpeg提取视频中音频与视频画面的方法详解
Jun 07 PHP
PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别分析
Jul 03 PHP
PHP实现redis限制单ip、单用户的访问次数功能示例
Jun 16 PHP
yii框架使用分页的方法分析
Jul 25 PHP
Laravel 5.5 实现禁用用户注册示例
Oct 24 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 版本]
2007/03/20 PHP
PHP 工厂模式使用方法
2010/05/18 PHP
php fsockopen伪造post与get方法的详解
2013/06/14 PHP
php检测文件编码的方法示例
2014/04/25 PHP
PHP实现利用MySQL保存session的方法
2014/08/23 PHP
PHP实现的数组和XML文件相互转换功能示例
2018/03/15 PHP
DOM精简教程
2006/10/03 Javascript
超强的IE背景图片闪烁(抖动)的解决办法
2007/09/09 Javascript
通过修改referer下载文件的方法
2008/05/11 Javascript
小试JQuery的AutoComplete插件
2011/05/04 Javascript
js实现带搜索功能的下拉框实时搜索实时匹配
2013/11/05 Javascript
浅谈 jQuery 事件源码定位问题
2014/06/18 Javascript
node.js开机自启动脚本文件
2014/12/24 Javascript
javascript实时获取鼠标坐标值并显示的方法
2015/04/30 Javascript
javascript倒计时效果实现
2015/11/12 Javascript
轻松学习jQuery插件EasyUI EasyUI实现拖放商品放置购物车
2015/11/30 Javascript
jQuery Easyui datagrid行内实现【添加】、【编辑】、【上移】、【下移】
2016/12/19 Javascript
基于vuejs实现一个todolist项目
2017/04/11 Javascript
浅析微信小程序自定义日历组件及flex布局最后一行对齐问题
2020/10/29 Javascript
详解Vue的mixin策略
2020/11/19 Vue.js
利用JavaScript模拟京东按键输入功能
2020/12/01 Javascript
[00:17]天涯墨客一技能展示
2018/08/25 DOTA
python Django模板的使用方法(图文)
2013/11/04 Python
python排序方法实例分析
2015/04/30 Python
python Flask实现restful api service
2017/12/04 Python
python解析yaml文件过程详解
2019/08/30 Python
Python编程快速上手——强口令检测算法案例分析
2020/02/29 Python
自定义Django Form中choicefield下拉菜单选取数据库内容实例
2020/03/13 Python
在jupyter notebook 添加 conda 环境的操作详解
2020/04/10 Python
CSS3 3D位移translate效果实例介绍
2016/05/03 HTML / CSS
大学生最常用的自我评价
2013/12/07 职场文书
个人委托函范文
2015/01/29 职场文书
幼儿园端午节活动总结
2015/05/05 职场文书
2015年副班长工作总结
2015/05/15 职场文书
国家助学金受助感言
2015/08/01 职场文书
新学期家长寄语2016
2015/12/03 职场文书