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 export with pagination disabled https://github.com/siyuan-note/siyuan/issues/19262
Daniel8e195b56a4bc
diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.tsindex ab5eaa163b..2190fbc9a6 100644--- a/app/src/protyle/export/index.ts+++ b/app/src/protyle/export/index.ts@@ -509,7 +509,7 @@ ${getIconScript(servePath)}<script src="${servePath}stage/protyle/js/lute/lute.min.js?${Constants.SIYUAN_VERSION}"></script><script>const previewElement = document.getElementById('preview');- const fixBlockWidth = async () => {+ const fixBlockWidth = async (printableWidth = false) => {const isLandscape = document.querySelector("#landscape").checked;let width = 800let height = 1131@@ -540,6 +540,10 @@ ${getIconScript(servePath)}break;}const scale = parseFloat(document.querySelector("#scale").value);+ if (printableWidth) {+ width -= ((parseFloat(document.querySelector("#marginsLeft").value) || 0) ++ (parseFloat(document.querySelector("#marginsRight").value) || 0)) * 96;+ }width = width / scale;height = (height -(parseFloat(document.querySelector("#marginsTop").value) +@@ -899,6 +903,17 @@ ${getIconScript(servePath)}reserveEmbeddedAssetSpace(removeAssetsElement.checked);await waitForImages();const isPaged = actionElement.querySelector("#paged").checked;+ document.body.classList.add("exporting");+ previewElement.style.zoom = "";+ previewElement.style.padding = "6px 0 0 0";+ if (!isPaged) {+ // 按打印可用宽度完成排版后测量,避免预览边距、缩放和列表布局影响长页高度。+ previewElement.style.margin = "0";+ previewElement.style.minHeight = "0";+ }+ await fixBlockWidth(!isPaged);+ await document.fonts.ready;+ await waitForImages();let exportConfig;if (!isPaged) {const getPageSizeDimensions = () => {@@ -913,22 +928,25 @@ ${getIconScript(servePath)}};return pageSizes[actionElement.querySelector("#pageSize").value];};- const previewHeight = Math.max(previewElement.scrollHeight / 96 - (parseFloat(document.querySelector("#marginsTop").value) || 0) - (parseFloat(document.querySelector("#marginsBottom").value) || 0), getPageSizeDimensions().height);- exportConfig = buildExportConfig(actionElement.querySelector("#landscape").checked ? {- height: getPageSizeDimensions().height,+ const dimensions = getPageSizeDimensions();+ const landscape = actionElement.querySelector("#landscape").checked;+ const scale = parseFloat(actionElement.querySelector("#scale").value);+ const margins = (parseFloat(document.querySelector("#marginsTop").value) || 0) ++ (parseFloat(document.querySelector("#marginsBottom").value) || 0);+ // 纸张高度包含缩放后的正文和打印边距,并预留一个像素以容纳单位换算误差。+ const previewHeight = Math.max((previewElement.scrollHeight * scale + 1) / 96 + margins,+ landscape ? dimensions.width : dimensions.height);+ exportConfig = buildExportConfig(landscape ? {+ height: dimensions.height,width: previewHeight,} : {- width: getPageSizeDimensions().width,+ width: dimensions.width,height: previewHeight,});} else {exportConfig = buildExportConfig();}exportConfig.filePaths = result.filePaths;- document.body.classList.add("exporting");- previewElement.style.zoom = "";- previewElement.style.padding = "6px 0 0 0";- await fixBlockWidth();actionElement.remove();ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", exportConfig);});diff --git a/app/tests/pdfUnpaged.test.js b/app/tests/pdfUnpaged.test.jsnew file mode 100644index 0000000000..3d6cdf5980--- /dev/null+++ b/app/tests/pdfUnpaged.test.js@@ -0,0 +1,107 @@+const assert = require("node:assert/strict");+const {readFileSync, mkdtempSync, rmSync} = 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});+ 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);+ assert.ok(offset >= 0);+ const limit = source.indexOf(end, offset);+ assert.ok(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;+ for (const landscape of [false, true]) {+ for (const scale of [0.5, 1, 1.5]) {+ for (const {margin, paged} of [{margin: 0, paged: false}, {margin: 1, paged: false},+ {margin: 1, paged: true}]) {+ await win.loadURL("data:text/html,<html><body></body></html>");+ await win.webContents.insertCSS(css);+ const options = await win.webContents.executeJavaScript(`(async () => {+ document.body.innerHTML = '<div id="action"></div><div id="preview"></div>';+ document.body.style.margin = "0";+ const actionElement = document.getElementById("action");+ const values = {pageSize: "A4", scale: ${scale}, marginsTop: ${margin},+ marginsBottom: ${margin}, marginsLeft: 0.54, marginsRight: 0.54,+ marginsType: "custom"};+ for (const [id, value] of Object.entries(values)) {+ 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 = ${landscape};+ document.getElementById("paged").checked = ${paged};+ const previewElement = document.getElementById("preview");+ previewElement.style.cssText = "box-sizing:border-box;margin:24px auto;padding:1in .54in;zoom:${scale}";+ previewElement.innerHTML = '<div class="protyle-wysiwyg" style="padding-top:8px;font:16px/24px Arial">' ++ '<div class="list" data-node-id="list">' + Array.from({length: 60}, (_, i) =>+ '<div class="li" data-node-id="item-' + i + '"><div style="padding-left:34px">Task ' + i + '</div>' ++ '<div class="list" data-node-id="nested-' + i + '"><div class="li" data-node-id="child-' + i + '">' ++ '<div style="padding-left:68px">' + '1234567890 '.repeat(12) + '</div></div></div></div>').join("") + '</div></div>';+ const Protyle = {highlightRender() {}, async mathRender() {}};+ const waitForImages = async () => {};+ const keepFoldElement = {}, addTitleElement = {}, customTitleElement = {},+ mergeSubdocsElement = {}, mergeDocHeadingModeElement = {},+ mergeContentHeadingModeElement = {}, watermarkElement = {}, removeAssetsElement = {};+ const response = {data: {name: "test"}};+ ${prepare}+ actionElement.remove();+ return exportConfig.pdfOptions;+ })()`);+ const pdf = await win.webContents.printToPDF({...options, displayHeaderFooter: true,+ headerTemplate: "<span></span>",+ footerTemplate: '<div style="font-size:10px"><span class="pageNumber"></span> / <span class="totalPages"></span></div>'});+ const pages = pdf.toString("latin1").match(/\/Type\s*\/Page\b/g) || [];+ if (paged) {+ assert.ok(pages.length > 1, JSON.stringify({landscape, scale, margin, paged}));+ } else {+ assert.equal(pages.length, 1, JSON.stringify({landscape, scale, margin, paged}));+ }+ }+ }+ }+ console.log("Unpaged PDF 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("exports long lists to one PDF page across margins, scales and orientations", {+ 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-unpaged-"));+ 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, /Unpaged PDF cases passed/);+ } finally {+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));+ assert.ok(path.basename(profile).startsWith("siyuan-pdf-unpaged-"));+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});+ }+ });+}