Skip to content

Commit 80b499f

Browse files
authored
Support custom settings (#229)
* Support custom settings * Build plugin
1 parent 861d5ef commit 80b499f

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

resources/js/electron-plugin/dist/server/api/system.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ router.get('/printers', (req, res) => __awaiter(void 0, void 0, void 0, function
6262
});
6363
}));
6464
router.post('/print', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
65-
const { printer, html } = req.body;
65+
const { printer, html, settings } = req.body;
6666
let printWindow = new BrowserWindow({
6767
show: false,
6868
});
69+
const defaultSettings = {
70+
silent: true,
71+
deviceName: printer,
72+
};
73+
const mergedSettings = Object.assign(Object.assign({}, defaultSettings), (settings && typeof settings === 'object' ? settings : {}));
6974
printWindow.webContents.on('did-finish-load', () => {
70-
printWindow.webContents.print({
71-
silent: true,
72-
deviceName: printer,
73-
}, (success, errorType) => {
75+
printWindow.webContents.print(mergedSettings, (success, errorType) => {
7476
if (success) {
7577
console.log('Print job completed successfully.');
7678
res.sendStatus(200);
@@ -88,12 +90,12 @@ router.post('/print', (req, res) => __awaiter(void 0, void 0, void 0, function*
8890
yield printWindow.loadURL(`data:text/html;charset=UTF-8,${html}`);
8991
}));
9092
router.post('/print-to-pdf', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
91-
const { html } = req.body;
93+
const { html, settings } = req.body;
9294
let printWindow = new BrowserWindow({
9395
show: false,
9496
});
9597
printWindow.webContents.on('did-finish-load', () => {
96-
printWindow.webContents.printToPDF({}).then(data => {
98+
printWindow.webContents.printToPDF(settings !== null && settings !== void 0 ? settings : {}).then(data => {
9799
printWindow.close();
98100
res.json({
99101
result: data.toString('base64'),

resources/js/electron-plugin/src/server/api/system.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,24 @@ router.get('/printers', async (req, res) => {
6060
});
6161

6262
router.post('/print', async (req, res) => {
63-
const {printer, html} = req.body;
63+
const {printer, html, settings} = req.body;
6464

6565
let printWindow = new BrowserWindow({
6666
show: false,
6767
});
6868

69+
const defaultSettings = {
70+
silent: true,
71+
deviceName: printer,
72+
};
73+
74+
const mergedSettings = {
75+
...defaultSettings,
76+
...(settings && typeof settings === 'object' ? settings : {}),
77+
};
78+
6979
printWindow.webContents.on('did-finish-load', () => {
70-
printWindow.webContents.print({
71-
silent: true,
72-
deviceName: printer,
73-
}, (success, errorType) => {
80+
printWindow.webContents.print(mergedSettings, (success, errorType) => {
7481
if (success) {
7582
console.log('Print job completed successfully.');
7683
res.sendStatus(200);
@@ -89,14 +96,14 @@ router.post('/print', async (req, res) => {
8996
});
9097

9198
router.post('/print-to-pdf', async (req, res) => {
92-
const {html} = req.body;
99+
const {html, settings} = req.body;
93100

94101
let printWindow = new BrowserWindow({
95102
show: false,
96103
});
97104

98105
printWindow.webContents.on('did-finish-load', () => {
99-
printWindow.webContents.printToPDF({}).then(data => {
106+
printWindow.webContents.printToPDF(settings ?? {}).then(data => {
100107
printWindow.close();
101108
res.json({
102109
result: data.toString('base64'),

0 commit comments

Comments
 (0)