本文介绍了如何使用jspadf和html2canvas下载pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am trying to create pdf from my web page. I am trying to use Html2Canvas to take image and then save as pdf using Jspdf.
But for some reason my code does not work.
Please look at my code and try to help me out.
我尝试过的:
What I have tried:
<button class="create_pdf" myPrint="#canvas" id="btn">Export To PDF</button>
<script type="text/javascript">
$(document).ready(function () {
$(".create_pdf").on("click", function (e) {
$('body').scrollTop(0);
var selector = $(e.target).attr('myprint');
createPDF(selector);
});
});
var form, a4 = [595.28, 841.89]; // for a4 size paper width and height
function createPDF(selector) {
form = $(selector);
getCanvas().then(function (canvas) {
var cache_width = form.width()
var
img = canvas.toDataURL("image/png"),
doc = new jsPDF({
unit: 'px',
format: 'a4'
});
doc.addImage(img, 'JPEG', 20, 20);
doc.save(TEst.pdf');
form.width(cache_width);
});
}
// create canvas object
function getCanvas() {
form.width((a4[0] * 1.33333) - 80).css('max-width', 'none');
return html2canvas(form, {
imageTimeout: 2000,
removeContainer: true
});
}
</script>
推荐答案
这篇关于如何使用jspadf和html2canvas下载pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!