I am trying to generate a PDF from an html element (a div) with some content:
<div id = "toPDF">
<table >
<thead>
<tr>
<th>Destination</th>
<th>Start Date</th>
<th>End Date</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dest1</td>
<td>Date1</td>
<td>Date2</td>
<td>Comment1</td>
</tr>
</tbody>
</table>
</div>
.
.
.
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/" crossorigin="anonymous"></script>
<script src="app.js"></script>
I am using the following javascript:
window.html2canvas = html2canvas;
var elem = document.getElementById('toPDF');
pdf.html(elem,
{
x: 20,
y: 140,
callback:
function (pdf) {
pdf.text(80, 230, "Test Text");
var iframe = document.createElement('iframe');
iframe.setAttribute('style', 'position:absolute;top:0;right:0;height:100%; width:600px');
document.body.appendChild(iframe);
iframe.src = pdf.output('datauristring');
}
});
The resulting PDF shows the "Test Text", but no trace of the html table. If I remove "Test Text" the output PDF is empty. The browser console shows the following error:
TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap
I have tried different options. I am asking for a working jsPDF.html() example.