2
0
mirror of https://github.com/frappe/books.git synced 2025-02-08 15:08:29 +00:00

feat: implement print document functionality

This commit is contained in:
AbleKSaju 2025-01-14 09:22:12 +05:30
parent 960fd06591
commit e60ec6b4bc
7 changed files with 75 additions and 14 deletions

View File

@ -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

28
main/printHtmlDocument.ts Normal file
View File

@ -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;
}

View File

@ -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) => {

View File

@ -35,7 +35,7 @@ export async function saveHtmlAsPdf(
return true;
}
async function getInitializedPrintWindow(
export async function getInitializedPrintWindow(
printFilePath: string,
width: number,
height: number

View File

@ -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;

View File

@ -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' });
}
}
}

View File

@ -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',