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

Commit 2ace99c

Browse files
authored
Add print-to-pdf endpoint to system.ts
1 parent ba81b43 commit 2ace99c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/server/api/system.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express'
22
import {BrowserWindow, systemPreferences} from 'electron'
3+
import {writeFile} from 'fs'
34
const router = express.Router();
45

56
router.get('/can-prompt-touch-id', (req, res) => {
@@ -47,4 +48,26 @@ router.post('/print', async (req, res) => {
4748
await printWindow.loadURL(`data:text/html;charset=UTF-8,${html}`);
4849
});
4950

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

0 commit comments

Comments
 (0)