mirror of
https://github.com/frappe/books.git
synced 2025-01-23 15:18:24 +00:00
[print] generate PDF via hidden window
This commit is contained in:
parent
122c0242ad
commit
5704a2f170
@ -37,6 +37,7 @@
|
||||
"nodemon": "^1.14.7",
|
||||
"nunjucks": "^3.1.0",
|
||||
"octicons": "^7.2.0",
|
||||
"os": "^0.1.1",
|
||||
"passport": "^0.4.0",
|
||||
"passport-jwt": "^4.0.0",
|
||||
"popper.js": "^1.12.9",
|
||||
|
@ -39,83 +39,31 @@ export default {
|
||||
},
|
||||
|
||||
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';
|
||||
// Open a hidden window
|
||||
let printWindow = new remote.BrowserWindow(
|
||||
// { show: false }
|
||||
);
|
||||
|
||||
let mindow = new remote.BrowserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
center: true,
|
||||
resizable: true,
|
||||
frame: true,
|
||||
transparent: false,
|
||||
});
|
||||
mindow.setMenu(null);
|
||||
printWindow.loadURL(
|
||||
"data:text/html;charset=utf-8," + encodeURI(this.printTemplate)
|
||||
);
|
||||
|
||||
// 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;
|
||||
printWindow.on("closed", () => {
|
||||
printWindow = 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}`
|
||||
// const pdfPath = path.join(os.tmpdir(), 'print.pdf')
|
||||
const pdfPath = '/Users/prateekshasingh/Desktop/print.pdf';
|
||||
|
||||
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)
|
||||
})
|
||||
// Use default printing options
|
||||
printWindow.webContents.printToPDF({}, (error, data) => {
|
||||
if (error) throw error;
|
||||
// printWindow.close();
|
||||
fs.writeFile(pdfPath, data, (error) => {
|
||||
if (error) throw error;
|
||||
shell.openExternal(`file://${pdfPath}`);
|
||||
})
|
||||
})
|
||||
|
||||
// await getPDFClient(this.doctype, this.name);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -134,6 +82,4 @@ export default {
|
||||
box-shadow: 0rem 0rem 0.5rem rgba(0,0,0,0.2);
|
||||
}
|
||||
|
||||
.print-view {}
|
||||
|
||||
</style>
|
||||
|
12
yarn.lock
12
yarn.lock
@ -1451,14 +1451,6 @@ 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"
|
||||
@ -2853,6 +2845,10 @@ os-tmpdir@^1.0.0, os-tmpdir@~1.0.2:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
|
||||
|
||||
os@^0.1.1:
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/os/-/os-0.1.1.tgz#208845e89e193ad4d971474b93947736a56d13f3"
|
||||
|
||||
osenv@0, osenv@^0.1.4:
|
||||
version "0.1.4"
|
||||
resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.4.tgz#42fe6d5953df06c8064be6f176c3d05aaaa34644"
|
||||
|
Loading…
x
Reference in New Issue
Block a user