Skip to content

Commit

Permalink
feat: implement print document functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
AbleKSaju committed Jan 14, 2025
1 parent 960fd06 commit e60ec6b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 14 deletions.
13 changes: 13 additions & 0 deletions main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ const ipc = {
)) as boolean;
},

async printDocument(
html: string,
width: number,
height: number
): Promise<boolean> {
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
Expand Down
28 changes: 28 additions & 0 deletions main/printHtmlDocument.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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<boolean>((resolve) => {
printWindow.webContents.print(
{ silent: false, printBackground: true },
(success) => resolve(success)
);
});

printWindow.close();
await fs.unlink(tempFile);
return success;
}
8 changes: 8 additions & 0 deletions main/registerIpcMainActionListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion main/saveHtmlAsPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function saveHtmlAsPdf(
return true;
}

async function getInitializedPrintWindow(
export async function getInitializedPrintWindow(
printFilePath: string,
width: number,
height: number
Expand Down
4 changes: 2 additions & 2 deletions src/pages/TemplateBuilder/TemplateBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 22 additions & 11 deletions src/utils/printTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
}
}

Expand Down
1 change: 1 addition & 0 deletions utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit e60ec6b

Please sign in to comment.