diff --git a/reports/commonExporter.js b/reports/commonExporter.js index 3c953834..54a0b9e8 100644 --- a/reports/commonExporter.js +++ b/reports/commonExporter.js @@ -1,10 +1,5 @@ import frappe from 'frappejs'; -import { - getSavePath, - saveData, - showItemInFolder, - showToast, -} from '../src/utils'; +import { getSavePath, saveData, showExportInFolder } from '../src/utils'; function templateToInnerText(innerHTML) { const temp = document.createElement('template'); @@ -106,11 +101,5 @@ export default function getCommonExportActions(reportName) { export async function saveExportData(data, filePath) { await saveData(data, filePath); - showToast({ - message: frappe._('Export Successful'), - actionText: frappe._('Open Folder'), - action: async () => { - await showItemInFolder(filePath); - }, - }); + showExportInFolder(frappe._('Export Successful'), filePath); } diff --git a/src/saveHtmlAsPdf.js b/src/saveHtmlAsPdf.js index d90d9f2c..8d448810 100644 --- a/src/saveHtmlAsPdf.js +++ b/src/saveHtmlAsPdf.js @@ -1,6 +1,5 @@ -import fs from 'fs'; -import { sleep } from 'frappejs/utils'; -import { shell, BrowserWindow } from 'electron'; +import { BrowserWindow } from 'electron'; +import fs from 'fs/promises'; const PRINT_OPTIONS = { marginsType: 1, // no margin @@ -17,14 +16,16 @@ export default async function makePDF(html, savePath) { document.body.innerHTML = \`${html}\`; `); - printWindow.webContents.on('did-finish-load', async () => { - await sleep(1); // Required else pdf'll be blank. - printWindow.webContents.printToPDF(PRINT_OPTIONS).then((data) => { - // printWindow.destroy(); - fs.writeFile(savePath, data, (error) => { + return await new Promise((resolve) => { + printWindow.webContents.on('did-finish-load', async () => { + await sleep(1); // Required else pdf'll be blank. + + const data = await printWindow.webContents.printToPDF(PRINT_OPTIONS); + await fs.writeFile(savePath, data, (error) => { if (error) throw error; - return shell.openPath(savePath); }); + + resolve(); }); }); } diff --git a/src/utils.js b/src/utils.js index f9767266..ee5043c1 100644 --- a/src/utils.js +++ b/src/utils.js @@ -174,6 +174,17 @@ export function handleErrorWithDialog(e, doc) { export async function makePDF(html, savePath) { await ipcRenderer.invoke(IPC_ACTIONS.SAVE_HTML_AS_PDF, html, savePath); + showExportInFolder(frappe._('Save as PDF Successful'), savePath); +} + +export function showExportInFolder(message, filePath) { + showToast({ + message, + actionText: frappe._('Open Folder'), + action: async () => { + await showItemInFolder(filePath); + }, + }); } export async function saveData(data, savePath) {