Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat: add pdf export
Browse files Browse the repository at this point in the history
  • Loading branch information
TheReincarnator authored and Jumace committed Apr 19, 2018
1 parent 2b07559 commit a72aa1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/electron/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/export/pdf-exporter.ts
Original file line number Diff line number Diff line change
@@ -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);
}
);
}
}

0 comments on commit a72aa1d

Please sign in to comment.