Skip to content

Build your personal knowledge base with Trilium Notes

refactor(desktop): move printing from server

Elian Doranaf6d07390400
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index ce7f2dd8de..5cf0255307 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -25,6 +25,7 @@ import IpcMessagingProvider from "./ipc_messaging_provider";
import DesktopPlatformProvider from "./platform_provider";
import { registerTriliumAppScheme } from "./protocol";
import { setupCustomDictionary } from "./services/custom_dictionary";
+import { setupPrintingHandlers } from "./services/printing";
import { setupShellHandlers } from "./services/shell";
import { setupSystemTray } from "./services/tray";
@@ -95,6 +96,7 @@ async function main() {
setupSystemTray();
setupCustomDictionary();
setupShellHandlers();
+ setupPrintingHandlers();
app.on("will-quit", () => {
globalShortcut.unregisterAll();
diff --git a/apps/server/src/services/printing.ts b/apps/desktop/src/services/printing.ts
similarity index 92%
rename from apps/server/src/services/printing.ts
rename to apps/desktop/src/services/printing.ts
index e3f038aa5e..30d23ee5ba 100644
--- a/apps/server/src/services/printing.ts
+++ b/apps/desktop/src/services/printing.ts
@@ -1,9 +1,30 @@
+import { utils as coreUtils } from "@triliumnext/core";
+import log from "@triliumnext/server/src/services/log.js";
import { default as electron, ipcMain, type IpcMainEvent } from "electron";
import fs from "fs/promises";
import { t } from "i18next";
-
-import log from "./log.js";
-import { formatDownloadTitle } from "./utils.js";
+import path from "path";
+
+// Preload bundle path — built next to main.cjs in production by
+// apps/desktop/scripts/build.ts, or compiled in-place by the dev runner
+// (scripts/electron-start.mts) into apps/desktop/src/preload.compiled.cjs.
+// Print windows share the same preload as the main window so the renderer
+// has access to `window.electronApi.printing.sendPrintProgress`.
+//
+// Lazy: `coreUtils.isDev()` calls `getPlatform()` which throws until
+// `initializeCore()` has run. Module load happens before that, so we defer
+// resolution to the first print invocation.
+let preloadScriptCache: string | undefined;
+function getPreloadScript(): string {
+ if (preloadScriptCache === undefined) {
+ preloadScriptCache = path.resolve(
+ coreUtils.isDev()
+ ? path.join(__dirname, "..", "preload.compiled.cjs")
+ : path.join(__dirname, "..", "preload.cjs")
+ );
+ }
+ return preloadScriptCache;
+}
interface PrintOpts {
notePath: string;
@@ -72,8 +93,6 @@ function parsePageRangesForPrint(pageRanges: string): { from: number; to: number
return ranges.length ? ranges : undefined;
}
-let preloadScriptPath: string | undefined;
-
async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string, action: "printing" | "exporting_pdf") {
// Offscreen rendering crashes on Wayland due to a Chromium bug where the OSR surface
// lacks a valid xdg_toplevel role, causing a fatal zxdg_exporter_v2 protocol error.
@@ -92,7 +111,7 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string, ac
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
- preload: preloadScriptPath,
+ preload: getPreloadScript(),
offscreen: useOffscreen,
devTools: false,
session: e.sender.session
@@ -183,12 +202,8 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string, ac
return { browserWindow, printReport };
}
-/** Registers all printing-related IPC handlers. Call once on Electron startup.
- * `preloadPath` is the same preload bundle used by the main window — the print
- * window needs it to expose `window.electronApi.printing.sendPrintProgress`. */
-export function initPrintingHandlers(preloadPath: string) {
- preloadScriptPath = preloadPath;
-
+/** Registers all printing-related IPC handlers. Call once on Electron startup. */
+export function setupPrintingHandlers() {
electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => {
try {
const { browserWindow, printReport } = await getBrowserWindowForPrinting(e, notePath, "printing");
@@ -214,7 +229,7 @@ export function initPrintingHandlers(preloadPath: string) {
async function print() {
const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
- defaultPath: formatDownloadTitle(title, "file", "application/pdf"),
+ defaultPath: coreUtils.formatDownloadTitle(title, "file", "application/pdf"),
filters: [
{
name: t("pdf.export_filter"),
@@ -318,7 +333,7 @@ export function initPrintingHandlers(preloadPath: string) {
if (!focusedWindow) return;
const filePath = electron.dialog.showSaveDialogSync(focusedWindow, {
- defaultPath: formatDownloadTitle(title, "file", "application/pdf"),
+ defaultPath: coreUtils.formatDownloadTitle(title, "file", "application/pdf"),
filters: [
{
name: t("pdf.export_filter"),
diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts
index 31612c3cb9..2abe6b299c 100644
--- a/apps/server/src/services/window.ts
+++ b/apps/server/src/services/window.ts
@@ -7,7 +7,6 @@ import cls from "./cls.js";
import keyboardActionsService from "./keyboard_actions.js";
import log from "./log.js";
import optionService from "./options.js";
-import { initPrintingHandlers } from "./printing.js";
import { RESOURCE_DIR } from "./resource_dir.js";
import sqlInit from "./sql_init.js";
import { isDev, isMac, isWindows } from "./utils.js";
@@ -221,8 +220,6 @@ electron.ipcMain.on("navigation-history-go-to-index", (event, index: number) =>
event.sender.navigationHistory.goToIndex(index);
});
-initPrintingHandlers(PRELOAD_SCRIPT);
-
async function createMainWindow(app: App) {
if ("setUserTasks" in app) {
app.setUserTasks([