2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

fix: Migrate makePDF method from frappejs

This commit is contained in:
Faris Ansari 2019-12-07 00:13:37 +05:30
parent d4c997317d
commit 688083c28d
2 changed files with 67 additions and 4 deletions

View File

@ -32,7 +32,7 @@ import PageHeader from '@/components/PageHeader';
import SearchBar from '@/components/SearchBar';
import DropdownWithAction from '@/components/DropdownWithAction';
import Button from '@/components/Button';
import { getPDFForElectron } from 'frappejs/server/pdf';
import { makePDF } from '@/utils';
import { remote } from 'electron';
export default {
@ -64,7 +64,7 @@ export default {
async makePDF() {
let destination = await this.getSavePath();
let html = this.$refs.printContainer.innerHTML;
getPDFForElectron(this.doctype, this.name, destination, html);
makePDF(html, destination);
},
getSavePath() {

View File

@ -1,8 +1,11 @@
import frappe from 'frappejs';
import fs from 'fs';
import path from 'path';
import { _ } from 'frappejs/utils';
import { remote, shell } from 'electron';
import router from '@/router';
import Avatar from '@/components/Avatar';
import { _ } from 'frappejs';
import { remote } from 'electron';
export function createNewDatabase() {
return new Promise(resolve => {
@ -147,3 +150,63 @@ export function openQuickEdit({ doctype, name, hideFields, defaults = {} }) {
}
});
}
export function makePDF(html, destination) {
const { BrowserWindow } = remote;
let printWindow = new BrowserWindow({
width: 600,
height: 800,
show: false,
webPreferences: {
nodeIntegration: true
}
});
let url;
if (process.env.NODE_ENV === 'development') {
url = `http://localhost:${process.env.PORT}/static/print.html`;
} else {
let printPath = path.join(
remote.app.getAppPath(),
'dist',
'electron',
'static',
'print.html'
);
url = `file://${printPath}`;
}
printWindow.loadURL(url);
printWindow.on('closed', () => {
printWindow = null;
});
const code = `
let el = document.querySelector('.printTarget');
document.body.innerHTML = \`${html}\`;
`;
printWindow.webContents.executeJavaScript(code);
return new Promise(resolve => {
printWindow.webContents.on('did-finish-load', () => {
printWindow.webContents.printToPDF(
{
marginsType: 1, // no margin
pageSize: 'A4',
printBackground: true
},
(error, data) => {
if (error) throw error;
printWindow.close();
fs.writeFile(destination, data, error => {
if (error) throw error;
resolve(shell.openItem(destination));
});
}
);
});
});
}