【问题标题】:how to append files with jsreport-core如何使用 jsreport-core 附加文件
【发布时间】:2021-06-17 16:26:37
【问题描述】:

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

这是我的代码:

ma​​in.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();
}

【问题讨论】:

    标签: jsreport


    【解决方案1】:
    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,总是有很多例子。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2023-04-08
    • 2021-06-22
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多