2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/client/pdf.js
Faris Ansari 32cd6581d1 PDF generation
- /api/method/pdf
- frappe.getPDF
- download using blob url
2018-04-15 00:44:02 +05:30

31 lines
733 B
JavaScript

async function getPDF(doctype, name) {
const headers = {
'Accept': 'application/pdf',
'Content-Type': 'application/json'
}
const res = await fetch('/api/method/pdf', {
method: 'POST',
headers,
body: JSON.stringify({ doctype, name })
});
const blob = await res.blob();
showFile(blob);
}
function showFile(blob, filename='file.pdf') {
const newBlob = new Blob([blob], { type: "application/pdf" })
const data = window.URL.createObjectURL(newBlob);
const link = document.createElement('a');
link.href = data;
link.download = filename;
link.click();
setTimeout(() => window.URL.revokeObjectURL(data), 100);
}
module.exports = {
getPDF
}