简体   繁体   English

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

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

I need to change default pdf page width and font size in jspdf.debug.js.我需要在 jspdf.debug.js 中更改默认的 pdf 页面宽度和字体大小

Where and how to change the default values in jspdf.debug.js?在哪里以及如何更改 jspdf.debug.js 中的默认值?

Besides using one of the default formats you can specify any size you want in the unit you specify.除了使用一种默认格式之外,您还可以在您指定的单位中指定您想要的任何大小。

For example:例如:

// 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]);

The 3rd parameter of the constructor can take an array of the dimensions.构造函数的第三个参数可以采用维度数组。 However they do not correspond to width and height, instead they are long side and short side (or flipped around).然而,它们不对应于宽度和高度,而是长边和短边(或翻转)。

Your 1st parameter ( landscape or portrait ) determines what becomes the width and the height.您的第一个参数( landscapeportrait )决定了宽度和高度。

In the sourcecode on GitHub you can see the supported units (relative proportions to pt ) , and you can also see the default page formats (with their sizes in pt ) .在 GitHub 上的源代码中,您可以看到支持的单位(与pt相对比例) ,您还可以看到默认页面格式(其大小以pt

From the documentation page文档页面

To set the page type pass the value in constructor要设置页面类型,请在构造函数中传递值

jsPDF(orientation, unit, format) Creates new jsPDF document object jsPDF(orientation, unit, format)创建新的 jsPDF 文档对象

instance Parameters:实例参数:

orientation One of "portrait" or "landscape" (or shortcuts "p" (Default), "l")方向“纵向”或“横向”之一(或快捷方式“p”(默认),“l”)

unit Measurement unit to be used when coordinates are specified. unit指定坐标时使用的测量单位。 One of "pt" (points), "mm" (Default), "cm", "in" “pt”(点)、“mm”(默认)、“cm”、“in”之一

format One of 'a3', 'a4' (Default),'a5' ,'letter' ,'legal'格式'a3'、'a4'(默认)、'a5'、'letter'、'legal' 之一

To set font size设置字体大小

setFontSize(size)

Sets font size for upcoming text elements.为即将出现的文本元素设置字体大小。

Parameters:参数:

{Number} size Font size in points. {Number} size 以磅为单位的字体大小。

For anyone trying to this in react.对于任何试图对此做出反应的人。 There is a slight difference.有一点不同。

// Document of 8.5 inch width and 11 inch high
new jsPDF('p', 'in', [612, 792]);

or或者

// Document of 8.5 inch width and 11 inch high
new jsPDF({
        orientation: 'p', 
        unit: 'in', 
        format: [612, 792]
});

When i tried the @Aidiakapi solution the pages were tiny.当我尝试@Aidiakapi 解决方案时,页面很小。 For a difference size take size in inches * 72 to get the dimensions you need.对于不同的尺寸,以英寸 * 72 为单位的尺寸以获得您需要的尺寸。 For example, i wanted 8.5 so 8.5 * 72 = 612. This is for jspdf@1.5.3.例如,我想要 8.5 所以 8.5 * 72 = 612。这是用于 jspdf@1.5.3。

My case was to print horizontal (landscape) summary section - so:我的情况是打印水平(横向)摘要部分 - 所以:

}).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');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM