【问题标题】:How do I save a jsreport render to file with nodeJs?如何使用 nodeJs 将 jsreport 渲染保存到文件?
【发布时间】:2023-04-08 22:30:02
【问题描述】:

我是 node.js 和 jsreport 的新手,但我想做的是使用 node.js 在内存中创建一个 pdf,然后将其保存到磁盘。我需要它是独立的,因为它将作为 AWS Lambda 函数运行。

var fs = require('fs');
require("jsreport").render("<h1>Hi there!</h1>").then(function(out) {
    //pipe pdf with "Hi there!"
    fs.writeFile('C:\\helloworld.pdf', out, function (err) {
        if (err) return console.log(err);
        console.log('Hello World > helloworld.txt');
    });
fs.close();
    console.log("The End");
});

虽然这样运行,但输出 pdf 不会在 Adob​​e Reader 中打开,所以我认为文件输出不是有效的 PDF。

这需要 npm install jsreport

【问题讨论】:

  • fyi .writeFile 是异步的。

标签: javascript node.js jsreport


【解决方案1】:

根据我从 jsreport 网站收集到的信息(尽管我无法验证,因为他们网站上的示例都不适用于我),看起来 out 没有呈现 (PDF) 数据,而是一个包含(除其他外)流的对象。

这让我相信这可能会奏效:

require("jsreport").render("<h1>Hi there!</h1>").then(function(out) {
  out.result.pipe(fs.createWriteStream('c:\\helloworld.pdf'));
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多