OpenLayers – Export PDF Map with jsPDF Using JavaScript

javascriptjqueryopenlayers

I try to use javascript library JSPDF for export my OpenLayers map but it doesn't work. I create my HTML :

<head>
     <script type="text/javascript" src="script/ol_3.7.0/build/ol.js"></script>
     <script type="text/javascript" src="script/proj4js/dist/proj4.js"></script>
     <script type="text/javascript" src="script/jquery/jquery.js"></script>
     <script src="http://parall.ax/parallax/js/jspdf.js"></script>
</head>

<body>
     <div id = "index_map"></div>

     <div id = "editor"></div>
     <button id="cmd">generate PDF</button>
</body>

And my Javascript :

$(document).ready(function () {

    var doc = new jsPDF();

    var specialElementHandlers = {
        '#editor': function (element, renderer) {
            console.log(1);
            return true;
        }
    };

    $('#cmd').click(function () {

        var html=$("'#index_map").html();
        console.log (html);
        doc.fromHTML(html, 200, 200, {
            'width': 500,
                'elementHandlers': specialElementHandlers
        });
        doc.save('test.pdf');

    });
});

And when I click on my button I've the message "doc.fromHTML is not a function"

enter image description here

Best Answer

Use:

doc.fromHTML($('#index_map').get(0), 15, 15, {
    'width': 170, 
    'elementHandlers': specialElementHandlers
});

note the get: doc.fromHTML($('#index_map').get(0)

Related Question