Got a curious situation where if I run google.script.run.savePdf(file, fileName)
within .then()
, it just doesn't get run when I check the logs in the server-side.
If I run it after html2pdf....then()
, the file gets saved, but it's empty. So, I thought I'd store it in a variable and pass it to the server side after, but I get an error, as the object returned seems to be illegal.
function savefile() {
var element = document.getElementById("pgBody");
var poNumber = document.getElementById('fabricPo').value;
var supplier = document.getElementById('selectSupplier').value;
let fileName = supplier + " - " + poNumber + ".pdf";
var opt = {
margin: [2, 5, 2, 5], //top, left, bottom, right
filename: fileName,
image: {
type: "jpeg",
quality: 0.98,
},
html2canvas: {
scale: 2,
},
jsPDF: {
unit: "mm",
orientation: "landscape",
},
};
let file = ''
html2pdf()
.set(opt)
.from(element)
.outputPdf()
.then(function (p) {
file += btoa(p);
});
google.script.run.savePdf(file, fileName);
window.print();
google.script.host.close();
}
Try to use the arrow function.
html2pdf()
.set(opt)
.from(element)
.outputPdf()
.then((p) => {
file += btoa(p);
});
google.script.run.savePdf(file, fileName);
window.print();
google.script.host.close();
Or use await
syntax.
async function savefile() {
var element = document.getElementById("pgBody");
var poNumber = document.getElementById('fabricPo').value;
var supplier = document.getElementById('selectSupplier').value;
let fileName = supplier + " - " + poNumber + ".pdf";
var opt = {
margin: [2, 5, 2, 5], //top, left, bottom, right
filename: fileName,
image: {
type: "jpeg",
quality: 0.98,
},
html2canvas: {
scale: 2,
},
jsPDF: {
unit: "mm",
orientation: "landscape",
},
};
let file = ''
const p = await html2pdf()
.set(opt)
.from(element)
.outputPdf()
file += btoa(p);
google.script.run.savePdf(file, fileName);
window.print();
google.script.host.close();
}