What must I do if html2canvas is not defined?

1.7K    Asked by bruce_8968 in Salesforce , Asked on Feb 28, 2023

 I need to call the jsPdf function, however in the console I am getting genPdf is not defined.

Here is my code--


  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />


    <title>Universal Health Fellowship</title>
       <!-- [removed][removed]
        [removed][removed]
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
        [removed][removed]     
        [removed][removed] -->
        [removed][removed]
        [removed][removed]     
        [removed]


        
        
        
           <style>
               @media print {
                  #printPageButton {
                    display: none;
                  }
                }
            </style>
            [removed]
                function genPdf(){
                    html2canvas(document.body,{
                        onrendered: function(canvas){
                            var imgData = canvas.toDataURL('image/png');
                            console.log('Report Image URL: '+imgData);
                            var doc = new jsPDF('p', 'mm', [297, 210]); //210mm wide and 297mm high
                            doc.addImage(imgData, 'PNG', 10, 10);
                            doc.save('sample.pdf');
                        }
                    });
                }
            [removed]
  </head>
  <body style="padding:20px;">
  <button type="button" id="printPageButton" class="btn btn-primary" onclick="genPdf();">Print</button>

If html2canvas is not defined - Removing html2Canvas library and changing code like below will resolve your issue.



   
   
   
    Universal Health Fellowship
   
   
   
   
   
        function genPdf(){
        html2canvas(document.body,{
            onrendered: function(canvas){
                var imgData = canvas.toDataURL('image/png');
                console.log('Report Image URL: '+imgData);
                var doc = new jsPDF('p', 'mm', [297, 210]); //210mm wide and 297mm high
                doc.addImage(imgData, 'PNG', 10, 10);
                doc.save('sample.pdf');
            }
        });
    }
   

Your Answer

Answer (1)

If html2canvas is not defined, follow these steps:


Include the Script: Ensure you have included the html2canvas library in your HTML file:

[removed][removed]

Install via npm: If using npm, install the library and import it:

npm install html2canvas

import html2canvas from 'html2canvas';

Check for Typos: Verify there are no typos in the script tag or import statement.

Network Issues: Make sure there are no network issues blocking the script from loading.

By following these steps, you can resolve the issue of html2canvas not being defined.


6 Months

Interviews

Parent Categories