diff --git a/main/preload.ts b/main/preload.ts index 24ca6eda..f00619d8 100644 --- a/main/preload.ts +++ b/main/preload.ts @@ -148,6 +148,19 @@ const ipc = { )) as boolean; }, + async printDocument( + html: string, + width: number, + height: number + ): Promise { + return (await ipcRenderer.invoke( + IPC_ACTIONS.PRINT_HTML_DOCUMENT, + html, + width, + height + )) as boolean; + }, + async getDbList() { return (await ipcRenderer.invoke( IPC_ACTIONS.GET_DB_LIST diff --git a/main/printHtmlDocument.ts b/main/printHtmlDocument.ts new file mode 100644 index 00000000..6442dd3a --- /dev/null +++ b/main/printHtmlDocument.ts @@ -0,0 +1,28 @@ +import { App } from 'electron'; +import path from 'path'; +import fs from 'fs-extra'; +import { getInitializedPrintWindow } from './saveHtmlAsPdf'; + +export async function printHtmlDocument( + html: string, + app: App, + width: number, + height: number +): Promise { + const tempRoot = app.getPath('temp'); + const tempFile = path.join(tempRoot, `temp-print.html`); + await fs.writeFile(tempFile, html, { encoding: 'utf-8' }); + + const printWindow = await getInitializedPrintWindow(tempFile, width, height); + + const success = await new Promise((resolve) => { + printWindow.webContents.print( + { silent: false, printBackground: true }, + (success) => resolve(success) + ); + }); + + printWindow.close(); + await fs.unlink(tempFile); + return success; +} diff --git a/main/registerIpcMainActionListeners.ts b/main/registerIpcMainActionListeners.ts index 05da90aa..85d51df7 100644 --- a/main/registerIpcMainActionListeners.ts +++ b/main/registerIpcMainActionListeners.ts @@ -19,6 +19,7 @@ import { IPC_ACTIONS } from '../utils/messages'; import { getUrlAndTokenString, sendError } from './contactMothership'; import { getLanguageMap } from './getLanguageMap'; import { getTemplates } from './getPrintTemplates'; +import { printHtmlDocument } from './printHtmlDocument'; import { getConfigFilesWithModified, getErrorHandledReponse, @@ -105,6 +106,13 @@ export default function registerIpcMainActionListeners(main: Main) { } ); + ipcMain.handle( + IPC_ACTIONS.PRINT_HTML_DOCUMENT, + async (_, html: string, width: number, height: number) => { + return await printHtmlDocument(html, app, width, height); + } + ); + ipcMain.handle( IPC_ACTIONS.SAVE_DATA, async (_, data: string, savePath: string) => { diff --git a/main/saveHtmlAsPdf.ts b/main/saveHtmlAsPdf.ts index 81b08c88..145a4e57 100644 --- a/main/saveHtmlAsPdf.ts +++ b/main/saveHtmlAsPdf.ts @@ -35,7 +35,7 @@ export async function saveHtmlAsPdf( return true; } -async function getInitializedPrintWindow( +export async function getInitializedPrintWindow( printFilePath: string, width: number, height: number diff --git a/src/pages/PrintView/PrintView.vue b/src/pages/PrintView/PrintView.vue index 4f514a17..676508df 100644 --- a/src/pages/PrintView/PrintView.vue +++ b/src/pages/PrintView/PrintView.vue @@ -16,9 +16,12 @@ @change="onTemplateNameChange" /> - + @@ -246,16 +249,16 @@ export default defineComponent({ this.templateList = list.map(({ name }) => name); }, - async savePDF() { + async savePDF(shouldPrint?: boolean) { const printContainer = this.$refs.printContainer as { - savePDF: (name?: string) => Promise; + savePDF: (name?: string, shouldPrint?: boolean) => Promise; }; if (!printContainer?.savePDF) { return; } - await printContainer.savePDF(this.doc?.name); + await printContainer.savePDF(this.doc?.name, shouldPrint); }, async setTemplateFromDefault() { const defaultName = diff --git a/src/pages/PrintView/ReportPrintView.vue b/src/pages/PrintView/ReportPrintView.vue index 475cbc82..93a49673 100644 --- a/src/pages/PrintView/ReportPrintView.vue +++ b/src/pages/PrintView/ReportPrintView.vue @@ -1,9 +1,12 @@ - +