这篇文章主要给大家介绍了关于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'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'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.docxworddocument.xml 文件即可。
总结
以上是如何在Linux系统中利用node.js提取Word及PDF文本内容的案例介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

Linux操作系统的5个核心组件是:1.内核,2.系统库,3.系统工具,4.系统服务,5.文件系统。这些组件协同工作,确保系统的稳定和高效运行,共同构成了一个强大而灵活的操作系统。

Linux的五个核心元素是:1.内核,2.命令行界面,3.文件系统,4.包管理,5.社区与开源。这些元素共同定义了Linux的本质和功能。

Linux用户管理和安全性可以通过以下步骤实现:1.创建用户和组,使用命令如sudouseradd-m-gdevelopers-s/bin/bashjohn。2.批量创建用户和设置密码策略,使用for循环和chpasswd命令。3.检查和修复常见错误,如家目录和shell设置。4.实施最佳实践,如强密码策略、定期审计和最小权限原则。5.优化性能,使用sudo和调整PAM模块配置。通过这些方法,可以有效管理用户和提升系统安全性。

Linux文件系统和进程管理的核心操作包括文件系统的管理和进程的控制。1)文件系统操作包括创建、删除、复制和移动文件或目录,使用命令如mkdir、rmdir、cp和mv。2)进程管理涉及启动、监控和终止进程,使用命令如./my_script.sh&、top和kill。

Shell脚本是Linux系统中用于自动化执行命令的强大工具。1)Shell脚本通过解释器逐行执行命令,处理变量替换和条件判断。2)基本用法包括备份操作,如使用tar命令备份目录。3)高级用法涉及使用函数和case语句管理服务。4)调试技巧包括使用set-x开启调试模式和set-e在命令失败时退出。5)性能优化建议避免子Shell,使用数组和优化循环。

Linux是一个基于Unix的多用户、多任务操作系统,强调简单性、模块化和开放性。其核心功能包括:文件系统:以树状结构组织,支持多种文件系统如ext4、XFS、Btrfs,使用df-T查看文件系统类型。进程管理:通过ps命令查看进程,使用PID管理进程,涉及优先级设置和信号处理。网络配置:灵活设置IP地址和管理网络服务,使用sudoipaddradd配置IP。这些功能在实际操作中通过基本命令和高级脚本自动化得以应用,提升效率并减少错误。

进入Linux维护模式的方法包括:1.编辑GRUB配置文件,添加"single"或"1"参数并更新GRUB配置;2.在GRUB菜单中编辑启动参数,添加"single"或"1"。退出维护模式只需重启系统。通过这些步骤,你可以在需要时快速进入维护模式,并安全地退出,确保系统的稳定性和安全性。

Linux的核心组件包括内核、shell、文件系统、进程管理和内存管理。1)内核管理系统资源,2)shell提供用户交互界面,3)文件系统支持多种格式,4)进程管理通过fork等系统调用实现,5)内存管理使用虚拟内存技术。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境