mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-11 20:08:37 +00:00
Co-authored-by: himself65 <himself65@outlook.com> Co-authored-by: Peng Xiao <pengxiao@outlook.com>
62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { BrowserWindow, dialog, shell } from 'electron';
|
|
import fs from 'fs-extra';
|
|
|
|
import { logger } from '../logger';
|
|
import type { ErrorMessage } from './utils';
|
|
import { getFakedResult } from './utils';
|
|
|
|
export interface SavePDFFileResult {
|
|
filePath?: string;
|
|
canceled?: boolean;
|
|
error?: ErrorMessage;
|
|
}
|
|
|
|
/**
|
|
* This function is called when the user clicks the "Export to PDF" button in the electron.
|
|
*
|
|
* It will just copy the file to the given path
|
|
*/
|
|
export async function savePDFFileAs(
|
|
pageTitle: string
|
|
): Promise<SavePDFFileResult> {
|
|
try {
|
|
const ret =
|
|
getFakedResult() ??
|
|
(await dialog.showSaveDialog({
|
|
properties: ['showOverwriteConfirmation'],
|
|
title: 'Save PDF',
|
|
showsTagField: false,
|
|
buttonLabel: 'Save',
|
|
defaultPath: `${pageTitle}.pdf`,
|
|
message: 'Save Page as a PDF file',
|
|
}));
|
|
const filePath = ret.filePath;
|
|
if (ret.canceled || !filePath) {
|
|
return {
|
|
canceled: true,
|
|
};
|
|
}
|
|
|
|
await BrowserWindow.getFocusedWindow()
|
|
?.webContents.printToPDF({
|
|
pageSize: 'A4',
|
|
printBackground: true,
|
|
landscape: false,
|
|
})
|
|
.then(data => {
|
|
fs.writeFile(filePath, data, error => {
|
|
if (error) throw error;
|
|
logger.log(`Wrote PDF successfully to ${filePath}`);
|
|
});
|
|
});
|
|
|
|
shell.openPath(filePath);
|
|
return { filePath };
|
|
} catch (err) {
|
|
logger.error('savePDFFileAs', err);
|
|
return {
|
|
error: 'UNKNOWN_ERROR',
|
|
};
|
|
}
|
|
}
|