Skip to content

Instantly share code, notes, and snippets.

@meetzaveri
Created April 15, 2019 04:44
Show Gist options
  • Save meetzaveri/e07c31b5e7480625ac258e2ed61355c0 to your computer and use it in GitHub Desktop.
Save meetzaveri/e07c31b5e7480625ac258e2ed61355c0 to your computer and use it in GitHub Desktop.
Config for jsPDF with canvas

Config for jspDF with canvas

   window.html2canvas = html2canvas;

    var content = document.getElementById("content-22");
    console.log("content", content);
    console.log("document.body", document.body);

    html2canvas(content, { background: "red" }).then(canvas => {
      let imgData = canvas.toDataURL("application/pdf", 1.0);
      //   window.open(imgData);
      var pdf = new jsPDF({
        orientation: "landscape",
        unit: "px"
      });
      if (canvas.width > canvas.height) {
        pdf = new jsPDF({
          orientation: "landscape",
          unit: "mm",
          format: [canvas.width, canvas.height]
        });
      } else {
        pdf = new jsPDF({
          orientation: "portrait",
          unit: "mm",
          format: [canvas.width, canvas.height]
        });
      }

      // pdf.addImage(imgData, "JPEG", 0, 0);
      // pdf.save("filemy.pdf");

      pdf.html(content, {
        callback: function(doc) {
          console.log("in callback");
          pdf.addImage(imgData, "JPEG", 0, 0);
          pdf.save("filemy.pdf");
        }
      });
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment