<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
<script src="https://cdn.bootcss.com/jspdf/1.3.4/jspdf.debug.js"></script>
img.onload = function(){
//使用h5畫布重新畫出返回的base64圖片,直接寫進pdf會失真和不完整,估計是格式有問題
let can = $('<canvas width="' + img.naturalWidth+'" height="'+ img.naturalHeight
+'"></canvas>').get(0);
can.getContext("2d").drawImage(img,0,0);
let canvasBase64 = can.toDataURL('image/jpeg',2.0);
var pdf = new jspdf('p', 'pt', 'a4'); //默認是a4紙大小,試了很多種辦法不能調節大小
pdf.addPage([600,420]); //添加新的指定大小的頁面
pdf.deletePage(1); //刪除空白的第一頁,就可以得到指定大小的pdf
//l:橫向, p:縱向
//var doc = new jsPDF('p', 'mm', [290, 210]);
var contentWidth = can.width;
var contentHeight = can.height;
//一頁pdf顯示html頁面生成的canvas高度;
//a4紙的尺寸[595.28,841.89],html頁面生成的canvas在pdf中圖片的寬高
var imgWidth = 595.28;
var imgHeight = 592.28/contentWidth * contentHeight;
//x位置,y位置,寬,高
pdf.addImage(canvasBase64, 'jpeg', 0, 0, 592.28, 592.28/contentWidth * contentHeight);
pdf.save('test.pdf');
};