我需要在 jspdf.debug.js 中更改默认的 pdf 页面宽度和字体大小。
在 jspdf.debug.js 中如何更改默认值?
我需要在 jspdf.debug.js 中更改默认的 pdf 页面宽度和字体大小。
在 jspdf.debug.js 中如何更改默认值?
// Document of 210mm wide and 297mm high
new jsPDF('p', 'mm', [297, 210]);
// Document of 297mm wide and 210mm high
new jsPDF('l', 'mm', [297, 210]);
// Document of 5 inch width and 3 inch high
new jsPDF('l', 'in', [3, 5]);
构造函数的第三个参数可以接受一个维度数组。然而,它们不对应于宽度和高度,而是长边和短边(或者反过来)。
你的第一个参数 (landscape
或 portrait
) 决定了哪个是宽度,哪个是高度。
在 GitHub 上的源代码中,你可以看到支持的单位(相对于pt
的比例),也可以看到默认的页面格式(以pt
为单位的尺寸)。
从文档页面中
为设置页面类型,请将值传递给构造函数
jsPDF(orientation, unit, format)
创建新的jsPDF文档对象。实例参数:
orientation其中之一:"portrait"或"landscape"(或快捷方式"p"(默认),"l")
unit当指定坐标时要使用的测量单位。其中之一:"pt"(点),"mm"(默认),"cm","in"
format其中之一:'a3','a4'(默认),'a5','信纸','法律'
为设置字体大小
setFontSize(size)
设置即将出现的文本元素的字体大小。
参数:
{Number} size 字体大小,以点为单位。
我的情况是要打印横向(横版)的摘要部分 - 所以:
}).then((canvas) => {
const img = canvas.toDataURL('image/jpg');
new jsPDF({
orientation: 'l', // landscape
unit: 'pt', // points, pixels won't work properly
format: [canvas.width, canvas.height] // set needed dimensions for any element
});
pdf.addImage(img, 'JPEG', 0, 0, canvas.width, canvas.height);
pdf.save('your-filename.pdf');
});
对于任何尝试在React中实现此操作的人,有些细微差别。
// Document of 8.5 inch width and 11 inch high
new jsPDF('p', 'in', [612, 792]);
// Document of 8.5 inch width and 11 inch high
new jsPDF({
orientation: 'p',
unit: 'in',
format: [612, 792]
});
当我尝试使用@Aidiakapi的解决方案时,页面非常小。要获取不同尺寸,请将英寸大小乘以72以获得所需的尺寸。例如,我想要8.5,因此8.5 * 72 = 612。 这适用于jspdf@1.5.3。