Skip to content

jspdf + html2canvas with many pages - performance issue #2198

@quentinsierocki

Description

@quentinsierocki

JSPDF.debug version : Version 1.4.1 Built on 2018-06-06T07:49:28.721Z
html2canvas : html2canvas 1.0.0-alpha.11 https://html2canvas.hertzen.com

I want to generate PDF using this code :

new Promise(function(resolve, reject) {
  try {
    resolve(Controller.getViewPdf(pagePdf));
  } catch (e) {
    reject(e);
  }
})
  .then(function(finished) {
    requirejs.s.contexts.wealth.require(
      [
        "../../../common/vendor/jspdf/jspdf.debug",
        "../../../common/vendor/html2canvas/html2canvas"
      ],
      function(jsPDF, html2canvas) {
        var pdf = new jsPDF({
          orientation: "p",
          unit: "mm",
          format: "a4",
          compression: true
        });
        var promises = $(".wea_pdf").map(function(index, element) {
          return new Promise(function(resolve, reject) {
            html2canvas(element, { scale: 2, logging: true })
              .then(function(canvas) {
                resolve(canvas.toDataURL());
              })
              .catch(function(error) {
                //voir ce qu'on fait si une page est en rerreur
                reject("erreur PDF page : " + index);
              });
          });
        });
        Promise.all(promises).then(function(dataURLs) {
          for (var ind in dataURLs) {
            pdf.addImage(
              dataURLs[ind],
              "PNG",
              0,
              0,
              210,
              297,
              undefined,
              "FAST"
            );
            pdf.addPage();
          }
          pdf.save("botoum");
          App.MainModule.Main.Controller.hideAreaPdf();
        });
      }
    );
  })
  .catch(function(error) {
    console.log(error);
  });

The PDF is exactly what I see in the navigator but it a long time to generate it and the navigator freeze more than 5 minutes.
What I need : I want the pdf will be generate the faster as possible without freeze of the navigator .
Is any one have an idea to optimize this code ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions