PHP讀取PDF內(nèi)容配合Xpdf的使用
一.下載
首先,我們先把資料下下來(lái)先。
如果不需要轉(zhuǎn)中文的話(huà),只需要下載它就可以:xpdf-bin-linux-3.03.tar,如果需要轉(zhuǎn)中文,那你就還需要它了:xpdf-chinese-simplified.tar
二.安裝
現(xiàn)在,下載完畢了吧,我們可以進(jìn)行安裝了。
[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
如果不需要讀取中文的話(huà),到這里就可以結(jié)束了,如果需要,那我們繼續(xù)往后
[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 的內(nèi)容復(fù)制到/usr/local/etc/xpdfrc文件中。記得里面的路徑要正確。(注意,這里面的簡(jiǎn)體中文包包括以下三種格式:ISO-2022-CN,EUC-CN,GBK ,看清楚哦,不支持UTF-8,可以先轉(zhuǎn)為GBK,然后進(jìn)行轉(zhuǎn)義)
三.功能實(shí)現(xiàn)
至此,所有的配置完畢,我們要開(kāi)始使用它了。
如果是簡(jiǎn)單的PDF讀取,那么直接用下面的語(yǔ)句就OK了。
$content = shell_exec('/usr/local/bin/pdftotext '.$filename.' -');
如果需要轉(zhuǎn)中文,如此這般,加上參數(shù)。
$content = shell_exec('/usr/local/bin/pdftotext -layout -enc GBK '.$filename.' -');
當(dāng)然,加了參數(shù)之后依然是不影響英文的轉(zhuǎn)換的,所以,放心使用吧。需要注意的是,這里轉(zhuǎn)出來(lái)的是GBK編碼的哦,現(xiàn)在網(wǎng)站很多用的是UTF-8,想要不顯示亂碼的話(huà),需要再次轉(zhuǎn)義一下哦。
$content = mb_convert_encoding($content, 'UTF-8','GBK');
至此,就大功告成了。讀取出來(lái)的內(nèi)容,你想如何使用,再寫(xiě)代碼處理吧。
最后加一下pdftotext 的參數(shù)說(shuō)明給大家。
主要參數(shù)如下:
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實(shí)現(xiàn)在線(xiàn)閱讀PDF文件的方法
- php生成PDF格式文件并且加密
- PHP實(shí)現(xiàn)在線(xiàn)閱讀PDF文件的方法
- PHP中使用imagick實(shí)現(xiàn)把PDF轉(zhuǎn)成圖片
- PHP實(shí)現(xiàn)HTML生成PDF文件的方法
- PHP在網(wǎng)頁(yè)中動(dòng)態(tài)生成PDF文件詳細(xì)教程
- PHP中使用TCPDF生成PDF文檔實(shí)例
- php使瀏覽器直接下載pdf文件的方法
- 用PHP創(chuàng)建PDF中文文檔
- 用PHP生成PDF文件 with FPDF
- PHP簡(jiǎn)單讀取PDF頁(yè)數(shù)的實(shí)現(xiàn)方法
相關(guān)文章
php實(shí)現(xiàn)的短網(wǎng)址算法分享
這篇文章主要介紹了php實(shí)現(xiàn)的短網(wǎng)址算法,理論上支持1,073,741,824個(gè)短網(wǎng)址,個(gè)人使用足夠了,需要的朋友可以參考下2014-06-06
CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
這篇文章主要介紹了CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法,結(jié)合實(shí)例形式詳細(xì)分析了CI框架優(yōu)化文件上傳及多文件上傳的實(shí)現(xiàn)思路與具體操作步驟,需要的朋友可以參考下2017-01-01
PHP文件緩存smarty模板應(yīng)用實(shí)例分析
這篇文章主要介紹了PHP文件緩存smarty模板應(yīng)用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了smarty模板緩存的相關(guān)使用技巧,需要的朋友可以參考下2016-02-02
laravel框架 laravel-admin上傳圖片到oss的方法
今天小編就為大家分享一篇laravel框架 laravel-admin上傳圖片到oss的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHP如何使用JWT做Api接口身份認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了PHP如何使用JWT做Api接口身份認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
php設(shè)計(jì)模式之正面模式實(shí)例分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之正面模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了php正面模式相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
Thinkphp3.2簡(jiǎn)單解決多文件上傳只上傳一張的問(wèn)題
下面小編就為大家?guī)?lái)一篇Thinkphp3.2簡(jiǎn)單解決多文件上傳只上傳一張的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

