From e60ec6b4bce37ab2f49a91a109c6d8baa2a8c56b Mon Sep 17 00:00:00 2001 From: AbleKSaju <126228406+AbleKSaju@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:22:12 +0530 Subject: [PATCH] feat: implement print document functionality --- main/preload.ts | 13 ++++++++ main/printHtmlDocument.ts | 28 ++++++++++++++++ main/registerIpcMainActionListeners.ts | 8 +++++ main/saveHtmlAsPdf.ts | 2 +- src/pages/TemplateBuilder/TemplateBuilder.vue | 4 +-- src/utils/printTemplates.ts | 33 ++++++++++++------- utils/messages.ts | 1 + 7 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 main/printHtmlDocument.ts diff --git a/main/preload.ts b/main/preload.ts index 24ca6edae..f00619d8e 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 000000000..6442dd3a1 --- /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 05da90aa9..85d51df72 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 81b08c88d..145a4e57a 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/TemplateBuilder/TemplateBuilder.vue b/src/pages/TemplateBuilder/TemplateBuilder.vue index 5d4848f88..09582b18c 100644 --- a/src/pages/TemplateBuilder/TemplateBuilder.vue +++ b/src/pages/TemplateBuilder/TemplateBuilder.vue @@ -641,14 +641,14 @@ export default defineComponent({ }, savePDF(action?: 'print') { const printContainer = this.$refs.printContainer as { - savePDF: (name?: string, action?:string) => void; + savePDF: (name?: string, action?: string) => void; }; if (!printContainer?.savePDF) { return; } - printContainer.savePDF(this.doc?.name, action); + printContainer.savePDF(this.doc?.name, action); }, async setDisplayInitialDoc() { const schemaName = this.doc?.type; diff --git a/src/utils/printTemplates.ts b/src/utils/printTemplates.ts index 6e0db7314..aeacc5593 100644 --- a/src/utils/printTemplates.ts +++ b/src/utils/printTemplates.ts @@ -391,19 +391,30 @@ export async function getPathAndMakePDF( name: string, innerHTML: string, width: number, - height: number + height: number, + action: 'print' | 'save' ) { - const { filePath: savePath } = await getSavePath(name, 'pdf'); - if (!savePath) { - return; - } + if (action === 'save') { + const { filePath: savePath } = await getSavePath(name, 'pdf'); + if (!savePath) { + return; + } - const html = constructPrintDocument(innerHTML); - const success = await ipc.makePDF(html, savePath, width, height); - if (success) { - showExportInFolder(t`Save as PDF Successful`, savePath); - } else { - showToast({ message: t`Export Failed`, type: 'error' }); + const html = constructPrintDocument(innerHTML); + const success = await ipc.makePDF(html, savePath, width, height); + if (success) { + showExportInFolder(t`Save as PDF Successful`, savePath); + } else { + showToast({ message: t`Export Failed`, type: 'error' }); + } + } else if (action === 'print') { + const html = constructPrintDocument(innerHTML); + const success = await ipc.printDocument(html, width, height); + if (success) { + showToast({ message: t`Print Successful`, type: 'success' }); + } else { + showToast({ message: t`Print Failed`, type: 'error' }); + } } } diff --git a/utils/messages.ts b/utils/messages.ts index 601a94ce6..7e9e44c40 100644 --- a/utils/messages.ts +++ b/utils/messages.ts @@ -21,6 +21,7 @@ export enum IPC_ACTIONS { GET_DIALOG_RESPONSE = 'show-message-box', GET_ENV = 'get-env', SAVE_HTML_AS_PDF = 'save-html-as-pdf', + PRINT_HTML_DOCUMENT = 'print-html-document', SAVE_DATA = 'save-data', SHOW_ERROR = 'show-error', SEND_ERROR = 'send-error',