Nodejs相关:用过phantomjs的得请教一些问题

Nodejs相关:用过phantomjs的得请教一些问题

环境:linux 64 从官网下载了 phantomjs 并复制到 /usr/bin目录 切 也按照了 github 上 npm install phantom // phantomjs 桥; 服务环境: expressjs

代码片段 app.get(’/pdf’,function(req,res){ var phantom = require(‘phantom’); console.log(phantom); phantom.create(function(ph){ console.log(ph); res.end(); }) })

报错代码片段: { create: [Function], stderrHandler: [Function] } phantom stderr: phantomjs: error while loading shared libraries: libfreetype.so.6: cannot open shared object file: No such file or directory

assert.js:92 throw new assert.AssertionError({ ^ AssertionError: abnormal phantomjs exit code: 127 at Console.assert (console.js:102:23) at ChildProcess.<anonymous> (/home/wwwroot/mysite/node_modules/phantom/phantom.js:129:28) at ChildProcess.EventEmitter.emit (events.js:98:17) at Process.ChildProcess._handle.onexit (child_process.js:789:12)

成功得代码片段: { create: [Function], stderrHandler: [Function] } { set: [Function], get: [Function], exit: [Function], injectJs: [Function], getCookies: [Function], addCookie: [Function], clearCookies: [Function], createPage: [Function], _createPage: [Function] } GET /pdf 200 1460ms

同样得代码 本地没问题 (本地mac os)

请教大侠


2 回复

针对你遇到的问题,错误信息显示libfreetype.so.6文件缺失。这通常意味着你的Linux系统缺少某些必要的库文件。libfreetype.so.6是FreeType字体渲染库的一部分,PhantomJS依赖于它来处理页面上的文本。

解决方案

首先,你需要安装缺失的库文件。你可以通过运行以下命令来安装:

sudo apt-get update
sudo apt-get install -y libfreetype6 libfreetype6-dev

上述命令会更新你的包列表,并安装libfreetype6及其开发文件libfreetype6-dev。确保你的Linux发行版支持这些命令,如果使用的是不同的Linux发行版(如CentOS),则需要使用对应的包管理器命令。

示例代码与解释

这里是一个简单的例子,展示如何使用phantom模块创建一个PhantomJS实例并生成PDF文件。假设你已经解决了上述库文件缺失的问题。

const express = require('express');
const phantom = require('phantom');

const app = express();

app.get('/pdf', async function(req, res) {
    const instance = await phantom.create();
    const page = await instance.createPage();
    const status = await page.open('http://example.com'); // 打开一个网页
    const pdf = await page.renderBase64('PDF');

    // 发送PDF数据作为响应
    res.set('Content-Type', 'application/pdf');
    res.send(new Buffer(pdf, 'base64'));

    // 关闭PhantomJS实例
    await instance.exit();
});

app.listen(3000, () => console.log('Server running on port 3000'));

说明

  • phantom.create() 创建一个新的PhantomJS实例。
  • instance.createPage() 创建一个新的页面。
  • page.open(url) 打开指定URL的页面。
  • page.renderBase64(format) 渲染页面为Base64编码的字符串。
  • instance.exit() 关闭PhantomJS实例。

这段代码将创建一个Express应用,当访问/pdf路径时,会生成一个PDF文件并返回给客户端。注意,你需要根据实际情况调整URL和响应格式。


根据你的描述,错误信息提示phantomjs缺少依赖库libfreetype.so.6。这通常是因为系统中缺少某些必要的库文件导致的。你可以尝试通过以下步骤解决这个问题:

  1. 安装缺失的依赖库。对于大多数Linux发行版,可以使用包管理器安装libfreetype库。

    如果你使用的是基于Debian的系统(如Ubuntu),可以通过运行以下命令来安装:

    sudo apt-get update
    sudo apt-get install libfreetype6
    

    对于基于Red Hat的系统(如CentOS),可以使用以下命令:

    sudo yum install freetype
    
  2. 确保phantomjs能够找到这些库文件。有时可能需要手动设置LD_LIBRARY_PATH环境变量。

    在启动Node.js应用之前,可以设置环境变量:

    export LD_LIBRARY_PATH=/path/to/freetype:$LD_LIBRARY_PATH
    node your_app.js
    
  3. 验证安装。可以在终端运行以下命令来确认是否已正确安装libfreetype库:

    ldconfig -p | grep freetype
    
  4. 如果仍然出现问题,尝试重新安装phantomjs

    npm uninstall phantom
    npm install phantom
    

如果上述步骤仍无法解决问题,建议检查phantomjs版本是否与Node.js及操作系统兼容,并考虑使用其他替代方案,如Puppeteer,它基于Chrome并且维护更好。

回到顶部