Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to adape es6 and changeable width and height #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
document.getElementById("btn-html2canvas").onclick = function(){

// 将 id 为 content 的 div 渲染成 canvas
html2canvas(document.getElementById("content"), {

let parent = document.getElementById("content")
let width = parent.offsetWidth;
let height = parent.offsetHeight
html2canvas(document.getElementById("content"))
,then(canvas => {
// 渲染完成时调用,获得 canvas
onrendered: function(canvas) {

// 从 canvas 提取图片数据
var imgData = canvas.toDataURL('image/jpeg');

var doc = new jsPDF("p", "mm", "a4");
// |
// |—————————————————————————————|
// A0 841×1189
// A1 594×841
// A2 420×594
// A3 297×420
// A4 210×297
// A5 148×210
// A6 105×148
// A7 74×105
// A8 52×74
// A9 37×52
// A10 26×37
// |——|———————————————————————————|
// |——|——|
// | |
doc.addImage(imgData, 'JPEG', 0, 0,210,297);
var doc = new jsPDF("p", "mm", [width, height]);

doc.save('content.pdf');
}
});
doc.addImage(imgData, 'JPEG', 0, 0,width, height);

doc.save('content.pdf');

})
}