【解决】jsPDF 之长图片生成 PDF
//当内容未超过 pdf 一页显示的范围,无需分页
if(leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth,imgHeight);
} else {
while(leftHeight > 0) {
//arg3-->距离左边距;arg4-->距离上边距;arg5-->宽度;ar 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 g6-->高度
pdf.addImage(pageData, 'JPEG', 0, position,imgWidth, imgHeight)
leftHeight -= pageHeight;
position -= 841.89;
//避免添加空白页
if(leftHeight > 0) {
//注②
pdf.addPage();
}
}
}
pdf.save('name_hos.pdf');
}
});
})
注①new jsPDF(orientation, unit, format) → {jsPDF}
| Name | Description |
| --- | --- |
| orientation | One of “portrait” or “landscape” (or shortcuts “p” (Default), “l”) |
| unit | Measurement unit to be used when coordinates are specified. One of “pt” (points), “mm” (Default), “cm”, “in” |
| format | One of ‘pageFormats’ as shown below, default: a4 |
.
注②addPage() → {jsPDF}
/**
Adds (and transfers the focus to) new page to the PDF document.
@function
@returns {jsPDF}
@methodOf jsPDF#
@name addPage
*/
API.addPage = function() {
_addPage.apply(this, arguments);
return this;
};
注③各种型号纸张大小
pageFormats = { // Size in pt of various paper formats
'a0': [2383.94, 3370.39],
'a1': [1683.78, 2383.94],
'a2': [1190.55, 1683.78],
'a3': [841.89, 1190.55],
'a4': [595.28, 841.89],
'a5': [419.53, 595.28],
'a6': [297.64, 419.53],
'a7': [209.76, 297.64],
'a8': [147.40, 209.76],
'a9': [104.88, 147.40],
'a10': [73.70, 104.88],
'b0': [2834.65, 4008.19],
'b1': [2004.09, 2834.65],
'b2': [1417.32, 2004.09],
'b3': [1000.63, 1417.32],
'b4': [708.66, 1000.63],
'b5': [498.90, 708.66],
'b6': [354.33, 498.90],
'b7': [249.45, 354.33],
'b8': [175.75, 249.45],
'b9': [124.72, 175.75],
'b10': [87.87, 124.72],
'c0': [2599.37, 3676.54],
'c1': [1836.85, 2599.37],
'c2': [1298.27, 1836.85],
'c3': [918.43, 1298.27],
'c4': [649.13, 918.43],
'c5': [459.21, 649.13],
'c6': [323.15, 459.21],
'c7': [229.61, 323.15],
'c8': [161.57, 229.61],
'c9': [113.39, 161.57],
'c10': [79.37, 113.39],
评论