Linux下安装html-pdf报“PhantomJS not found on PATH”错误的解决办法

问题场景:使用html-pdf将html转换pdf文件,html-pdf依赖于phantomjs-prebuilt。phantomjs-prebuilt是用来安装PhantomJS的npm 安装器,安装html-pdf时会自动安装该依赖。

然而,安装html-pdf插件的时候遇到了phantomjs-prebuilt装不上的问题: 

[root@localhost PROJECTSERVICE]# npm install html-pdf --save

> [email protected] install /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt
> node install.js

PhantomJS not found on PATH
Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Verified checksum of previously downloaded file
Extracting tar contents (via spawned process)
Removing /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1544582492513/phantomjs-2.1.1-linux-x86_64 -> /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt/lib/phantom
chmod failed: phantomjs was not successfully copied to /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-12-12T02_41_36_373Z-debug.log

上面显示安装phantomjs-prebuilt的时候出错,于是我在项目根目录下执行 npm install phantomjs-prebuilt --save,还是报同样的错误。于是我又往下看,看到这段话

PhantomJS not found on PATH
Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Verified checksum of previously downloaded file
Extracting tar contents (via spawned process)
Removing /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1544582492513/phantomjs-2.1.1-linux-x86_64 -> /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt/lib/phantom
chmod failed: phantomjs was not successfully copied to /pathto/PROJECTSERVICE/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs

看到标红的地方显示复制文件失败,然后我在tmp目录下找到了phantomjs/phantomjs-2.1.1-linux-x86_64.tar以及解压文件,想手动按照它上面的路径复制,但是我在node_modules目录下并没有找到phantomjs-prebuilt这个文件夹(因为之前的安装失败)。

尝试过程

一、

第一步:在git下面找phantomjs-prebuilt,看到了两种安装方式:

Linux下安装html-pdf报“PhantomJS not found on PATH”错误的解决办法_第1张图片

既然上面的第一种行不通那就第二种。将zip包下载下来并解压,然后拷贝进node_modules目录下并改名为phantomjs-prebuilt。

第二步:cd 进入phantomjs-prebuilt目录,执行node install.js,执行的过程中发现有报错,缺少模块,安装即可。然后调用html-pdf的代码,发现可以用了。

二、

第一步:在百度的时候看到了一个方法:npm install phantomjs-prebuilt@version --ignore-scripts就可以安装成功,由上面的提示看到版本为2.1.16,于是安装npm install [email protected] --ignore-scripts。这次安装不报错了。

第二步:同上第二步。

三、

第一步:同上第一步。

第二步:将tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1544582492513/phantomjs-2.1.1-linux-x86_64/bin下的phantomjs文件拷贝到node_modules下的phantomjs-prebuilt文件中的bin目录下。并更改项目代码中pdf转换的options配置对象,将phantomPath属性改为通往bin目录下文件的路径。

 

 

你可能感兴趣的:(NodeJs)