mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-30 20:25:17 +01:00
This commit is contained in:
parent
cef8ca8cf7
commit
f6fdfb654b
2 changed files with 49 additions and 72 deletions
|
|
@ -221,9 +221,6 @@ export const initWindow = async (app: App) => {
|
|||
});
|
||||
ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, async (e, ipcData) => {
|
||||
const msgId = showMessage(window.siyuan.languages.exporting, -1);
|
||||
const isPaged = ipcData.paged !== undefined ? ipcData.paged : true;
|
||||
// 分页模式下 pageSize 是字符串,不分页模式下是对象,统一使用 pageSizeValue 获取原始页面大小
|
||||
const pageSizeValue = ipcData.pageSizeValue !== undefined ? ipcData.pageSizeValue : (isPaged ? ipcData.pdfOptions.pageSize : "A4");
|
||||
window.siyuan.storage[Constants.LOCAL_EXPORTPDF] = {
|
||||
removeAssets: ipcData.removeAssets,
|
||||
keepFold: ipcData.keepFold,
|
||||
|
|
@ -231,17 +228,17 @@ export const initWindow = async (app: App) => {
|
|||
watermark: ipcData.watermark,
|
||||
landscape: ipcData.pdfOptions.landscape,
|
||||
marginType: ipcData.pdfOptions.marginType,
|
||||
pageSize: pageSizeValue,
|
||||
pageSize: ipcData.pageSize,
|
||||
scale: ipcData.pdfOptions.scale,
|
||||
marginTop: ipcData.pdfOptions.margins.top,
|
||||
marginRight: ipcData.pdfOptions.margins.right,
|
||||
marginBottom: ipcData.pdfOptions.margins.bottom,
|
||||
marginLeft: ipcData.pdfOptions.margins.left,
|
||||
paged: isPaged,
|
||||
paged: ipcData.paged,
|
||||
};
|
||||
setStorageVal(Constants.LOCAL_EXPORTPDF, window.siyuan.storage[Constants.LOCAL_EXPORTPDF]);
|
||||
try {
|
||||
if (window.siyuan.config.export.pdfFooter.trim() && isPaged) {
|
||||
if (window.siyuan.config.export.pdfFooter.trim()) {
|
||||
const response = await fetchSyncPost("/api/template/renderSprig", {template: window.siyuan.config.export.pdfFooter});
|
||||
ipcData.pdfOptions.displayHeaderFooter = true;
|
||||
ipcData.pdfOptions.headerTemplate = "<span></span>";
|
||||
|
|
|
|||
|
|
@ -137,6 +137,9 @@ const getSnippetJS = () => {
|
|||
/// #if !BROWSER
|
||||
const renderPDF = async (id: string) => {
|
||||
const localData = window.siyuan.storage[Constants.LOCAL_EXPORTPDF];
|
||||
if (typeof localData.paged === "undefined") {
|
||||
localData.paged = true;
|
||||
}
|
||||
const servePathWithoutTrailingSlash = window.location.protocol + "//" + window.location.host;
|
||||
const servePath = servePathWithoutTrailingSlash + "/";
|
||||
const isDefault = (window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark === "midnight") || (window.siyuan.config.appearance.mode === 0 && window.siyuan.config.appearance.themeLight === "daylight");
|
||||
|
|
@ -304,7 +307,7 @@ const renderPDF = async (id: string) => {
|
|||
${window.siyuan.languages.exportPDF1}
|
||||
</div>
|
||||
<span class="fn__hr"></span>
|
||||
<input id="landscape" class="b3-switch" type="checkbox" ${localData.landscape && localData.paged ? "checked" : ""}>
|
||||
<input id="landscape" class="b3-switch" type="checkbox" ${localData.landscape ? "checked" : ""}>
|
||||
</label>
|
||||
<label class="b3-label">
|
||||
<div>
|
||||
|
|
@ -339,7 +342,7 @@ const renderPDF = async (id: string) => {
|
|||
${window.siyuan.languages.paged}
|
||||
</div>
|
||||
<span class="fn__hr"></span>
|
||||
<input id="paged" class="b3-switch" type="checkbox" ${(localData.paged === undefined ? true : localData.paged) ? "checked" : ""}>
|
||||
<input id="paged" class="b3-switch" type="checkbox" ${localData.paged ? "checked" : ""}>
|
||||
</label>
|
||||
</div>
|
||||
<div class="fn__flex" style="padding: 0 16px">
|
||||
|
|
@ -593,62 +596,47 @@ ${getIconScript(servePath)}
|
|||
setPadding();
|
||||
});
|
||||
actionElement.querySelector("#landscape").addEventListener('change', (e) => {
|
||||
if (e.target.checked) {
|
||||
actionElement.querySelector("#paged").checked = true;
|
||||
}
|
||||
setPadding();
|
||||
});
|
||||
actionElement.querySelector("#paged").addEventListener('change', (e) => {
|
||||
// 不分页模式没有适配横向页面,强制纵向
|
||||
if (!e.target.checked) {
|
||||
const landscapeElement = actionElement.querySelector("#landscape");
|
||||
if (landscapeElement.checked) {
|
||||
landscapeElement.checked = false;
|
||||
setPadding();
|
||||
}
|
||||
}
|
||||
});
|
||||
actionElement.querySelector('.b3-button--cancel').addEventListener('click', () => {
|
||||
const {ipcRenderer} = require("electron");
|
||||
ipcRenderer.send("${Constants.SIYUAN_CMD}", "destroy")
|
||||
});
|
||||
const buildExportConfig = (unPagedPageSize) => {
|
||||
const pageSize = actionElement.querySelector("#pageSize").value;
|
||||
// https://www.electronjs.org/docs/latest/api/web-contents#contentsprinttopdfoptions
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
|
||||
return {
|
||||
title: "${window.siyuan.languages.export} PDF",
|
||||
pdfOptions: {
|
||||
printBackground: true,
|
||||
landscape: actionElement.querySelector("#landscape").checked,
|
||||
marginType: actionElement.querySelector("#marginsType").value,
|
||||
margins: {
|
||||
top: parseFloat(document.querySelector("#marginsTop").value) || 0,
|
||||
bottom: parseFloat(document.querySelector("#marginsBottom").value) || 0,
|
||||
left: parseFloat(document.querySelector("#marginsLeft").value) || 0,
|
||||
right: parseFloat(document.querySelector("#marginsRight").value) || 0,
|
||||
},
|
||||
scale: parseFloat(actionElement.querySelector("#scale").value),
|
||||
pageSize: unPagedPageSize || pageSize,
|
||||
},
|
||||
pageSize,
|
||||
keepFold: keepFoldElement.checked,
|
||||
mergeSubdocs: mergeSubdocsElement.checked,
|
||||
watermark: watermarkElement.checked,
|
||||
removeAssets: actionElement.querySelector("#removeAssets").checked,
|
||||
paged: !unPagedPageSize,
|
||||
rootId: "${id}",
|
||||
rootTitle: response.data.name,
|
||||
parentWindowId: ${currentWindowId},
|
||||
};
|
||||
};
|
||||
actionElement.querySelector('.b3-button--text').addEventListener('click', () => {
|
||||
const {ipcRenderer} = require("electron");
|
||||
const pageSizeValue = actionElement.querySelector("#pageSize").value;
|
||||
const isPaged = actionElement.querySelector("#paged").checked;
|
||||
|
||||
const buildExportConfig = (pageSize) => {
|
||||
// https://www.electronjs.org/docs/latest/api/web-contents#contentsprinttopdfoptions
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
|
||||
return {
|
||||
title: "${window.siyuan.languages.export} PDF",
|
||||
pdfOptions: {
|
||||
printBackground: true,
|
||||
landscape: actionElement.querySelector("#landscape").checked,
|
||||
marginType: actionElement.querySelector("#marginsType").value,
|
||||
margins: {
|
||||
top: parseFloat(document.querySelector("#marginsTop").value) || 0,
|
||||
bottom: parseFloat(document.querySelector("#marginsBottom").value) || 0,
|
||||
left: parseFloat(document.querySelector("#marginsLeft").value) || 0,
|
||||
right: parseFloat(document.querySelector("#marginsRight").value) || 0,
|
||||
},
|
||||
scale: parseFloat(actionElement.querySelector("#scale").value),
|
||||
pageSize: pageSize,
|
||||
},
|
||||
keepFold: keepFoldElement.checked,
|
||||
mergeSubdocs: mergeSubdocsElement.checked,
|
||||
watermark: watermarkElement.checked,
|
||||
removeAssets: actionElement.querySelector("#removeAssets").checked,
|
||||
paged: isPaged,
|
||||
pageSizeValue: pageSizeValue,
|
||||
rootId: "${id}",
|
||||
rootTitle: response.data.name,
|
||||
parentWindowId: ${currentWindowId},
|
||||
};
|
||||
};
|
||||
|
||||
if (!isPaged) {
|
||||
const getPageSizeDimensions = (pageSizeName) => {
|
||||
const getPageSizeDimensions = () => {
|
||||
// https://github.com/electron/electron/blob/3df3a6a736b93e0d69fa3b0c403b33f201287780/lib/browser/api/web-contents.ts#L89-L101
|
||||
const pageSizes = {
|
||||
"A3": { width: 11.7, height: 16.54 },
|
||||
|
|
@ -658,26 +646,18 @@ ${getIconScript(servePath)}
|
|||
"Letter": { width: 8.5, height: 11 },
|
||||
"Tabloid": { width: 11, height: 17 },
|
||||
};
|
||||
return pageSizes[pageSizeName] || pageSizes["A4"];
|
||||
return pageSizes[actionElement.querySelector("#pageSize").value];
|
||||
};
|
||||
|
||||
const previewContent = previewElement.querySelector(".protyle-wysiwyg") || previewElement;
|
||||
const contentHeight = previewContent.scrollHeight || previewContent.offsetHeight;
|
||||
// 英寸 = 像素 / PPI,但不知道 PPI 怎么得到,直接使用经典 96 DPI,实际导出之后页面末尾会有空白
|
||||
const heightInches = contentHeight / 96;
|
||||
// 获取页面大小对应的宽度
|
||||
const selectedPageSize = getPageSizeDimensions(pageSizeValue);
|
||||
// 高度根据内容动态计算
|
||||
const marginTop = parseFloat(document.querySelector("#marginsTop").value) || 0;
|
||||
const marginBottom = parseFloat(document.querySelector("#marginsBottom").value) || 0;
|
||||
const totalHeightInches = heightInches + marginTop + marginBottom;
|
||||
const dynamicPageSize = {
|
||||
width: selectedPageSize.width,
|
||||
height: totalHeightInches
|
||||
};
|
||||
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", buildExportConfig(dynamicPageSize));
|
||||
const previewHeight = previewElement.scrollHeight / 96 - (parseFloat(document.querySelector("#marginsTop").value) || 0) - (parseFloat(document.querySelector("#marginsBottom").value) || 0);
|
||||
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", buildExportConfig(actionElement.querySelector("#landscape").checked ? {
|
||||
height: getPageSizeDimensions().height,
|
||||
width: previewHeight,
|
||||
} : {
|
||||
width: getPageSizeDimensions().width,
|
||||
height: previewHeight,
|
||||
}));
|
||||
} else {
|
||||
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", buildExportConfig(pageSizeValue));
|
||||
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", buildExportConfig());
|
||||
}
|
||||
previewElement.classList.add("exporting");
|
||||
previewElement.style.zoom = "";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue