feat(print): implement basic export as PDF support

This commit is contained in:
Elian Doran 2025-01-31 21:44:59 +02:00
parent dbf004d673
commit 0f7826d6d0
No known key found for this signature in database
2 changed files with 24 additions and 3 deletions

View File

@ -32,6 +32,7 @@ import AttachmentDetailTypeWidget from "./type_widgets/attachment_detail.js";
import MindMapWidget from "./type_widgets/mind_map.js";
import { getStylesheetUrl, isSyntaxHighlightEnabled } from "../services/syntax_highlight.js";
import GeoMapTypeWidget from "./type_widgets/geo_map.js";
import utils from "../services/utils.js";
const TPL = `
<div class="note-detail">
@ -296,7 +297,8 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
return;
}
alert("Hi");
const { ipcRenderer } = utils.dynamicRequire("electron");
ipcRenderer.send("export-as-pdf");
}
hoistedNoteChangedEvent({ ntxId }) {

View File

@ -1,3 +1,4 @@
import fs from "fs";
import path from "path";
import url from "url";
import port from "./port.js";
@ -7,8 +8,8 @@ import sqlInit from "./sql_init.js";
import cls from "./cls.js";
import keyboardActionsService from "./keyboard_actions.js";
import remoteMain from "@electron/remote/main/index.js";
import type { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents } from "electron";
import { ipcMain } from "electron";
import { BrowserWindow, type App, type BrowserWindowConstructorOptions, type WebContents } from "electron";
import { dialog, ipcMain } from "electron";
import { isDev, isMac, isWindows } from "./utils.js";
import { fileURLToPath } from "url";
@ -46,6 +47,24 @@ ipcMain.on("create-extra-window", (event, arg) => {
createExtraWindow(arg.extraWindowHash);
});
ipcMain.on("export-as-pdf", async (e) => {
const browserWindow = BrowserWindow.fromWebContents(e.sender);
if (!browserWindow) {
return;
}
const filePath = dialog.showSaveDialogSync(browserWindow, {});
if (!filePath) {
return;
}
// TODO: Report if there is an error in generating the PDF.
const buffer = await browserWindow.webContents.printToPDF({});
// TODO: Report if there was an error in saving the PDF.
fs.writeFileSync(filePath, buffer);
});
async function createMainWindow(app: App) {
if ("setUserTasks" in app) {
app.setUserTasks([