首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用jsreport-core追加文件

如何用jsreport-core追加文件
EN

Stack Overflow用户
提问于 2021-06-16 13:03:48
回答 1查看 253关注 0票数 0

我在https://jsreport.net/learn/pdf-utils中阅读过,但是我没有找到如何在不使用js的情况下通过脚本追加文件的答案。

这是我的密码:

main.js

代码语言:javascript
复制
const jsreport = require('jsreport-core')();
const fs = require('fs');
const d = require('./child');
const foo = async() => {
    await jsreport.init();
    const resp = await jsreport.render({
        template: {
            content: '<h1>Hello {{foo}}</h1>',
            engine: 'handlebars',
            recipe: 'chrome-pdf'
        },
        data: {
            foo: "1"
        }
    });
    fs.writeFileSync('jsreport.pdf', resp.content);
    process.exit();
}

foo();

child.js

代码语言:javascript
复制
module.exports = function beforeRender(req, res, done) {
    req.template.content = req.template.content('1', '2')
    done();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-17 08:26:37

代码语言:javascript
复制
const jsreport = require('jsreport-core')()
jsreport.use(require('jsreport-chrome-pdf')())
jsreport.use(require('jsreport-pdf-utils')())
jsreport.use(require('jsreport-handlebars')())
const fs = require('fs').promises

;(async () => {
  await jsreport.init()
  const response = await jsreport.render({
    template: {
      content: 'Hello from main report',
      engine: 'handlebars',
      recipe: 'chrome-pdf',
      pdfOperations: [{
        type: 'append',
        template: {
          engine: 'handlebars',
          recipe: 'chrome-pdf',
          content: 'Content to append'
        }
      }]
    }
  })

  await fs.writeFile('out.pdf', response.content)
  await jsreport.close()
})()

我建议检查特定存储库单元测试,总是有很多例子。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68003222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档