mirror of
https://github.com/frappe/books.git
synced 2025-01-03 07:12:21 +00:00
feat: update save pdf to use custom size
This commit is contained in:
parent
9011ae04fa
commit
1016e39c1b
@ -42,8 +42,8 @@ export default function registerIpcMainActionListeners(main: Main) {
|
||||
|
||||
ipcMain.handle(
|
||||
IPC_ACTIONS.SAVE_HTML_AS_PDF,
|
||||
async (event, html, savePath) => {
|
||||
return await saveHtmlAsPdf(html, savePath, app);
|
||||
async (event, html, savePath, width: number, height: number) => {
|
||||
return await saveHtmlAsPdf(html, savePath, app, width, height);
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -2,18 +2,12 @@ import { App, BrowserWindow } from 'electron';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const PRINT_OPTIONS = {
|
||||
marginsType: 1, // no margin
|
||||
pageSize: 'A4',
|
||||
printBackground: true,
|
||||
printBackgrounds: true,
|
||||
printSelectionOnly: false,
|
||||
};
|
||||
|
||||
export async function saveHtmlAsPdf(
|
||||
html: string,
|
||||
savePath: string,
|
||||
app: App
|
||||
app: App,
|
||||
width: number, // centimeters
|
||||
height: number // centimeters
|
||||
): Promise<boolean> {
|
||||
/**
|
||||
* Store received html as a file in a tempdir,
|
||||
@ -24,7 +18,17 @@ export async function saveHtmlAsPdf(
|
||||
const htmlPath = path.join(tempRoot, `${filename}.html`);
|
||||
await fs.writeFile(htmlPath, html, { encoding: 'utf-8' });
|
||||
|
||||
const printWindow = getInitializedPrintWindow(htmlPath);
|
||||
const printWindow = getInitializedPrintWindow(htmlPath, width, height);
|
||||
const printOptions = {
|
||||
marginsType: 1, // no margin
|
||||
pageSize: {
|
||||
height: height * 10_000, // micrometers
|
||||
width: width * 10_000, // micrometers
|
||||
},
|
||||
printBackground: true,
|
||||
printBackgrounds: true,
|
||||
printSelectionOnly: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* After the printWindow content is ready, save as pdf and
|
||||
@ -32,7 +36,7 @@ export async function saveHtmlAsPdf(
|
||||
*/
|
||||
return await new Promise((resolve) => {
|
||||
printWindow.webContents.once('did-finish-load', () => {
|
||||
printWindow.webContents.printToPDF(PRINT_OPTIONS).then((data) => {
|
||||
printWindow.webContents.printToPDF(printOptions).then((data) => {
|
||||
fs.writeFile(savePath, data).then(() => {
|
||||
printWindow.close();
|
||||
fs.unlink(htmlPath).then(() => {
|
||||
@ -44,10 +48,14 @@ export async function saveHtmlAsPdf(
|
||||
});
|
||||
}
|
||||
|
||||
function getInitializedPrintWindow(printFilePath: string) {
|
||||
function getInitializedPrintWindow(
|
||||
printFilePath: string,
|
||||
width: number,
|
||||
height: number
|
||||
) {
|
||||
const printWindow = new BrowserWindow({
|
||||
width: 595,
|
||||
height: 842,
|
||||
width: Math.floor(width * 28.333333), // pixels
|
||||
height: Math.floor(height * 28.333333), // pixels
|
||||
show: false,
|
||||
});
|
||||
|
||||
|
@ -148,7 +148,12 @@ export default defineComponent({
|
||||
return;
|
||||
}
|
||||
|
||||
await getPathAndMakePDF(name ?? this.t`Entry`, innerHTML);
|
||||
await getPathAndMakePDF(
|
||||
name ?? this.t`Entry`,
|
||||
innerHTML,
|
||||
this.width,
|
||||
this.height
|
||||
);
|
||||
},
|
||||
},
|
||||
computed: {
|
||||
|
@ -69,15 +69,22 @@ export async function saveData(data: string, savePath: string) {
|
||||
await ipcRenderer.invoke(IPC_ACTIONS.SAVE_DATA, data, savePath);
|
||||
}
|
||||
|
||||
export async function showItemInFolder(filePath: string) {
|
||||
await ipcRenderer.send(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, filePath);
|
||||
export function showItemInFolder(filePath: string) {
|
||||
ipcRenderer.send(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, filePath);
|
||||
}
|
||||
|
||||
export async function makePDF(html: string, savePath: string) {
|
||||
export async function makePDF(
|
||||
html: string,
|
||||
savePath: string,
|
||||
width: number,
|
||||
height: number
|
||||
) {
|
||||
const success = await ipcRenderer.invoke(
|
||||
IPC_ACTIONS.SAVE_HTML_AS_PDF,
|
||||
html,
|
||||
savePath
|
||||
savePath,
|
||||
width,
|
||||
height
|
||||
);
|
||||
|
||||
if (success) {
|
||||
|
@ -213,14 +213,19 @@ async function getPrintTemplateDocValues(doc: Doc, fieldnames?: string[]) {
|
||||
return values;
|
||||
}
|
||||
|
||||
export async function getPathAndMakePDF(name: string, innerHTML: string) {
|
||||
export async function getPathAndMakePDF(
|
||||
name: string,
|
||||
innerHTML: string,
|
||||
width: number,
|
||||
height: number
|
||||
) {
|
||||
const { filePath } = await getSavePath(name, 'pdf');
|
||||
if (!filePath) {
|
||||
return;
|
||||
}
|
||||
|
||||
const html = constructPrintDocument(innerHTML);
|
||||
await makePDF(html, filePath);
|
||||
await makePDF(html, filePath, width, height);
|
||||
}
|
||||
|
||||
function constructPrintDocument(innerHTML: string) {
|
||||
|
Loading…
Reference in New Issue
Block a user