Last active
May 28, 2020 09:32
-
-
Save huntz20/7cd1f4976b1396daf7a71f823601a350 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pdfMake = require("pdfmake/build/pdfmake.js"); | |
const pdfFonts = require("pdfmake/build/vfs_fonts.js"); | |
pdfMake.vfs = pdfFonts.pdfMake.vfs; | |
import { camelCase } from "./stringUtils"; | |
export const generatePdfFile = (arrData, idCardTemplate, role) => { | |
const data = JSON.parse(JSON.stringify(arrData)); | |
const { | |
content, | |
images, | |
pageMargins, | |
pageOrientation, | |
pageSize | |
} = JSON.parse(JSON.stringify(idCardTemplate)); | |
const pdfContent = data.map(e => { | |
return content.map((el, i) => { | |
const val = Object.assign({}, el); | |
// eslint-disable-next-line no-prototype-builtins | |
if (el.hasOwnProperty("text")) { | |
if (el.text.toLowerCase() === "name") { | |
val.text = e.name; | |
} | |
if (el.text.toLowerCase() === "role") { | |
val.text = role; | |
} | |
} | |
// eslint-disable-next-line no-prototype-builtins | |
if (el.hasOwnProperty("image")) { | |
console.log("Masuk image"); | |
if (el.image === "defaultImage" && e.logo_url) { | |
// toDataUrl(e.logo_url).then(base64 => { | |
// const camelizeName = camelCase(`${e.name}Image`); | |
// console.log("Terima"); | |
// | |
// images[camelizeName] = base64; | |
// val.image = camelizeName; | |
// }); | |
fetch(e.logo_url) | |
.then(res => res.blob()) | |
.then(blob => { | |
const reader = new FileReader(); | |
reader.readAsDataURL(blob); | |
return new Promise(resolve => { | |
reader.onloadend = () => { | |
resolve(reader.result); | |
}; | |
}); | |
}) | |
.then(base64 => { | |
const camelizeName = camelCase(`${e.name}Image`); | |
console.log("Terima"); | |
images[camelizeName] = base64; | |
val.image = camelizeName; | |
}); | |
} | |
} | |
if (i > 0 && i % content.length === 0 && i !== content.length - 1) { | |
el.pageBreak = "after"; | |
} | |
return val; | |
}); | |
}); | |
const docDefinition = { | |
images, | |
pageMargins, | |
pageOrientation, | |
pageSize, | |
content: pdfContent.flat() | |
}; | |
console.log(docDefinition); | |
pdfMake.createPdf(docDefinition).open(); | |
}; | |
// const toDataUrl = async url => { | |
// return new Promise((resolve, reject) => { | |
// const xhr = new XMLHttpRequest(); | |
// xhr.onload = function() { | |
// const reader = new FileReader(); | |
// reader.onloadend = function() { | |
// resolve(reader.result); | |
// }; | |
// reader.onerror = reject; | |
// reader.readAsDataURL(xhr.response); | |
// }; | |
// console.log("mulai"); | |
// xhr.open("GET", url); | |
// xhr.responseType = "blob"; | |
// xhr.send(); | |
// console.log("kirim"); | |
// }); | |
// }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment