Skip to content

doc.save promise being executed before save  #2484

Open
@rsvaldes-amplifire

Description

@rsvaldes-amplifire

I am trying to close a tab after saving a pdf. However, the tab is closing before I get the chance to save the pdf. Is there a way to implement a synchronous callback?

here's the code:

function downloadPDF(fileName) {
    html2canvas(document.body).then(function (canvas) {
        // Export the canvas to its data URI representation
        var imgData = canvas.toDataURL("image/jpg");

        var doc = new jsPDF('l', 'pt', 'letter');
        var pageWidth = doc.internal.pageSize.width;
        var pageHeight = doc.internal.pageSize.height;
        var imgHeight = canvas.height * pageWidth / canvas.width;

        var heightRemaining = imgHeight;
        var position = 0;

        doc.addImage(imgData, 'JPEG', 0, position, pageWidth, imgHeight);
        heightRemaining -= pageHeight;

        while (heightRemaining >= 0) {
            position = heightRemaining - imgHeight;
            doc.addPage();
            doc.addImage(imgData, 'JPEG', 0, position, pageWidth, imgHeight);
            heightRemaining -= pageHeight;
        }

        doc.save(fileName, { returnPromise: true }).then(function(){
           console.log('callback');
       });
    });
}

Update I also tried this (with callback being window.close()) and now the browser isn't even hitting the html2canvas .then() callback function

function downloadPDF(fileName,callback) {
   html2canvas(document.body).then(function (canvas) {
      // Export the canvas to its data URI representation
      var imgData = canvas.toDataURL("image/jpg");
      debugger;
      var doc = new jsPDF('l', 'pt', 'letter');
      var pageWidth = doc.internal.pageSize.width;
      var pageHeight = doc.internal.pageSize.height;
      var imgHeight = canvas.height * pageWidth / canvas.width;

      var heightRemaining = imgHeight;
      var position = 0;

      doc.addImage(imgData, 'JPEG', 0, position, pageWidth, imgHeight);
      heightRemaining -= pageHeight;

      while (heightRemaining >= 0) {
          position = heightRemaining - imgHeight;
          doc.addPage();
          doc.addImage(imgData, 'JPEG', 0, position, pageWidth, imgHeight);
          heightRemaining -= pageHeight;
      }

      doc.save(fileName);
   });
   callback();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions