在jspdf.debug.js中更改默认pdf页面宽度和字体大小的位置

Where to change default pdf page width and font size in jspdf.debug.js?

本文关键字:字体 位置 js debug jspdf pdf 默认      更新时间:2023-09-26

我需要更改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]);

构造函数的第三个参数可以采用维度数组。然而,它们并不对应宽度和高度,而是长边和短边(或翻转)。

第一个参数(landscapeportrait)决定了宽度和高度。

在GitHub上的源代码中,您可以看到支持的单位(与pt的相对比例),还可以看到默认的页面格式(以pt为单位)。

从文档页面

要设置页面类型,请在构造函数中传递值

jsPDF(orientation, unit, format)创建新的jsPDF文档对象

实例参数:

方位;肖像;或";景观;(或快捷键"p"(默认);l〃)

单位指定坐标时使用的测量单位。";pt";(点),";mm";(默认),";cm"在";

格式"a3"、"a4"(默认)、"5"、"letter"、"legal"之一

设置字体大小

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.