mirror of
https://github.com/zadam/trilium.git
synced 2025-10-27 00:06:30 +01:00
fix(desktop/print): proper reporting when it finishes
This commit is contained in:
@@ -1723,7 +1723,8 @@
|
|||||||
},
|
},
|
||||||
"note_detail": {
|
"note_detail": {
|
||||||
"could_not_find_typewidget": "Could not find typeWidget for type '{{type}}'",
|
"could_not_find_typewidget": "Could not find typeWidget for type '{{type}}'",
|
||||||
"printing": "Printing in progress..."
|
"printing": "Printing in progress...",
|
||||||
|
"printing_pdf": "Exporting to PDF in progress..."
|
||||||
},
|
},
|
||||||
"note_title": {
|
"note_title": {
|
||||||
"placeholder": "type note's title here..."
|
"placeholder": "type note's title here..."
|
||||||
|
|||||||
@@ -141,6 +141,13 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
|
|||||||
doRender() {
|
doRender() {
|
||||||
this.$widget = $(TPL);
|
this.$widget = $(TPL);
|
||||||
this.contentSized();
|
this.contentSized();
|
||||||
|
|
||||||
|
if (utils.isElectron()) {
|
||||||
|
const { ipcRenderer } = utils.dynamicRequire("electron");
|
||||||
|
ipcRenderer.on("print-done", () => {
|
||||||
|
toast.closePersistent("printing");
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async refresh() {
|
async refresh() {
|
||||||
@@ -330,6 +337,12 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toast.showPersistent({
|
||||||
|
icon: "bx bx-loader-circle bx-spin",
|
||||||
|
message: t("note_detail.printing_pdf"),
|
||||||
|
id: "printing"
|
||||||
|
});
|
||||||
|
|
||||||
const { ipcRenderer } = utils.dynamicRequire("electron");
|
const { ipcRenderer } = utils.dynamicRequire("electron");
|
||||||
ipcRenderer.send("export-as-pdf", {
|
ipcRenderer.send("export-as-pdf", {
|
||||||
title: this.note.title,
|
title: this.note.title,
|
||||||
|
|||||||
@@ -89,9 +89,59 @@ electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => {
|
|||||||
} else {
|
} else {
|
||||||
electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason);
|
electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason);
|
||||||
}
|
}
|
||||||
|
e.sender.send("print-done");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pageSize }: ExportAsPdfOpts) => {
|
||||||
|
async function print() {
|
||||||
|
const browserWindow = await getBrowserWindowForPrinting(e, notePath);
|
||||||
|
|
||||||
|
const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
|
||||||
|
defaultPath: formatDownloadTitle(title, "file", "application/pdf"),
|
||||||
|
filters: [
|
||||||
|
{
|
||||||
|
name: t("pdf.export_filter"),
|
||||||
|
extensions: ["pdf"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
if (!filePath) return;
|
||||||
|
|
||||||
|
let buffer: Buffer;
|
||||||
|
try {
|
||||||
|
buffer = await browserWindow.webContents.printToPDF({
|
||||||
|
landscape,
|
||||||
|
pageSize,
|
||||||
|
generateDocumentOutline: true,
|
||||||
|
generateTaggedPDF: true,
|
||||||
|
printBackground: true,
|
||||||
|
displayHeaderFooter: true,
|
||||||
|
headerTemplate: `<div></div>`,
|
||||||
|
footerTemplate: `
|
||||||
|
<div class="pageNumber" style="width: 100%; text-align: center; font-size: 10pt;">
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-export-message"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.writeFile(filePath, buffer);
|
||||||
|
} catch (e) {
|
||||||
|
electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-save-message"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
electron.shell.openPath(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
await print();
|
||||||
|
e.sender.send("print-done");
|
||||||
|
});
|
||||||
|
|
||||||
async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
|
async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
|
||||||
const browserWindow = new electron.BrowserWindow({
|
const browserWindow = new electron.BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
@@ -112,52 +162,6 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
|
|||||||
return browserWindow;
|
return browserWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pageSize }: ExportAsPdfOpts) => {
|
|
||||||
const browserWindow = await getBrowserWindowForPrinting(e, notePath);
|
|
||||||
|
|
||||||
const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
|
|
||||||
defaultPath: formatDownloadTitle(title, "file", "application/pdf"),
|
|
||||||
filters: [
|
|
||||||
{
|
|
||||||
name: t("pdf.export_filter"),
|
|
||||||
extensions: ["pdf"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
if (!filePath) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let buffer: Buffer;
|
|
||||||
try {
|
|
||||||
buffer = await browserWindow.webContents.printToPDF({
|
|
||||||
landscape,
|
|
||||||
pageSize,
|
|
||||||
generateDocumentOutline: true,
|
|
||||||
generateTaggedPDF: true,
|
|
||||||
printBackground: true,
|
|
||||||
displayHeaderFooter: true,
|
|
||||||
headerTemplate: `<div></div>`,
|
|
||||||
footerTemplate: `
|
|
||||||
<div class="pageNumber" style="width: 100%; text-align: center; font-size: 10pt;">
|
|
||||||
</div>
|
|
||||||
`
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-export-message"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fs.writeFile(filePath, buffer);
|
|
||||||
} catch (e) {
|
|
||||||
electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-save-message"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
electron.shell.openPath(filePath);
|
|
||||||
});
|
|
||||||
|
|
||||||
async function createMainWindow(app: App) {
|
async function createMainWindow(app: App) {
|
||||||
if ("setUserTasks" in app) {
|
if ("setUserTasks" in app) {
|
||||||
app.setUserTasks([
|
app.setUserTasks([
|
||||||
|
|||||||
Reference in New Issue
Block a user