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

[pdf] init electron flow

This commit is contained in:
Prateeksha Singh 2018-07-16 05:17:14 +05:30
parent 0d4cdb5a74
commit 122c0242ad
4 changed files with 99 additions and 8 deletions

View File

@ -23,6 +23,7 @@
"express": "^4.16.2",
"flatpickr": "^4.3.2",
"frappe-datatable": "^1.1.2",
"fs": "^0.0.1-security",
"jquery": "^3.3.1",
"jwt-simple": "^0.5.1",
"luxon": "^1.0.0",

View File

@ -5,7 +5,6 @@
<div class="button-group">
<f-button secondary @click="$emit('view-form')">{{ _('View Form') }}</f-button>
<f-button primary @click="$emit('pdf')">{{ _('PDF') }}</f-button>
<f-dropdown></f-dropdown>
</div>
</div>

View File

@ -13,9 +13,12 @@
<script>
import { getHTML } from '../../../common/print.js';
import { getPDF as getPDFClient } from '../../../client/pdf.js';
import PrintActions from './PrintActions';
// for PDF in Electron
import { BrowserWindow, remote, ipcMain, ipcRenderer, shell } from 'electron'
import fs from 'fs';
export default {
name: 'PrintView',
props: ['doctype', 'name'],
@ -35,8 +38,84 @@ export default {
this.$router.push(`/edit/${this.doctype}/${this.name}`);
},
async getPDF() {
await getPDFClient(this.doctype, this.name);
getPDF() {
// const win = BrowserWindow.fromWebContents()
const win = remote.getCurrentWindow();
const filePath = '/Users/prateekshasingh/Desktop/test';
const extension = '.pdf';
const data = this.printTemplate;
const type = 'pdf';
let mindow = new remote.BrowserWindow({
width: 800,
height: 600,
center: true,
resizable: true,
frame: true,
transparent: false,
});
mindow.setMenu(null);
// create BrowserWindow with dynamic HTML content
var html = [
"",
"<body>",
"<h1>It works</h1>",
"</body>",
].join("");
mindow.loadURL("data:text/html;charset=utf-8," + encodeURI(this.printTemplate));
mindow.openDevTools();
mindow.on("closed", function() {
mindow = null;
});
const writeFile = (pathname, content, extension) => {
if (!pathname) {
const errMsg = '[ERROR] Cannot save file without path.'
return Promise.reject(errMsg)
}
pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
console.log(content);
return fse.outputFile(pathname, content, 'utf-8')
}
win.webContents.printToPDF({ printBackground: false }, (err, data) => {
if (err) log(err)
else {
writeFile(filePath, data, extension)
.then(() => {
win.webContents.send('AGANI::export-success', { type, filePath })
})
// .catch(log)
}
})
printPDFBtn.addEventListener('click', (event) => {
ipcRenderer.send('print-to-pdf')
})
ipcRenderer.on('wrote-pdf', (event, path) => {
const message = `Wrote PDF to: ${path}`
document.getElementById('pdf-path').innerHTML = message
})
ipcMain.on('print-to-pdf', (event) => {
const pdfPath = path.join(os.tmpdir(), 'print.pdf')
const win = BrowserWindow.fromWebContents(event.sender)
// Use default printing options
win.webContents.printToPDF({}, (error, data) => {
if (error) throw error
fs.writeFile(pdfPath, data, (error) => {
if (error) throw error
shell.openExternal(`file://${pdfPath}`)
event.sender.send('wrote-pdf', pdfPath)
})
})
})
// await getPDFClient(this.doctype, this.name);
}
}
}
@ -46,7 +125,7 @@ export default {
@import "../../styles/variables";
.print-container {
padding: 1rem;
padding: 1rem 5rem;
}
.print-template {

View File

@ -1417,9 +1417,9 @@ forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
frappe-datatable@frappe/datatable:
version "0.0.4"
resolved "https://codeload.github.com/frappe/datatable/tar.gz/4bb400230087fbf97e8587a34916e14f77fa01cd"
frappe-datatable@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/frappe-datatable/-/frappe-datatable-1.1.2.tgz#d28841fa9161ef2329032376fb48758456dbc9cb"
dependencies:
clusterize.js "^0.18.0"
lodash "^4.17.5"
@ -1451,6 +1451,14 @@ fs-extra@^5.0.0:
jsonfile "^4.0.0"
universalify "^0.1.0"
fs-extra@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.1.tgz#8abc128f7946e310135ddc93b98bddb410e7a34b"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
universalify "^0.1.0"
fs-minipass@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d"
@ -1461,6 +1469,10 @@ fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
fs@^0.0.1-security:
version "0.0.1-security"
resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4"
fsevents@^1.0.0:
version "1.1.3"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.1.3.tgz#11f82318f5fe7bb2cd22965a108e9306208216d8"