gpt4 book ai didi

javascript - jsPDF + rasterizeHTML 不起作用?

转载 作者:行者123 更新时间:2023-12-02 14:48:21 31 4
gpt4 key购买 nike

jsPDF 的 addHTML 需要 html2canvas.jsrasterizeHTML.js

我想使用rasterizeHTML.js,但它不起作用。网上找不到任何示例。

使用html2canvas.js:

html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>


<body>
<p id="to-pdf">HTML content...</p>
</body>

js

var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.body,function() {
pdf.save('web.pdf');
});

无法使用rasterizeHTML.js(没有任何反应;没有错误):

html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rasterizehtml/1.2.2/rasterizeHTML.allinone.js"></script>


<body>
<p id="to-pdf">HTML content...</p>
</body>

js

var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.body,function() {
pdf.save('web.pdf');
});

我错过了什么?

最佳答案

我最近遇到了同样的问题,并设法从各种来源拼凑出一个工作示例。

基本 HTML

<div id="canvas" width="100%">
<table class='CSSTableGenerator'>
<tr>
<td>Item</td>
<td>Cost</td>
<td>Description</td>
<td>Available</td>
</tr>
<tr>
<td>Milk</td>
<td>$1.00</td>
<td>Cow puss</td>
<td>Out Of Stock</td>
</tr>
<tr>
<td>Milk</td>
<td>$1.00</td>
<td>white stuff</td>
<td>Out Of Stock</td>
</tr>
</table>
</div>
<button id="download">Create PDF</button>

JS

$(document).ready(function () {
$('#download').click(function () {
html2canvas(document.getElementById('canvas'), {
onrendered: function (canvas) {
var imgData = canvas.toDataURL('image/png');
var doc = new jsPDF('p', 'mm');
doc.addImage(imgData, 'PNG', 10, 10);
doc.save('download.pdf');
}
});
});
});

http://jsfiddle.net/andyg2/mtLqparw/

资源

https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js

https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js

关于javascript - jsPDF + rasterizeHTML 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38907272/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com