欢迎光临南昌笑劳网络科技有限公司,我们是一家专注中小型企业营销推广服务的公司!

咨询热线:400 76543 55
南昌笑劳网络科技有限公司
最新资讯News
南昌笑劳网络科技有限公司

如何在Linux系统中利用node.js提取Word及PDF文本内容的案例介绍

作者:黄舟 | 点击: | 来源:黄舟
1806
2017
这篇文章主要给大家介绍了关于Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面跟着小编来一起看看吧。...

这篇文章主要给大家介绍了关于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]  []
 -f    : first page to convert
 -l    : last page to convert
 -r    : resolution, in DPI (default is 72)
 -x    : x-coordinate of the crop area top left corner
 -y    : y-coordinate of the crop area top left corner
 -W    : width of crop area in pixels (default is 0)
 -H    : height of crop area in pixels (default is 0)
 -layout   : maintain original physical layout
 -fixed   : assume fixed-pitch (or tabular) text
 -raw    : keep strings in content stream order
 -htmlmeta   : generate a simple HTML file, including the meta information
 -enc   : output text encoding name
 -listenc   : list available encodings
 -eol   : output end-of-line convention (unix, dos, or mac)
 -nopgbrk   : don't insert page breaks between pages
 -bbox    : output bounding box for each word and page size to html. Sets -htmlmeta
 -opw   : owner password (for encrypted files)
 -upw   : user password (for encrypted files)
 -q    : don'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  Adobe PDF output
  -p  PostScript output
   paper size like: a4, letter or legal
  -x  XML output
   like: db (DocBook)
  -m  character mapping file
  -w  in characters of text output
  -i  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 文件即可。

总结


# javascript  # 要将  # 给大家  # 朋友们  # 要在  # 只需  # 出了  # 是一个  # 文档  # 开源  # linux  # node.js  # 这篇文章 

我要咨询做网站
成功案例
建站流程
  • 网站需
    求分析
  • 网站策
    划方案
  • 页面风
    格设计
  • 程序设
    计研发
  • 资料录
    入优化
  • 确认交
    付使用
  • 后续跟
    踪服务
  • 400 76543 55
    sale#ncxiaolao.cn
Hi,Are you ready?
准备好开始了吗?
那就与我们取得联系吧

咨询送礼现在提交,将获得笑劳科技策划专家免费为您制作
价值5880元《全网营销方案+优化视频教程》一份!
下单送礼感恩七周年,新老用户下单即送创业型空间+域名等大礼
24小时免费咨询热线400 76543 55
合作意向表
您需要的服务
您最关注的地方
预算

直接咨询