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:
parent
d4c997317d
commit
688083c28d
@ -32,7 +32,7 @@ import PageHeader from '@/components/PageHeader';
|
|||||||
import SearchBar from '@/components/SearchBar';
|
import SearchBar from '@/components/SearchBar';
|
||||||
import DropdownWithAction from '@/components/DropdownWithAction';
|
import DropdownWithAction from '@/components/DropdownWithAction';
|
||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
import { getPDFForElectron } from 'frappejs/server/pdf';
|
import { makePDF } from '@/utils';
|
||||||
import { remote } from 'electron';
|
import { remote } from 'electron';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -64,7 +64,7 @@ export default {
|
|||||||
async makePDF() {
|
async makePDF() {
|
||||||
let destination = await this.getSavePath();
|
let destination = await this.getSavePath();
|
||||||
let html = this.$refs.printContainer.innerHTML;
|
let html = this.$refs.printContainer.innerHTML;
|
||||||
getPDFForElectron(this.doctype, this.name, destination, html);
|
makePDF(html, destination);
|
||||||
},
|
},
|
||||||
|
|
||||||
getSavePath() {
|
getSavePath() {
|
||||||
|
67
src/utils.js
67
src/utils.js
@ -1,8 +1,11 @@
|
|||||||
import frappe from 'frappejs';
|
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 router from '@/router';
|
||||||
import Avatar from '@/components/Avatar';
|
import Avatar from '@/components/Avatar';
|
||||||
import { _ } from 'frappejs';
|
|
||||||
import { remote } from 'electron';
|
|
||||||
|
|
||||||
export function createNewDatabase() {
|
export function createNewDatabase() {
|
||||||
return new Promise(resolve => {
|
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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user