如何使用 jsreport-core 追加文件

how to append files with jsreport-core

我阅读了 https://jsreport.net/learn/pdf-utils,但我没有找到在不使用 js report studio 时如何通过脚本附加文件的答案。

这是我的代码:

main.js

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

module.exports = function beforeRender(req, res, done) {
    req.template.content = req.template.content('1', '2')
    done();
}
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()
})()

我建议检查 particular repository unit tests,那里总是有很多例子。