1

I am trying to export the webpage into pdf file when user click the button, but the below code is not working for me . can any one please help me where I went wrong. I am using jsPDF to export the web page into pdf.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>html2canvas example</title>
   <script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script>
    <script type="text/javascript" src="js/jspdf.js"></script>
    <script type="text/javascript" src="libs/Deflate/adler32cs.js"></script>
    <script type="text/javascript" src="libs/FileSaver.js/FileSaver.js"></script>
    <script type="text/javascript" src="libs/Blob.js/BlobBuilder.js"></script>
    <script type="text/javascript" src="jspdf.plugin.addimage.js"></script>
    <script type="text/javascript" src="jspdf.plugin.standard_fonts_metrics.js"></script>
    <script type="text/javascript" src="jspdf.plugin.split_text_to_size.js"></script>
    <script type="text/javascript" src="jspdf.plugin.from_html.js"></script>
    <script type="text/javascript" src="js/basic.js"></script>
 <script type="text/javascript">
 $(function () 
{
        var doc = new jsPDF();
        var specialElementHandlers = {
            '#editor': function (element, renderer) {
                return true;
            }
        };
        $('#cmd').click(function () {
            doc.fromHTML($('#content').html(), 15, 15, {
                'width': 170,
                    'elementHandlers': specialElementHandlers
            });
            doc.save('sample-file.pdf');
        });
    });
 </script>
</head>
<body id="target" >
     <div id="content">
         <h3>Hello, this is a H3 tag</h3>
        <p>a pararaph</p>
     </div>
  <div id="editor"></div>
  <button id="cmd">generate PDF</button>
</body>
</html>
2

2 Answers 2

4

There is an error in the script. Please try this code

<script>
$(document).ready(function (){
                            $('#cmd').click(function () {
                            var specialElementHandlers = 
                            function (element,renderer) {
                            return true;
                            }
                            var doc = new jsPDF();
                            doc.fromHTML($('#content').html(), 15, 15, {
                            'width': 170,
                            'elementHandlers': specialElementHandlers
                            });
                            doc.output('datauri'); 
                        });
                        });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Not allowed to navigate top frame to data URL
1

The fromHTML function was replaced by html and you have to add html2canvas manually. Have a look at this link:

https://stackoverflow.com/a/78587984/14938811

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.