我有一个基于jsreport-core的应用程序,内容模板呈现良好,但标头未呈现。这是我应用程序中的基本步骤:
import jsreportCore from 'jsreport-core';
import jsreportFsStore from 'jsreport-fs-store';
import jsreportTemplates from 'jsreport-templates';
import jsreportJsrender from 'jsreport-jsrender';
import jsreportPhantomPdf from 'jsreport-phantom-pdf';
import jsreportExpress from 'jsreport-express';
// ...
jsreport.use(new jsreportFsStore({dataDirectory: "data", syncModifications: true}));
jsreport.use(new jsreportTemplates());
jsreport.use(new jsreportJsrender);
jsreport.use(new jsreportPhantomPdf());
jsreport.use(new jsreportExpress({app : app}));
// ...
const jsreport = new jsreportCore({
loadConfig: false,
autoTempCleanup: true,
connectionString: { 'name': 'fs' },
tasks: { allowedModules: '*' }
});
// ...
jsreport.render({ template : {name: "foo",engine: "jsrender",recipe:"phantom-pdf"},data : dataObj}).then((out) => {
out.stream.pipe(res);
}).catch((e) => {
res.end(e.message);
});
这是从中加载模板化内容的模板目录结构。 (与jsreport-studio相同)。
---- data
------ templates
-------- foo
---------- content.html
---------- header.html
最佳答案
Solution: put the header information in a object "phantom" in the render paramter object:
{
template : {name: "foo",engine:"jsrender",recipe:"phantom-pdf",phantom :{
header :"<h1>TEST HEAD <div style='text-align:center'{{#pageNum}}/{{#numPages}}</div> </h1>", headerHeight : "10cm"}
}
and put an extra parameter in the jsreport-phantom-pdf contructor:
jsreport.use(new jsreportPhantomPdf({ strategy: 'phantom-server' }));
ref:
https://github.com/jsreport/jsreport-phantom-pdf
https://www.npmjs.com/package/phantom-pdf
http://jsreport.net/learn/phantom-pdf