Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 872895d

Browse files
authored
Merge pull request #12 from basst85/main
Add print-to-pdf endpoint
2 parents a0c0d71 + 6ac9aa7 commit 872895d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/server/api/system.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,29 @@ router.post('/print', async (req, res) => {
4747
await printWindow.loadURL(`data:text/html;charset=UTF-8,${html}`);
4848
});
4949

50+
router.post('/print-to-pdf', async (req, res) => {
51+
const {html} = req.body;
52+
53+
let printWindow = new BrowserWindow({
54+
show: false,
55+
});
56+
57+
printWindow.webContents.on('did-finish-load', () => {
58+
printWindow.webContents.printToPDF({'transferMode': 'ReturnAsBase64'}).then(data => {
59+
printWindow.close();
60+
res.json({
61+
result: data.toString('base64'),
62+
});
63+
}).catch(e => {
64+
printWindow.close();
65+
66+
res.status(400).json({
67+
error: e.message,
68+
});
69+
});
70+
});
71+
72+
await printWindow.loadURL(`data:text/html;charset=UTF-8,${html}`);
73+
});
74+
5075
export default router;

0 commit comments

Comments
 (0)