scottgalPastes

html2pdf

Dec 11th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function replaceGraphs() {
  2.         var cDoc = $("#PDFInterstitial")[0].contentDocument;
  3.         var graphs = cDoc.getElementsByTagName('canvas');
  4.         graphs = [].slice.call(graphs);
  5.         CanvasPngCompression.replaceToDataURL();
  6.         for (var i = 0; i < graphs.length; i++) {
  7.        
  8.             var image = cDoc.createElement('img');
  9.             var canvas = graphs[i];
  10.  
  11.  
  12.             var base64img = canvas.toDataURL("image/png", 0.7);
  13.             image.src = base64img;
  14.             image.style.height = '750px';
  15.             image.style.width = '750px';
  16.             graphs[i].parentElement.replaceChild(image, graphs[i]);
  17.  
  18.  
  19.  
  20.             var sizeInBytes = 4 * Math.ceil((base64img.length / 3)) * 0.5624896334383812;
  21.             var sizeInKb = sizeInBytes / 1000;
  22.             console.log("Image Size in Kb: " + sizeInKb);
  23.  
  24.         }
  25.         CanvasPngCompression.revertToDataURL();
  26.     }
  27.     var createPDFFromHtml = function (evt) {
  28.         var htmlButton = $(evt.target).closest("button");
  29.         var url = htmlButton.data("href");
  30.         var scale = 3;
  31.         var iframe = $("#PDFInterstitial");
  32.         var contents = iframe.contents();
  33.         var frame = iframe[0];
  34.         var contentWindow = frame.contentWindow;
  35.         var pixelRatio = contentWindow.devicePixelRatio;
  36.  
  37.         contentWindow.devicePixelRatio *= scale;
  38.         iframe.off("load").on("load", function () {
  39.             getPDF(iframe);
  40.         });
  41.         iframe.attr("src", url);
  42.  
  43.         var getPDF = function () {
  44.             var reportName = $("#TabContainer").data("name");
  45.             $("#ajaxSpinnerContainer").show();
  46.             global.replaceGraphs();
  47.             var opt = {
  48.                 margin: 10,
  49.                 filename: reportName + ".pdf",
  50.                 html2canvas: { scale: scale, letterRendering: true, logging: true },
  51.                 jsPDF: { unit: "mm", format: 'A4', compress: true, orientation: 'portrait', putOnlyUsedFonts: true },
  52.                 pagebreak: { mode: 'avoid-all', after: '.tabpage' }
  53.             };
  54.  
  55.             $("#PDFInterstitial").css("display", "block");
  56.             contents = $("#PDFInterstitial").contents();
  57.             var element = contents.find("html").html();
  58.  
  59.             //This will implicitly create the canvas and PDF objects before saving.
  60.             html2pdf().from(element).set(opt).toPdf().get("pdf").then(function (pdf) {
  61.  
  62.                 var totalPages = pdf.internal.getNumberOfPages();
  63.  
  64.                 for (let i = 1; i <= totalPages; i++) {
  65.                     pdf.setPage(i);
  66.                     pdf.setFontSize(10);
  67.                     pdf.setTextColor(150);
  68.                     pdf.text(`Page ${i} of ${totalPages}`,
  69.                         pdf.internal.pageSize.getWidth() - 100,
  70.                         pdf.internal.pageSize.getHeight() - 10);
  71.  
  72.                 }
  73.                 contentWindow.devicePixelRatio = pixelRatio;
  74.                 iframe.empty();
  75.                 $("#ajaxSpinnerContainer").hide();
  76.             }).save();
  77.  
  78.         };
  79.  
  80.  
  81.     };
  82.  
Add Comment
Please, Sign In to add comment