Home > Enterprise >  Why does window.print() and/or google.script.host.close() makes html2pdf save a blank page?
Why does window.print() and/or google.script.host.close() makes html2pdf save a blank page?

Time:09-27

I'm working on this one to save a pdf into a Google Drive folder, but it only works if I comment out window.print() and google.script.host.close(), but these are important steps.

How can I have them in the picture?

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: 1,
    },
    jsPDF: {
      unit: "mm",
      orientation: "landscape",
    },
  };
  //window.print();
  html2pdf()
    .set(opt)
    .from(element)
    .outputPdf()
    .then(function(p) {
      let file = btoa(p);
      google.script.run.savePdf(file, fileName);
      //google.script.host.close();
    });
}

Appreciate your help!

CodePudding user response:

Try it like this:

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: 1,
    },
    jsPDF: {
      unit: "mm",
      orientation: "landscape",
    },
  };
  html2pdf()
    .set(opt)
    .from(element)
    .outputPdf()
    .then(function(p) {
      let file = btoa(p);
      google.script.run
      .withSuccessHandler(() => {
        window.print();
        google.script.host.close();
      })
      .savePdf(file, fileName);
  
    });
  • Related