Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(print): thick headings on low-DPI displays
Elian Doran60047c6df553
diff --git a/apps/client/src/widgets/dialogs/print_preview.tsx b/apps/client/src/widgets/dialogs/print_preview.tsxindex df2d0edcd5..2a92c8a7b5 100644--- a/apps/client/src/widgets/dialogs/print_preview.tsx+++ b/apps/client/src/widgets/dialogs/print_preview.tsx@@ -413,7 +413,7 @@ export default function PrintPreviewDialog() {<span class="bx bx-loader-circle bx-spin" /></div>)}- {pdfUrl && <PdfViewer pdfUrl={pdfUrl} toolbar={false} disableSelection />}+ {pdfUrl && <PdfViewer pdfUrl={pdfUrl} toolbar={false} disableSelection minPixelRatio={2} />}</div></Modal>);diff --git a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsxindex aa742d325d..26459ff02b 100644--- a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx+++ b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx@@ -26,12 +26,19 @@ interface PdfViewerProps extends Pick<HTMLAttributes<HTMLIFrameElement>, "tabIndtoolbar?: boolean;/** If set, disables text selection in the rendered PDF. */disableSelection?: boolean;+ /**+ * Forces the rendered pages to use at least this device-pixel-ratio when rasterizing to canvas.+ * On standard-DPI displays (DPR 1) PDF.js renders at 1× and text/headings look coarsely+ * anti-aliased; raising this supersamples the canvas (mimicking a high-DPI screen) for a+ * crisper preview. Has no effect when the real DPR already meets or exceeds this value.+ */+ minPixelRatio?: number;}/*** Reusable component displaying a PDF. The PDF needs to be provided via a URL.*/-export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad, editable, toolbar = true, disableSelection }: PdfViewerProps) {+export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad, editable, toolbar = true, disableSelection, minPixelRatio }: PdfViewerProps) {const iframeRef = useSyncedRef(externalIframeRef, null);const [ locale ] = useTriliumOption("locale");const [ newLayout ] = useTriliumOptionBool("newLayout");@@ -42,7 +49,7 @@ export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoadref={iframeRef}class="pdf-preview"style={{width: "100%", height: "100%"}}- src={`pdfjs/web/viewer.html?v=${glob.triliumVersion}&file=${pdfUrl}&locale=${locale}&sidebar=${newLayout ? "0" : "1"}&editable=${editable ? "1" : "0"}&toolbar=${toolbar ? "1" : "0"}`}+ src={`pdfjs/web/viewer.html?v=${glob.triliumVersion}&file=${pdfUrl}&locale=${locale}&sidebar=${newLayout ? "0" : "1"}&editable=${editable ? "1" : "0"}&toolbar=${toolbar ? "1" : "0"}${minPixelRatio ? `&minPixelRatio=${minPixelRatio}` : ""}`}onLoad={() => {injectStyles();onLoad?.();diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.tsindex 4689eef3ba..91b46f9de2 100644--- a/packages/pdfjs-viewer/src/custom.ts+++ b/packages/pdfjs-viewer/src/custom.ts@@ -9,6 +9,8 @@ async function main() {const urlParams = new URLSearchParams(window.location.search);const isEditable = urlParams.get("editable") === "1";+ applyMinPixelRatio(urlParams);+const hideToolbar = urlParams.get("toolbar") === "0";document.body.classList.toggle("read-only-document", !isEditable);document.body.classList.toggle("no-toolbar", hideToolbar);@@ -52,6 +54,24 @@ async function main() {await app.initializedPromise;};+/**+ * Forces a minimum device-pixel-ratio for canvas rasterization. PDF.js sizes each page's+ * canvas backing store by `globalThis.devicePixelRatio` (read dynamically at render time via+ * `OutputScale`), so on a standard-DPI display (DPR 1) pages render at 1× and text/headings+ * look coarsely anti-aliased. Overriding the getter to a higher minimum supersamples the+ * canvas — the same crispness a high-DPI screen gets for free — without changing layout size.+ */+function applyMinPixelRatio(urlParams: URLSearchParams) {+ const minPixelRatio = Number(urlParams.get("minPixelRatio"));+ if (!Number.isFinite(minPixelRatio) || minPixelRatio <= 0) return;+ if ((window.devicePixelRatio || 1) >= minPixelRatio) return;++ Object.defineProperty(window, "devicePixelRatio", {+ configurable: true,+ get: () => minPixelRatio+ });+}+function configurePdfViewerOptions() {const urlParams = new URLSearchParams(window.location.search);const locale = urlParams.get("locale");