2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00
books/main/saveHtmlAsPdf.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.0 KiB
TypeScript
Raw Normal View History

import { emitMainProcessError } from 'backend/helpers';
import { App, BrowserWindow } from 'electron';
import fs from 'fs/promises';
import path from 'path';
export async function saveHtmlAsPdf(
html: string,
savePath: string,
app: App,
width: number, // centimeters
height: number // centimeters
): Promise<boolean> {
/**
* Store received html as a file in a tempdir,
* this will be loaded into the print view
*/
const tempRoot = app.getPath('temp');
const filename = path.parse(savePath).name;
const htmlPath = path.join(tempRoot, `${filename}.html`);
await fs.writeFile(htmlPath, html, { encoding: 'utf-8' });
const printWindow = await 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
* then close the window and delete the temp html file.
*/
return await new Promise((resolve) => {
printWindow.webContents.once('did-finish-load', () => {
printWindow.webContents
.printToPDF(printOptions)
.then((data) => {
fs.writeFile(savePath, data)
.then(() => {
printWindow.close();
fs.unlink(htmlPath)
.then(() => {
resolve(true);
})
.catch((err) => emitMainProcessError(err));
})
.catch((err) => emitMainProcessError(err));
})
.catch((err) => emitMainProcessError(err));
});
});
}
async function getInitializedPrintWindow(
printFilePath: string,
width: number,
height: number
) {
const printWindow = new BrowserWindow({
width: Math.floor(width * 28.333333), // pixels
height: Math.floor(height * 28.333333), // pixels
show: false,
});
await printWindow.loadFile(printFilePath);
return printWindow;
}