-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
104 lines (91 loc) · 3.43 KB
/
server.js
File metadata and controls
104 lines (91 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const express = require('express');
const path = require('path');
const chromium = require('playwright').chromium;
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.static('resources'));
app.use('/scripts', express.static(path.join(__dirname, 'node_modules/@mescius/activereportsjs/dist')));
async function generate(page, { reportUrl, yourText }) {
return await page.evaluate(
({ reportUrl, yourText }) =>
new Promise(async (resolve, reject) => {
try {
// MESCIUS.ActiveReportsJS.Core.setLicenseKey('');
await MESCIUS.ActiveReportsJS.Core.FontStore.registerFonts('fontsConfig.json');
const report = new MESCIUS.ActiveReportsJS.Core.PageReport();
await report.load(reportUrl, {
reportParameters: [
{
Name: 'paramYourText',
Value: yourText
}
]
});
const document = await report.run();
const result = await MESCIUS.ActiveReportsJS.PdfExport.exportDocument(document, {
info: { author: 'Author' }
});
const reader = new FileReader();
reader.readAsArrayBuffer(result.data);
reader.onload = () => resolve(Array.from(new Uint8Array(reader.result)));
reader.onerror = () => reject('Error occurred while reading PDF data.');
} catch (error) {
reject(error.message || 'error');
}
}),
{ reportUrl, yourText }
);
}
async function launchBrowser() {
return await chromium.launch({
headless: true,
args: ['--font-render-hinting=none']
});
}
app.get('/api/report', async (req, res) => {
let browser;
try {
browser = await launchBrowser();
const page = await browser.newPage();
// Listen for console messages
page.on('console', msg => console.log('Browser console:', msg.text()));
page.on('pageerror', error => console.log('Browser error:', error));
console.log('\nNavigating to index.html...');
const response = await page.goto(`http://localhost:${PORT}/index.html`, {
waitUntil: 'networkidle',
timeout: 30000
});
console.log('Page loaded with status:', response.status());
// Wait for ActiveReports scripts to load
console.log('Waiting for ActiveReports scripts...');
await page.waitForFunction(() => {
return typeof MESCIUS !== 'undefined' &&
typeof MESCIUS.ActiveReportsJS !== 'undefined' &&
typeof MESCIUS.ActiveReportsJS.Core !== 'undefined' &&
typeof MESCIUS.ActiveReportsJS.PdfExport !== 'undefined';
}, { timeout: 10000 });
console.log('ActiveReports scripts loaded');
console.log('Generating PDF...');
const pdfData = await generate(page, {
reportUrl: 'sample.rdlx-json',
yourText: req.query.yourtext || 'report'
});
console.log('PDF generated successfully');
await browser.close();
const buffer = Buffer.from(pdfData);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename="report.pdf"');
res.send(buffer);
} catch (error) {
console.error('Error generating PDF:', error);
if (browser) {
await browser.close();
}
res.status(500).send('Error generating PDF');
}
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on http://localhost:${PORT}`);
console.log(`API endpoint: http://localhost:${PORT}/api/report?yourtext=yourtext`);
});