利用场景:
客户在创立前后端拆散的利用时,前端只用于做一些根本的数据展现,如果波及大量的数据或报表生成,倡议放在服务器端展现,本文次要分享如何在服务器端应用 ActiveReportsJS 实现报表导出 PDF 性能
Demo 下载:
https://gcdn.grapecity.com.cn/forum.php?mod=attachment&aid=MTIzMjQwfGMwM2JkNWVhfDE2MjM3MjI5NzF8MjkzODJ8ODgwNzA%3D
环境筹备:
node.js v14.15.0+Headless 无头浏览器
操作步骤:
- 增加资源文件
- 配置资源和文件
const puppeteer = require('puppeteer');
const fs = require('fs');
var static = require('node-static');
var http = require('http');
var file = new(static.Server)(__dirname + '/resources');
http.createServer(function (req, res) {file.serve(req, res);
}).listen(9999);
const fonts = [
{
name: 'Montserrat',
source: 'Montserrat-Regular.ttf'
},
{
name: 'Montserrat',
source: 'Montserrat-Medium.ttf',
weight: 500
}
];
3. 调用浏览器并初始化 调用 ARJS 导出 PDF 文件
(async () => {const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto(`http://localhost:9999/host.html`);
//await page.goto(`${__dirname}/resources/host.html`);
const pdfString =await page.evaluate(({reportUrl, fonts}) =>
new Promise(async (resolve, reject) => {// await GC.ActiveReports.Core.FontStore.registerFonts(fonts);
const report = new GC.ActiveReports.Core.PageReport();
await report.load(reportUrl);
const doc = await report.run();
const result = await GC.ActiveReports.PdfExport.exportDocument(doc, {fonts: fonts, info: {author: 'GrapeCity'}});
const reader = new FileReader();
reader.readAsBinaryString(result.data);
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject('Error occurred while reading binary string');
}), {reportUrl: 'SimpleTable.rdlx-json', fonts: fonts});
const pdfData = Buffer.from(pdfString, 'binary');
fs.writeFileSync(`${__dirname}/out115.pdf`, pdfData);
console.log('done');
process.exit(0);
})();