diff --git a/src/electron/menu.ts b/src/electron/menu.ts index a7e028f32..cfba5b7ef 100644 --- a/src/electron/menu.ts +++ b/src/electron/menu.ts @@ -11,6 +11,7 @@ import * as FileExtraUtils from 'fs-extra'; import { Page } from '../store/page/page'; import { PageElement } from '../store/page/page-element'; import * as PathUtils from 'path'; +import { PdfExporter } from '../export/pdf-exporter'; import { PngExporter } from '../export/png-exporter'; import * as ProcessUtils from 'process'; import { Store } from '../store/store'; @@ -120,6 +121,23 @@ export function createMenu(): void { { label: '&Export', submenu: [ + { + label: 'Export page as PDF', + enabled: !isSplashscreen, + click: () => { + const pageFileName = getPageFileName(); + querySaveFilePath( + 'Export PDF as', + 'PDF Document', + pageFileName, + 'pdf', + (path: string) => { + const webview = document.getElementById('preview') as WebviewTag; + PdfExporter.exportToPdf(path, webview); + } + ); + } + }, { label: 'Export page as PNG', enabled: !isSplashscreen, diff --git a/src/export/pdf-exporter.ts b/src/export/pdf-exporter.ts new file mode 100644 index 000000000..4bfbf6bbc --- /dev/null +++ b/src/export/pdf-exporter.ts @@ -0,0 +1,19 @@ +import { WebviewTag } from 'electron'; +import * as FileExtraUtils from 'fs-extra'; + +export class PdfExporter { + public static exportToPdf(path: string, webview: WebviewTag): void { + webview.printToPDF( + { + marginsType: 1, + pageSize: 'A4', + printBackground: true, + printSelectionOnly: false, + landscape: false + }, + (error, pdfBuffer) => { + FileExtraUtils.writeFileSync(path, pdfBuffer); + } + ); + } +}