mirror of
https://github.com/frappe/books.git
synced 2024-12-24 11:55:46 +00:00
32cd6581d1
- /api/method/pdf - frappe.getPDF - download using blob url
31 lines
733 B
JavaScript
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
|
|
}
|