Skip to content

Build your personal knowledge base with Trilium Notes

feat(pdf): make printable using PDF.js builtin mechanism

Elian Doran8d6597f198d1
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index b5f94b75fa..8476e32172 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -182,6 +182,9 @@ export default function NoteDetail() {
useTriliumEvent("printActiveNote", () => {
if (!noteContext?.isActive() || !note) return;
+ // PDF printing is handled by the PDF viewer's own print mechanism.
+ if (note.type === "file" && note.mime === "application/pdf") return;
+
if (isElectron()) {
// On Electron, open the print preview dialog. Actual print/PDF actions
// are triggered from the dialog's footer buttons.
diff --git a/apps/client/src/widgets/ribbon/NoteActions.tsx b/apps/client/src/widgets/ribbon/NoteActions.tsx
index 8ea30baeae..c518176cb6 100644
--- a/apps/client/src/widgets/ribbon/NoteActions.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActions.tsx
@@ -82,7 +82,8 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNote
const isContentAvailable = note.isContentAvailable();
const isPrintable = isContentAvailable && (
["text", "code", "spreadsheet"].includes(noteType) ||
- (noteType === "book" && ["presentation", "list", "table"].includes(viewType ?? ""))
+ (noteType === "book" && ["presentation", "list", "table"].includes(viewType ?? "")) ||
+ (noteType === "file" && note.mime === "application/pdf")
);
const isElectron = getIsElectron();
const isMac = getIsMac();
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
index d20665ab6b..311074ccdc 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -181,6 +181,13 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
});
});
+ useTriliumEvent("printActiveNote", () => {
+ if (!noteContext.isActive()) return;
+ iframeRef.current?.contentWindow?.postMessage({
+ type: "trilium-print"
+ }, window.location.origin);
+ });
+
return (historyConfig &&
<PdfViewer
iframeRef={iframeRef}
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index 5c931a5094..b65e05125c 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -28,6 +28,8 @@ async function main() {
}
const app = window.PDFViewerApplication;
+ managePrint();
+
if (isEditable) {
app.eventBus.on("documentloaded", () => {
manageSave();
@@ -127,4 +129,14 @@ function manageDownload() {
});
}
+function managePrint() {
+ window.addEventListener("message", event => {
+ if (event.origin !== window.location.origin) return;
+
+ if (event.data?.type === "trilium-print") {
+ window.print();
+ }
+ });
+}
+
main();