An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Improve PDF image pagination
Daniel1c284563a355
diff --git a/app/src/assets/scss/export.scss b/app/src/assets/scss/export.scssindex df048e4c6c..9baa99903a 100644--- a/app/src/assets/scss/export.scss+++ b/app/src/assets/scss/export.scss@@ -52,14 +52,26 @@ svg {}}-.exporting .protyle-wysiwyg [data-node-id] {- // 使用普通块布局以确保列表跨页后保持内容流顺序- &.list,- &.li {- display: block;+.exporting .protyle-wysiwyg {+ // 使用普通块布局以确保图片和列表跨页后保持内容流顺序。+ display: block;++ [data-node-id] {+ &.list,+ &.li {+ display: block;+ }}}+// 按纸张高度等比限制图片,预览和打印使用相同的尺寸约束。+#preview .img img[data-export-image-fit="true"] {+ max-height: var(--export-image-max-height);+ width: auto;+ height: auto;+ object-fit: contain;+}+@media print {.protyle-wysiwyg [data-export-heading-page-break="true"] {break-before: page;diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.tsindex 2f216e6c09..fbd5891cf6 100644--- a/app/src/protyle/export/index.ts+++ b/app/src/protyle/export/index.ts@@ -626,6 +626,30 @@ ${getIconScript(servePath)}item.parentElement.style.overflow = "hidden";}})+ const images = Array.from(previewElement.querySelectorAll(".img img"));+ // 先恢复用户设置的尺寸,再按当前纸张、边距和缩放计算,避免重复调整时只能缩小。+ images.forEach(image => image.removeAttribute("data-export-image-fit"));+ if (document.querySelector("#paged").checked) {+ images.forEach(image => {+ const block = image.closest("[data-node-id]") || image.parentElement;+ const style = getComputedStyle(block);+ const blockSpacing = [style.paddingTop, style.paddingBottom, style.marginTop, style.marginBottom]+ .reduce((sum, value) => sum + (parseFloat(value) || 0), 0);+ let maxHeight = height;+ // 图片缩窄后说明文字可能增加行数,逐步收紧高度以保留说明、段落间距和行盒的空间。+ while (true) {+ const extraHeight = Math.max(0, image.parentElement.offsetHeight - image.offsetHeight) ++ (parseFloat(style.lineHeight) || parseFloat(style.fontSize) * 1.625) + blockSpacing;+ const nextHeight = Math.max(1, Math.floor(height - extraHeight - 1));+ if (image.clientHeight <= nextHeight || nextHeight >= maxHeight) {+ break;+ }+ maxHeight = nextHeight;+ image.style.setProperty("--export-image-max-height", maxHeight + "px");+ image.setAttribute("data-export-image-fit", "true");+ }+ });+ }}const setPadding = () => {const isLandscape = document.querySelector("#landscape").checked;@@ -691,6 +715,23 @@ ${getIconScript(servePath)}}})}+ const waitForImages = () => Promise.all(Array.from(previewElement.querySelectorAll("img")).map((image) => {+ image.loading = "eager";+ if (image.complete) {+ return Promise.resolve();+ }+ return new Promise((resolve) => {+ const finish = () => {+ clearTimeout(timeout);+ image.removeEventListener("load", finish);+ image.removeEventListener("error", finish);+ resolve();+ };+ const timeout = setTimeout(finish, 30000);+ image.addEventListener("load", finish, {once: true});+ image.addEventListener("error", finish, {once: true});+ });+ }));let previewReady = Promise.resolve();let previewRevision = 0;const renderPreview = async (data) => {@@ -730,6 +771,7 @@ ${getIconScript(servePath)}Protyle.htmlRender(wysElement);Protyle.plantumlRender(wysElement, "${servePath}stage/protyle");updateHeadingPageBreaks();+ await waitForImages();}fetchPost("/api/export/exportPreviewHTML", {id: "${id}",@@ -904,6 +946,9 @@ ${getIconScript(servePath)}actionElement.querySelector("#landscape").addEventListener('change', (e) => {setPadding();});+ actionElement.querySelector("#paged").addEventListener('change', () => {+ fixBlockWidth();+ });actionElement.querySelector('.b3-button--cancel').addEventListener('click', () => {const {ipcRenderer} = require("electron");ipcRenderer.send("${Constants.SIYUAN_CMD}", "destroy")@@ -961,23 +1006,6 @@ ${getIconScript(servePath)}removeAssetsElement.addEventListener("change", () => {reserveEmbeddedAssetSpace(removeAssetsElement.checked);});- const waitForImages = () => Promise.all(Array.from(previewElement.querySelectorAll("img")).map((image) => {- image.loading = "eager";- if (image.complete) {- return Promise.resolve();- }- return new Promise((resolve) => {- const finish = () => {- clearTimeout(timeout);- image.removeEventListener("load", finish);- image.removeEventListener("error", finish);- resolve();- };- const timeout = setTimeout(finish, 30000);- image.addEventListener("load", finish, {once: true});- image.addEventListener("error", finish, {once: true});- });- }));actionElement.querySelector('.b3-button--text').addEventListener('click', async () => {await previewReady;const {ipcRenderer} = require("electron");@@ -1007,7 +1035,7 @@ ${getIconScript(servePath)}previewElement.style.margin = "0";previewElement.style.minHeight = "0";}- await fixBlockWidth(!isPaged);+ await fixBlockWidth(true);await document.fonts.ready;await waitForImages();let exportConfig;@@ -1169,7 +1197,11 @@ export const onExport = async (data: IWebSocketData, filePath: string, servePathconst mobileHtml = isInMobile ? {js: exportOption.type === "pdf" ? "" : `document.body.style.minWidth = "${getScreenWidth()}px";`,css: `@page { size: A4; margin: 10mm; background-color: var(--b3-theme-background); }-@media print { body { margin: 0; } }+@media print {+ body { margin: 0; }+ .protyle-wysiwyg { display: block; }+ #preview .img img { max-height: calc(277mm - 32px); object-fit: contain; }+}.protyle-wysiwyg {padding: 0; margin: 0;}`} : {js: "", css: ""};const html = `<!DOCTYPE html>diff --git a/app/tests/pdfImages.test.js b/app/tests/pdfImages.test.jsnew file mode 100644index 0000000000..ee857157a2--- /dev/null+++ b/app/tests/pdfImages.test.js@@ -0,0 +1,201 @@+const assert = require("node:assert/strict");+const {readFileSync, mkdtempSync, rmSync, writeFileSync} = require("node:fs");+const path = require("node:path");+const os = require("node:os");++const runElectron = async () => {+ const {app, BrowserWindow} = require("electron");+ app.setPath("userData", process.argv[2]);+ await app.whenReady();+ const win = new BrowserWindow({show: false, webPreferences: {backgroundThrottling: false}});+ let exitCode = 0;+ try {+ const source = readFileSync(path.join(__dirname, "../src/protyle/export/index.ts"), "utf8");+ const section = (start, end) => {+ const offset = source.indexOf(start);+ const limit = source.indexOf(end, offset);+ assert.ok(offset >= 0 && limit > offset);+ return source.slice(offset, limit);+ };+ const prepare = section(" const fixBlockWidth =", " const setPadding =") ++ section(" const buildExportConfig =", " const reserveEmbeddedAssetSpace =")+ .replaceAll("${currentWindowId}", "0") ++ section(" const isPaged =", " exportConfig.filePaths =");+ const css = require("sass").compile(path.join(__dirname, "../src/assets/scss/export.scss")).css;+ const desktopCSS = section(" body {", " ${await setInlineStyle(false, servePath)}");+ const mobileStart = source.indexOf("css: `@page") + "css: `".length;+ const mobileCSS = source.slice(mobileStart, source.indexOf("`", mobileStart));+ const moduleURL = name => "data:text/javascript;base64," + Buffer.from(readFileSync(+ path.join(__dirname, "../stage/protyle/js/pdf/", name))).toString("base64");+ const runtime = moduleURL("pdf.min.mjs");+ const worker = moduleURL("pdf.worker.min.mjs");+ const cases = [+ {name: "full-size", scale: 1, margin: 1},+ {name: "landscape", scale: 1, margin: 0.42, landscape: true},+ {name: "scaled-up", scale: 1.5, margin: 0.5},+ {name: "scaled-down", scale: 0.5, margin: 0},+ {name: "small-image", scale: 1, margin: 1, small: true},+ {name: "resized-image", scale: 1, margin: 1, width: "150px", expectedHeight: 300},+ {name: "enlarged-image", scale: 1, margin: 1, small: true, naturalWidth: 120},+ {name: "caption", scale: 1, margin: 1, caption: true},+ {name: "unpaged", scale: 1, margin: 1, unpaged: true},+ {name: "mobile", scale: 1, margin: 10 / 25.4, mobile: true},+ ];+ for (const item of cases) {+ await win.loadURL("data:text/html,<!DOCTYPE html><html><body></body></html>");+ await win.webContents.insertCSS(css + (item.mobile ? mobileCSS : desktopCSS));+ const options = await win.webContents.executeJavaScript(`(async () => {+ const item = ${JSON.stringify(item)};+ document.body.innerHTML = '<div id="action"></div><div id="previewContainer"><div id="preview"></div></div>';+ const actionElement = document.getElementById("action");+ for (const [id, value] of Object.entries({pageSize: "A4", scale: item.scale,+ marginsTop: item.margin, marginsBottom: item.margin,+ marginsLeft: 0.54, marginsRight: 0.54, marginsType: "custom"})) {+ actionElement.insertAdjacentHTML("beforeend", '<input id="' + id + '" value="' + value + '">');+ }+ actionElement.insertAdjacentHTML("beforeend", '<input id="landscape" type="checkbox"><input id="paged" type="checkbox">');+ document.getElementById("landscape").checked = !!item.landscape;+ document.getElementById("paged").checked = !item.unpaged;+ const previewElement = document.getElementById("preview");+ previewElement.style.cssText = "box-sizing:border-box;margin:24px auto;padding:1in .54in;zoom:" + item.scale;+ const canvas = document.createElement("canvas");+ canvas.width = item.naturalWidth || 600;+ canvas.height = item.small ? canvas.width / 3 : 1600;+ const ctx = canvas.getContext("2d");+ ctx.fillStyle = "#ff8000";+ ctx.fillRect(0, 0, canvas.width, canvas.height);+ ctx.fillStyle = "#0000ff";+ ctx.fillRect(0, 0, canvas.width, canvas.height / 10);+ ctx.fillStyle = "#00ff00";+ ctx.fillRect(0, canvas.height * 0.9, canvas.width, canvas.height / 10);+ const image = '<span class="img" data-type="img" style="display: inline-block;' ++ (item.naturalWidth ? 'width:600px' : '') + '">' ++ '<span> </span><span style="width:' + (item.width || "100%") + '"><img src="' + canvas.toDataURL() + '">' ++ (item.caption ? '<span class="img__alt"><span>' + 'Image caption '.repeat(30) + '</span></span>' : "") ++ '</span><span> </span></span>';+ previewElement.innerHTML = '<div class="protyle-wysiwyg" style="padding:8px 0 0;font:16px/26px Arial">' ++ '<div data-node-id="before" data-type="NodeParagraph" style="height:140px">Before image</div>' ++ '<div data-node-id="picture" data-type="NodeParagraph">' + image + '<hr style="margin:0;border:0"></div>' ++ '<div data-node-id="after" data-type="NodeHeading">After image</div></div>';+ await previewElement.querySelector("img").decode();+ if (item.mobile) {+ previewElement.style.cssText = "max-width:800px;margin:0 auto";+ actionElement.remove();+ return {pageSize: "A4", preferCSSPageSize: true, printBackground: true};+ }+ const Protyle = {highlightRender() {}, async mathRender() {}};+ const waitForImages = async () => {};+ const keepFoldElement = {}, addTitleElement = {}, titleImageElement = {}, customTitleElement = {},+ mergeSubdocsElement = {}, mergeDocHeadingModeElement = {}, mergeContentHeadingModeElement = {},+ watermarkElement = {}, removeAssetsElement = {};+ const response = {data: {name: "test"}};+ const getHeadingPageBreakLevels = () => [];+ const updateHeadingPageBreaks = () => {};+ ${prepare}+ const firstHeight = previewElement.querySelector("img").clientHeight;+ document.getElementById("paged").checked = false;+ await fixBlockWidth(true);+ const naturalHeight = previewElement.querySelector("img").clientHeight;+ document.getElementById("paged").checked = !item.unpaged;+ await fixBlockWidth(true);+ if (previewElement.querySelector("img").clientHeight !== firstHeight || naturalHeight < firstHeight) {+ throw new Error("Toggling pagination must restore and reapply the selected image size");+ }+ actionElement.remove();+ return exportConfig.pdfOptions;+ })()`);+ const pdf = await win.webContents.printToPDF(options);+ const result = await win.webContents.executeJavaScript(`(async () => {+ const {getDocument} = await import(${JSON.stringify(runtime)});+ globalThis.pdfjsWorker = await import(${JSON.stringify(worker)});+ const task = getDocument({data: Uint8Array.from(atob(${JSON.stringify(pdf.toString("base64"))}), c => c.charCodeAt(0))});+ const pdf = await task.promise;+ const pages = [];+ for (let i = 1; i <= pdf.numPages; i++) {+ const page = await pdf.getPage(i);+ const viewport = page.getViewport({scale: 1});+ const canvas = document.createElement("canvas");+ canvas.width = Math.ceil(viewport.width);+ canvas.height = Math.ceil(viewport.height);+ const ctx = canvas.getContext("2d");+ await page.render({canvasContext: ctx, viewport}).promise;+ const pixels = ctx.getImageData(0, 0, canvas.width, canvas.height).data;+ const bounds = {};+ for (let y = 0; y < canvas.height; y++) {+ for (let x = 0; x < canvas.width; x++) {+ const j = (y * canvas.width + x) * 4;+ const [r, g, b] = pixels.subarray(j, j + 3);+ const color = b > 240 && r < 15 && g < 15 ? "top" :+ g > 240 && r < 15 && b < 15 ? "bottom" :+ r > 240 && g > 110 && g < 145 && b < 15 ? "image" : null;+ if (!color) continue;+ const rect = bounds[color] ||= {left: x, right: x, top: y, bottom: y};+ rect.left = Math.min(rect.left, x); rect.right = Math.max(rect.right, x);+ rect.top = Math.min(rect.top, y); rect.bottom = Math.max(rect.bottom, y);+ }+ }+ const text = await page.getTextContent();+ const after = text.items.find(item => item.str === "After image");+ pages.push({bounds, hasText: text.items.some(item => item.str.trim()),+ afterTop: after ? viewport.height - after.transform[5] - after.height : null,+ png: ${!!process.env.SIYUAN_PDF_IMAGE_PREVIEW} ? canvas.toDataURL().split(",")[1] : undefined});+ }+ await task.destroy();+ return pages;+ })()`);+ if (process.env.SIYUAN_PDF_IMAGE_PREVIEW) {+ result.forEach((page, i) => writeFileSync(path.join(process.env.SIYUAN_PDF_IMAGE_PREVIEW,+ `${item.name}-${i + 1}.png`), Buffer.from(page.png, "base64")));+ }+ const imagePages = result.filter(page => Object.keys(page.bounds).length);+ assert.ok(result.every(page => page.hasText || page.bounds.image), `${item.name}: no blank PDF pages`);+ assert.equal(imagePages.length, 1, `${item.name}: image must stay on one page`);+ const imagePage = imagePages[0];+ assert.ok(imagePage.bounds.top && imagePage.bounds.bottom, `${item.name}: both ends must remain visible`);+ const height = imagePage.bounds.bottom.bottom - imagePage.bounds.top.top + 1;+ const width = imagePage.bounds.image.right - imagePage.bounds.image.left + 1;+ const ratio = 600 / (item.small ? 200 : 1600);+ assert.ok(Math.abs(width / height - ratio) / ratio < 0.01,+ `${item.name}: image aspect ratio must be preserved`);+ const afterIndex = result.findIndex(page => page.afterTop !== null);+ const imageIndex = result.indexOf(imagePage);+ assert.ok(afterIndex > imageIndex || afterIndex === imageIndex &&+ result[afterIndex].afterTop >= imagePage.bounds.bottom.bottom - 1,+ `${item.name}: following text must not overlap the image`);+ if (item.unpaged) assert.equal(result.length, 1, "Unpaged exports retain the entire long image");+ if (item.small) assert.ok(height > 140 && height < 160,+ `${item.name}: small images retain their displayed size (${height})`);+ if (item.expectedHeight) assert.ok(Math.abs(height - item.expectedHeight) < 3,+ "Manually resized images retain their displayed size");+ }+ console.log("PDF image layout cases passed");+ } catch (error) {+ console.error(error);+ exitCode = 1;+ } finally {+ win.destroy();+ app.exit(exitCode);+ }+};++if (process.versions.electron && process.type === "browser") {+ runElectron().catch(error => { console.error(error); require("electron").app.exit(1); });+} else {+ require("node:test").it("PDF images fit one page without distortion or overlapping following text", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 60000,+ }, async () => {+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-pdf-images-"));+ try {+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 55000});+ assert.match(stdout, /PDF image layout cases passed/);+ } finally {+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));+ assert.ok(path.basename(profile).startsWith("siyuan-pdf-images-"));+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});+ }+ });+}