Yasir Malik
Yasir Malik

Reputation: 191

jsPDF not working in localhost

<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<script type="text/javascript">
    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>
<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>

my code is working fine in jsfiddle but not working in localhost i have mentioned all my code so it will be easy to understand that what i am doing

Upvotes: 0

Views: 1451

Answers (1)

Carlos Delgado
Carlos Delgado

Reputation: 3065

The problem is the order of your html, change your markup to :

<div id="editor"></div>
    <button id="cmd">generate PDF</button>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
    <script type="text/javascript">
        var doc = new jsPDF();
        var specialElementHandlers = {
            '#editor': function (element, renderer) {
                return true;
            }
        };

        $('#cmd').click(function () {
            console.log("Hola");
            doc.fromHTML($('#content').html(), 15, 15, {
                'width': 170,
                    'elementHandlers': specialElementHandlers
            });
            doc.save('sample-file.pdf');
        });
    </script>
    <div id="content">
         <h3>Hello, this is a H3 tag</h3>

        <p>a pararaph</p>
    </div>

The click event is not being triggered because you're adding a listener to something that doesn't exist. The code works, even in file:// protocol. With jsFiddle works because the markup is rendered automatically by the app.

Upvotes: 2

Related Questions