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",
|
"nodemon": "^1.14.7",
|
||||||
"nunjucks": "^3.1.0",
|
"nunjucks": "^3.1.0",
|
||||||
"octicons": "^7.2.0",
|
"octicons": "^7.2.0",
|
||||||
|
"os": "^0.1.1",
|
||||||
"passport": "^0.4.0",
|
"passport": "^0.4.0",
|
||||||
"passport-jwt": "^4.0.0",
|
"passport-jwt": "^4.0.0",
|
||||||
"popper.js": "^1.12.9",
|
"popper.js": "^1.12.9",
|
||||||
|
@ -39,83 +39,31 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getPDF() {
|
getPDF() {
|
||||||
// const win = BrowserWindow.fromWebContents()
|
// Open a hidden window
|
||||||
const win = remote.getCurrentWindow();
|
let printWindow = new remote.BrowserWindow(
|
||||||
const filePath = '/Users/prateekshasingh/Desktop/test';
|
// { show: false }
|
||||||
const extension = '.pdf';
|
);
|
||||||
const data = this.printTemplate;
|
|
||||||
const type = 'pdf';
|
|
||||||
|
|
||||||
let mindow = new remote.BrowserWindow({
|
printWindow.loadURL(
|
||||||
width: 800,
|
"data:text/html;charset=utf-8," + encodeURI(this.printTemplate)
|
||||||
height: 600,
|
);
|
||||||
center: true,
|
|
||||||
resizable: true,
|
|
||||||
frame: true,
|
|
||||||
transparent: false,
|
|
||||||
});
|
|
||||||
mindow.setMenu(null);
|
|
||||||
|
|
||||||
// create BrowserWindow with dynamic HTML content
|
printWindow.on("closed", () => {
|
||||||
var html = [
|
printWindow = null;
|
||||||
"",
|
|
||||||
"<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) => {
|
// const pdfPath = path.join(os.tmpdir(), 'print.pdf')
|
||||||
if (!pathname) {
|
const pdfPath = '/Users/prateekshasingh/Desktop/print.pdf';
|
||||||
const errMsg = '[ERROR] Cannot save file without path.'
|
|
||||||
return Promise.reject(errMsg)
|
|
||||||
}
|
|
||||||
pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
|
|
||||||
|
|
||||||
console.log(content);
|
// Use default printing options
|
||||||
return fse.outputFile(pathname, content, 'utf-8')
|
printWindow.webContents.printToPDF({}, (error, data) => {
|
||||||
}
|
if (error) throw error;
|
||||||
|
// printWindow.close();
|
||||||
win.webContents.printToPDF({ printBackground: false }, (err, data) => {
|
fs.writeFile(pdfPath, data, (error) => {
|
||||||
if (err) log(err)
|
if (error) throw error;
|
||||||
else {
|
shell.openExternal(`file://${pdfPath}`);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,6 +82,4 @@ export default {
|
|||||||
box-shadow: 0rem 0rem 0.5rem rgba(0,0,0,0.2);
|
box-shadow: 0rem 0rem 0.5rem rgba(0,0,0,0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
.print-view {}
|
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
12
yarn.lock
12
yarn.lock
@ -1451,14 +1451,6 @@ fs-extra@^5.0.0:
|
|||||||
jsonfile "^4.0.0"
|
jsonfile "^4.0.0"
|
||||||
universalify "^0.1.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:
|
fs-minipass@^1.2.5:
|
||||||
version "1.2.5"
|
version "1.2.5"
|
||||||
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d"
|
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"
|
version "1.0.2"
|
||||||
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
|
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:
|
osenv@0, osenv@^0.1.4:
|
||||||
version "0.1.4"
|
version "0.1.4"
|
||||||
resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.4.tgz#42fe6d5953df06c8064be6f176c3d05aaaa34644"
|
resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.4.tgz#42fe6d5953df06c8064be6f176c3d05aaaa34644"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user