jsPDF html 方法总是失败
jsPDF html method always fails
我正在尝试从 html 元素(div)生成包含以下内容的 PDF:
<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>
我正在使用以下 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');
}
});
生成的 PDF 显示 "Test Text",但没有 html table 的踪迹。如果我删除 "Test Text" 输出 PDF 为空。浏览器控制台显示以下错误:
TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap
我尝试过不同的选择。我要求一个有效的 jsPDF.html() 示例。
问题很可能是您使用的 html2canvas
版本。 html2canvas 0.4.1
是行不通的。它应该是 html2canvas 1.0.0-alpha.11
或更高。但是,当前版本 html2canvas 1.0.0-rc.3
doesn't work either, it also gives me a blank page. At least html2canvas 1.0.0-alpha.12
仍然适用于我。
<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="~/lib/html2canvas/html2canvas.min.js"></script>
<!-- html2canvas 1.0.0-alpha.11 or html2canvas 1.0.0-alpha.12 is needed -->
<script>
function download() {
let pdf = new jsPDF('p', 'pt', 'a4');
pdf.html(document.getElementById('toPDF'), {
callback: function () {
window.open(pdf.output('bloburl'));
}
});
}
</script>
Update: the latest version that works for me is html2canvas v1.0.0-rc.1
我正在尝试从 html 元素(div)生成包含以下内容的 PDF:
<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>
我正在使用以下 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');
}
});
生成的 PDF 显示 "Test Text",但没有 html table 的踪迹。如果我删除 "Test Text" 输出 PDF 为空。浏览器控制台显示以下错误:
TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap
我尝试过不同的选择。我要求一个有效的 jsPDF.html() 示例。
问题很可能是您使用的 html2canvas
版本。 html2canvas 0.4.1
是行不通的。它应该是 html2canvas 1.0.0-alpha.11
或更高。但是,当前版本 html2canvas 1.0.0-rc.3
doesn't work either, it also gives me a blank page. At least html2canvas 1.0.0-alpha.12
仍然适用于我。
<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="~/lib/html2canvas/html2canvas.min.js"></script>
<!-- html2canvas 1.0.0-alpha.11 or html2canvas 1.0.0-alpha.12 is needed -->
<script>
function download() {
let pdf = new jsPDF('p', 'pt', 'a4');
pdf.html(document.getElementById('toPDF'), {
callback: function () {
window.open(pdf.output('bloburl'));
}
});
}
</script>
Update: the latest version that works for me is
html2canvas v1.0.0-rc.1