PHP讀取PDF內容(LINUX下XPDF的配置和使用)

來源:互聯網
上載者:User

        最近領導拍腦袋想出了一個需求,要我讀取PDF裡面的內容,並且入庫存為本文,用來搜尋。

        一.下載

        首先,我們先把資料下下來先。在這裡:http://www.foolabs.com/xpdf/download.html

        如果不需要轉中文的話,只需要下載它就可以: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.