在Node.js / express Web应用程序中生成PDF报告

时间:2017-01-24 17:34:25

标签: node.js express

我需要将现有的ASP.NET MVC Web应用程序转换为Node.JS. 这个现有的.net应用程序可以选择生成PDF报告,设计和生成使用Active Reports for .NET的报告。

我想知道在Node.js / Express Web应用程序中设计和生成PDF报告的选项有哪些。

1 个答案:

答案 0 :(得分:0)

有许多库将html转换为PDF。其中一个是PhantomJS。您可以使用他们的rasterize.js示例来创建PDF。

棘手的部分是让它与NodeJS一起使用。 NodeJs运行在与PhantomJs不同的进程中。一种解决方案可能是phantomjs-node。你可以这样做example

const phantom = require('phantom');

(async function() {
    const instance = await phantom.create();
    const page = await instance.createPage();

    await page.property('viewportSize', {width: 1024, height: 600});
    const status = await page.open('https://stackoverflow.com/');
    console.log(`Page opened with status [${status}].`);

    await page.render('stackoverflow.pdf');
    console.log(`File created at [./stackoverflow.pdf]`);

    await instance.exit();
}());

// node --harmony-async-await render.js

请注意,您需要使用节点7来使用上面的示例。

免责声明:我是phantomjs-node的作者。