首頁  >  文章  >  運維  >  如何在Linux系統中利用node.js提取Word及PDF文字內容的案例介紹

如何在Linux系統中利用node.js提取Word及PDF文字內容的案例介紹

黄舟
黄舟原創
2017-06-18 09:11:011830瀏覽

這篇文章主要為大家介紹了Linux系統中利用node.js提取Word(doc/docx)及PDF文本的內容,文中給出了詳細的範例程式碼供大家參考學習,需要的朋友們下面跟著小編來一起看看吧。

前言

想要做全文搜尋引擎,則需要將word/pdf等文件內容擷取出來。對於pdf有xpdf等一些開源方案。

但Word文件的情況則會複雜一些。

提取PDF文字內容

#XPDF是一個免費開源的軟體,用於顯示PDF文件,並可將pdf轉換成文字圖片等,同樣支援Windows版。在Debian Linux上安裝非常簡單:

apt-get install xpdf

我們這裡只使用pdftotext這個功能,直接輸入可查看幫助:

root@raspberrypi:/var/www# pdftotext
pdftotext version 0.26.5
Copyright 2005-2014 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011 Glyph & Cog, LLC
Usage: pdftotext [options] <PDF-file> [<text-file>]
 -f <int>   : first page to convert
 -l <int>   : last page to convert
 -r <fp>   : resolution, in DPI (default is 72)
 -x <int>   : x-coordinate of the crop area top left corner
 -y <int>   : y-coordinate of the crop area top left corner
 -W <int>   : width of crop area in pixels (default is 0)
 -H <int>   : height of crop area in pixels (default is 0)
 -layout   : maintain original physical layout
 -fixed <fp>  : assume fixed-pitch (or tabular) text
 -raw    : keep strings in content stream order
 -htmlmeta   : generate a simple HTML file, including the meta information
 -enc <string>  : output text encoding name
 -listenc   : list available encodings
 -eol <string>  : output end-of-line convention (unix, dos, or mac)
 -nopgbrk   : don&#39;t insert page breaks between pages
 -bbox    : output bounding box for each word and page size to html. Sets -htmlmeta
 -opw <string>  : owner password (for encrypted files)
 -upw <string>  : user password (for encrypted files)
 -q    : don&#39;t print any messages or errors
 -v    : print copyright and version info
 -h    : print usage information
 -help    : print usage information
 --help   : print usage information
 -?    : print usage information

測試一下:

root@raspberrypi:/var/www# pdftotext onceai.pdf onceai.txt
root@raspberrypi:/var/www# cat onceai.txt 产品介绍 顽石智能科技(上海)有限公司
....

然後在node. js中使用child_process直接呼叫此指令即可,pdftotext會將內容輸出以文字檔中,可能需要多一些操作。具體代碼略。

用antiword提取.doc 的內容

#我們這裡使用了antiword 開源軟體,來提取word2003以前版本的內容,安裝同樣非常簡單:

apt-get install antiword

查看幫助:

root@raspberrypi:/var/www# antiword
 Name: antiword
 Purpose: Display MS-Word files
 Author: (C) 1998-2005 Adri van Os
 Version: 0.37 (21 Oct 2005)
 Status: GNU General Public License
 Usage: antiword [switches] wordfile1 [wordfile2 ...]
 Switches: [-f|-t|-a papersize|-p papersize|-x dtd][-m mapping][-w #][-i #][-Ls]
  -f formatted text output
  -t text output (default)
  -a <paper size name> Adobe PDF output
  -p <paper size name> PostScript output
   paper size like: a4, letter or legal
  -x <dtd> XML output
   like: db (DocBook)
  -m <mapping> character mapping file
  -w <width> in characters of text output
  -i <level> image level (PostScript only)
  -L use landscape mode (PostScript only)
  -r Show removed text
  -s Show hidden (by Word) text


#antiword直接將word內容輸出到了console中:


root@raspberrypi:/var/www# antiword spec.doc

SYNC Mobile – Ford APA
Project Number: DFYST
Requirements Specification


同樣在node.js用child_process呼叫此指令即可。

解析提取.docx 的內容

#對於docx 文件來說,因基本身就是一個zip文件,只需要在node.js先將其解壓縮,再解析text.docx\word\document.xml 檔案即可。

總結

#

以上是如何在Linux系統中利用node.js提取Word及PDF文字內容的案例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn