简体   繁体   中英

Export pdf from dynamic html

I have dynamic elements in my html loaded with my javascript. (Things like charts by example). I would like to export those charts into pdf. But when I try I don't have them, I only have my static html like the h5

You can see in the example bellow, the canvas part is empty and then loaded using Chart.js.

<div id="stats">                
    <div class="card mb-4">
        <div class="card-body">
            <h5 class="card-title text-uppercase">Nombre adherents / mois</h5>
            <canvas id="myChartInscription"></canvas>
        </div>
    </div>
</div>
<a class="btn btn-primary" id="exportPDF" href="#">Export PDF</a>
<div id="editor"></div>

This is what I'm trying

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<script>

    var doc = new jsPDF();
    var specialElementHandlers = {
        '#editor': function (element, renderer) {
            return true;
        }
    };

    $('#exportPDF').click(function () {
        doc.fromHTML($('#stats').html(), 15, 15, {
            'width': 170,
            'elementHandlers': specialElementHandlers
        });
        doc.save('stats.pdf');
    });

</script>

Thanks for your help.

您必须将画布另存为数据URL,因为jsPdf的“ fromHtml”不支持画布渲染。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM