NFE*_*NFE 20 javascript pdf-generation jspdf
我在jspdf中创建表时遇到了一些问题.我找不到任何好的相关文档,但如果有人知道,请分享.
我找到了两个相关的方法:
1) doc.getStringUnitWidth(text, {widths:12,kerning:2});
2) doc.getTextDimensions(text)
什么宽度和字距在第一种方法是什么意思?我应该提供什么?我应该使用哪种方法来获取文本的宽度和高度?
我的目标是解决与重叠单元格内容相关的一些问题,文本和文本溢出相对于页面宽度的包装.
Sim*_*son 21
您可能希望查看AutoTable插件以使用jsPDF生成表.我发现内置的插件难以使用,根本没有记录.
我发现计算宽度的最好方法就是这样做:
var doc = new jsPDF();
var width = doc.getTextWidth('Text');
console.log(width);
Run Code Online (Sandbox Code Playgroud)
如果您使用其他东西,那么getDimensions()
您将需要进一步转换它.以下是关于该方法的官方评论pt
:
如果字体大小设置为1磅,则返回给定字体中字符串的宽度.
换句话说,这是"比例"值.对于1个字体大小的单位,字符串的长度将是那么多.
乘以字体大小得到点的实际宽度然后除以72得到英寸或除以(72/25.6)得到'mm'等.
正如您在上面的示例中所看到的,具有宽度和字距调整的对象是可选的,除非使用非常特定的字体,否则不应指定.
如果你真的需要计算高度getDimensions()
可能是一个选项.然而,它不是像素完美,看着源我怀疑它会适用于任何东西,但pt
.这是一个例子:
var doc = new jsPDF('p', 'pt');
var dim = doc.getTextDimensions('Text');
console.log(dim); // Object {w: 24.149968818897642, h: 19.499975433070865}
Run Code Online (Sandbox Code Playgroud)
我发现这个问题正在寻找类似问题的答案.不幸的是,提供的答案并没有解决我的问题,但我认为我所发现的可能会帮助其他人绊倒这个问题.
如前所述,.getStringUnitWidth(text)将返回字符串的宽度(以pt为单位).我永远无法在文档或实际的.js文件中找到任何关于"字距调整"或"宽度"等选项的解释.我也注意到我自己的用法,这种方法实际上低估了大约10%的宽度,开始加起来非常快.最后,这并没有考虑粗体,斜体或粗体和斜体字体.
Since I required a high level of accuracy, I ended up calculating the width of each character on my own and using this to calculate a string width (by totaling the width of each character in a string).
Below are the font widths at size 1 (in pt) for each character accepted by jsPDF in the 3 default fonts (helvetica, times, courier):
helvetica
char unicode normal bold italic bolditalic
32 0.278 0.278 0.278 0.278
! 33 0.278 0.333 0.278 0.333
" 34 0.355 0.474 0.355 0.474
# 35 0.556 0.556 0.556 0.556
$ 36 0.556 0.556 0.556 0.556
% 37 0.889 0.889 0.889 0.889
& 38 0.667 0.722 0.667 0.722
' 39 0.191 0.238 0.191 0.238
( 40 0.333 0.333 0.333 0.333
) 41 0.333 0.333 0.333 0.333
* 42 0.389 0.389 0.389 0.389
+ 43 0.584 0.584 0.584 0.584
, 44 0.278 0.278 0.278 0.278
- 45 0.333 0.333 0.333 0.333
. 46 0.278 0.278 0.278 0.278
/ 47 0.278 0.278 0.278 0.278
0 48 0.556 0.556 0.556 0.556
1 49 0.556 0.556 0.556 0.556
2 50 0.556 0.556 0.556 0.556
3 51 0.556 0.556 0.556 0.556
4 52 0.556 0.556 0.556 0.556
5 53 0.556 0.556 0.556 0.556
6 54 0.556 0.556 0.556 0.556
7 55 0.556 0.556 0.556 0.556
8 56 0.556 0.556 0.556 0.556
9 57 0.556 0.556 0.556 0.556
: 58 0.278 0.333 0.278 0.333
; 59 0.278 0.333 0.278 0.333
< 60 0.584 0.584 0.584 0.584
= 61 0.584 0.584 0.584 0.584
> 62 0.584 0.584 0.584 0.584
? 63 0.556 0.611 0.556 0.611
@ 64 1.015 0.975 1.015 0.975
A 65 0.667 0.722 0.667 0.722
B 66 0.667 0.722 0.667 0.722
C 67 0.722 0.722 0.722 0.722
D 68 0.722 0.722 0.722 0.722
E 69 0.667 0.667 0.667 0.667
F 70 0.611 0.611 0.611 0.611
G 71 0.778 0.778 0.778 0.778
H 72 0.722 0.722 0.722 0.722
I 73 0.278 0.278 0.278 0.278
J 74 0.500 0.556 0.500 0.556
K 75 0.667 0.722 0.667 0.722
L 76 0.556 0.611 0.556 0.611
M 77 0.833 0.833 0.833 0.833
N 78 0.722 0.722 0.722 0.722
O 79 0.778 0.778 0.778 0.778
P 80 0.667 0.667 0.667 0.667
Q 81 0.778 0.778 0.778 0.778
R 82 0.722 0.722 0.722 0.722
S 83 0.667 0.667 0.667 0.667
T 84 0.611 0.611 0.611 0.611
U 85 0.722 0.722 0.722 0.722
V 86 0.667 0.667 0.667 0.667
W 87 0.944 0.944 0.944 0.944
X 88 0.667 0.667 0.667 0.667
Y 89 0.667 0.667 0.667 0.667
Z 90 0.611 0.611 0.611 0.611
[ 91 0.278 0.333 0.278 0.333
\ 92 0.278 0.278 0.278 0.278
] 93 0.278 0.333 0.278 0.333
^ 94 0.469 0.584 0.469 0.584
_ 95 0.556 0.556 0.556 0.556
` 96 0.333 0.333 0.333 0.333
a 97 0.556 0.556 0.556 0.556
b 98 0.556 0.611 0.556 0.611
c 99 0.500 0.556 0.500 0.556
d 100 0.556 0.611 0.556 0.611
e 101 0.556 0.556 0.556 0.556
f 102 0.278 0.333 0.278 0.333
g 103 0.556 0.611 0.556 0.611
h 104 0.556 0.611 0.556 0.611
i 105 0.222 0.278 0.222 0.278
j 106 0.222 0.278 0.222 0.278
k 107 0.500 0.556 0.500 0.556
l 108 0.222 0.278 0.222 0.278
m 109 0.833 0.889 0.833 0.889
n 110 0.556 0.611 0.556 0.611
o 111 0.556 0.611 0.556 0.611
p 112 0.556 0.611 0.556 0.611
q 113 0.556 0.611 0.556 0.611
r 114 0.333 0.389 0.333 0.389
s 115 0.500 0.556 0.500 0.556
t 116 0.278 0.333 0.278 0.333
u 117 0.556 0.611 0.556 0.611
v 118 0.500 0.556 0.500 0.556
w 119 0.722 0.778 0.722 0.778
x 120 0.500 0.556 0.500 0.556
y 121 0.500 0.556 0.500 0.556
z 122 0.500 0.500 0.500 0.500
{ 123 0.334 0.389 0.334 0.389
| 124 0.260 0.280 0.260 0.280
} 125 0.334 0.389 0.334 0.389
~ 126 0.584 0.584 0.584 0.584
€ 128 0.556 0.556 0.556 0.556
‚ 130 0.222 0.278 0.222 0.278
ƒ 131 0.556 0.556 0.556 0.556
„ 132 0.333 0.500 0.333 0.500
… 133 1.000 1.000 1.000 1.000
† 134 0.556 0.556 0.556 0.556
‡ 135 0.556 0.556 0.556 0.556
ˆ 136 0.333 0.333 0.333 0.333
‰ 137 1.000 1.000 1.000 1.000
Š 138 0.667 0.667 0.667 0.667
‹ 139 0.333 0.333 0.333 0.333
Œ 140 1.000 1.000 1.000 1.000
Ž 142 0.611 0.611 0.611 0.611
‘ 145 0.222 0.278 0.222 0.278
’ 146 0.222 0.278 0.222 0.278
“ 147 0.333 0.500 0.333 0.500
” 148 0.333 0.500 0.333 0.500
• 149 0.350 0.350 0.350 0.350
– 150 0.556 0.556 0.556 0.556
— 151 1.000 1.000 1.000 1.000
˜ 152 0.333 0.333 0.333 0.333
™ 153 1.000 1.000 1.000 1.000
š 154 0.500 0.556 0.500 0.556
› 155 0.333 0.333 0.333 0.333
œ 156 0.944 0.944 0.944 0.944
ž 158 0.500 0.500 0.500 0.500
Ÿ 159 0.667 0.667 0.667 0.667
160 0.278 0.278 0.278 0.278
¡ 161 0.333 0.333 0.333 0.333
¢ 162 0.556 0.556 0.556 0.556
£ 163 0.556 0.556 0.556 0.556
¤ 164 0.556 0.556 0.556 0.556
¥ 165 0.556 0.556 0.556 0.556
¦ 166 0.260 0.280 0.260 0.280
§ 167 0.556 0.556 0.556 0.556
¨ 168 0.333 0.333 0.333 0.333
© 169 0.737 0.737 0.737 0.737
ª 170 0.370 0.370 0.370 0.370
« 171 0.556 0.556 0.556 0.556
¬ 172 0.584 0.584 0.584 0.584
- 173 0.333 0.333 0.333 0.333
® 174 0.737 0.737 0.737 0.737
¯ 175 0.552 0.552 0.552 0.552
° 176 0.400 0.400 0.400 0.400
± 177 0.549 0.549 0.549 0.549
² 178 0.333 0.333 0.333 0.333
³ 179 0.333 0.333 0.333 0.333
´ 180 0.333 0.333 0.333 0.333
µ 181 0.576 0.576 0.576 0.576
¶ 182 0.537 0.556 0.537 0.556
· 183 0.333 0.333 0.333 0.333
¸ 184 0.333 0.333 0.333 0.333
¹ 185 0.333 0.333 0.333 0.333
º 186 0.365 0.365 0.365 0.365
» 187 0.556 0.556 0.556 0.556
¼ 188 0.834 0.834 0.834 0.834
½ 189 0.834 0.834 0.834 0.834
¾ 190 0.834 0.834 0.834 0.834
¿ 191 0.611 0.611 0.611 0.611
À 192 0.667 0.722 0.667 0.722
Á 193 0.667 0.722 0.667 0.722
 194 0.667 0.722 0.667 0.722
à 195 0.667 0.722 0.667 0.722
Ä 196 0.667 0.722 0.667 0.722
Å 197 0.667 0.722 0.667 0.722
Æ 198 1.000 1.000 1.000 1.000
Ç 199 0.722 0.722 0.722 0.722
È 200 0.667 0.667 0.667 0.667
É 201 0.667 0.667 0.667 0.667
Ê 202 0.667 0.667 0.667 0.667
Ë 203 0.667 0.667 0.667 0.667
Ì 204 0.278 0.278 0.278 0.278
Í 205 0.278 0.278 0.278 0.278
Î 206 0.278 0.278 0.278 0.278
Ï 207 0.278 0.278 0.278 0.278
Ð 208 0.722 0.722 0.722 0.722
Ñ 209 0.722 0.722 0.722 0.722
Ò 210 0.778 0.778 0.778 0.778
Ó 211 0.778 0.778 0.778 0.778
Ô 212 0.778 0.778 0.778 0.778
Õ 213 0.778 0.778 0.778 0.778
Ö 214 0.778 0.778 0.778 0.778
× 215 0.584 0.584 0.584 0.584
Ø 216 0.778 0.778 0.778 0.778
Ù 217 0.722 0.722 0.722 0.722
Ú 218 0.722 0.722 0.722 0.722
Û 219 0.722 0.722 0.722 0.722
Ü 220 0.722 0.722 0.722 0.722
Ý 221 0.667 0.667 0.667 0.667
Þ 222 0.667 0.667 0.667 0.667
ß 223 0.611 0.611 0.611 0.611
à 224 0.556 0.556 0.556 0.556
á 225 0.556 0.556 0.556 0.556
â 226 0.556 0.556 0.556 0.556
ã 227 0.556 0.556 0.556 0.556
ä 228 0.556 0.556 0.556 0.556
å 229 0.556 0.556 0.556 0.556
æ 230 0.889 0.889 0.889 0.889
ç 231 0.500 0.556 0.500 0.556
è 232 0.556 0.556 0.556 0.556
é 233 0.556 0.556 0.556 0.556
ê 234 0.556 0.556 0.556 0.556
ë 235 0.556 0.556 0.556 0.556
ì 236 0.278 0.278 0.278 0.278
í 237 0.278 0.278 0.278 0.278
î 238 0.278 0.278 0.278 0.278
ï 239 0.278 0.278 0.278 0.278
ð 240 0.556 0.611 0.556 0.611
ñ 241 0.556 0.611 0.556 0.611
ò 242 0.556 0.611 0.556 0.611
ó 243 0.556 0.611 0.556 0.611
ô 244 0.556 0.611 0.556 0.611
õ 245 0.556 0.611 0.556 0.611
ö 246 0.556 0.611 0.556 0.611
÷ 247 0.549 0.549 0.549 0.549
ø 248 0.611 0.611 0.611 0.611
ù 249 0.556 0.611 0.556 0.611
ú 250 0.556 0.611 0.556 0.611
û 251 0.556 0.611 0.556 0.611
ü 252 0.556 0.611 0.556 0.611
ý 253 0.500 0.556 0.500 0.556
þ 254 0.556 0.611 0.556 0.611
ÿ 255 0.500 0.556 0.500 0.556
times
char unicode normal bold italic bolditalic
32 0.250 0.250 0.250 0.250
! 33 0.333 0.333 0.333 0.389
" 34 0.408 0.555 0.420 0.555
# 35 0.500 0.500 0.500 0.500
$ 36 0.500 0.500 0.500 0.500
% 37 0.833 1.000 0.833 0.833
& 38 0.778 0.833 0.778 0.778
' 39 0.180 0.278 0.214 0.278
( 40 0.333 0.333 0.333 0.333
) 41 0.333 0.333 0.333 0.333
* 42 0.500 0.500 0.500 0.500
+ 43 0.564 0.570 0.675 0.570
, 44 0.250 0.250 0.250 0.250
- 45 0.333 0.333 0.333 0.333
. 46 0.250 0.250 0.250 0.250
/ 47 0.278 0.278 0.278 0.278
0 48 0.500 0.500 0.500 0.500
1 49 0.500 0.500 0.500 0.500
2 50 0.500 0.500 0.500 0.500
3 51 0.500 0.500 0.500 0.500
4 52 0.500 0.500 0.500 0.500
5 53 0.500 0.500 0.500 0.500
6 54 0.500 0.500 0.500 0.500
7 55 0.500 0.500 0.500 0.500
8 56 0.500 0.500 0.500 0.500
9 57 0.500 0.500 0.500 0.500
: 58 0.278 0.333 0.333 0.333
; 59 0.278 0.333 0.333 0.333
< 60 0.564 0.570 0.675 0.570
= 61 0.564 0.570 0.675 0.570
> 62 0.564 0.570 0.675 0.570
? 63 0.444 0.500 0.500 0.500
@ 64 0.921 0.930 0.920 0.832
A 65 0.722 0.722 0.611 0.667
B 66 0.667 0.667 0.611 0.667
C 67 0.667 0.722 0.667 0.667
D 68 0.722 0.722 0.722 0.722
E 69 0.611 0.667 0.611 0.667
F 70 0.556 0.611 0.611 0.667
G 71 0.722 0.778 0.722 0.722
H 72 0.722 0.778 0.722 0.778
I 73 0.333 0.389 0.333 0.389
J 74 0.389 0.500 0.444 0.500
K 75 0.722 0.778 0.667 0.667
L 76 0.611 0.667 0.556 0.611
M 77 0.889 0.944 0.833 0.889
N 78 0.722 0.722 0.667 0.722
O 79 0.722 0.778 0.722 0.722
P 80 0.556 0.611 0.611 0.611
Q 81 0.722 0.778 0.722 0.722
R 82 0.667 0.722 0.611 0.667
S 83 0.556 0.556 0.500 0.556
T 84 0.611 0.667 0.556 0.611
U 85 0.722 0.722 0.722 0.722
V 86 0.722 0.722 0.611 0.667
W 87 0.944 1.000 0.833 0.889
X 88 0.722 0.722 0.611 0.667
Y 89 0.722 0.722 0.556 0.611
Z 90 0.611 0.667 0.556 0.611
[ 91 0.333 0.333 0.389 0.333
\ 92 0.278 0.278 0.278 0.278
] 93 0.333 0.333 0.389 0.333
^ 94 0.469 0.581 0.422 0.570
_ 95 0.500 0.500 0.500 0.500
` 96 0.333 0.333 0.333 0.333
a 97 0.444 0.500 0.500 0.500
b 98 0.500 0.556 0.500 0.500
c 99 0.444 0.444 0.444 0.444
d 100 0.500 0.556 0.500 0.500
e 101 0.444 0.444 0.444 0.444
f 102 0.333 0.333 0.278 0.333
g 103 0.500 0.500 0.500 0.500
h 104 0.500 0.556 0.500 0.556
i 105 0.278 0.278 0.278 0.278
j 106 0.278 0.333 0.278 0.278
k 107 0.500 0.556 0.444 0.500
l 108 0.278 0.278 0.278 0.278
m 109 0.778 0.833 0.722 0.778
n 110 0.500 0.556 0.500 0.556
o 111 0.500 0.500 0.500 0.500
p 112 0.500 0.556 0.500 0.500
q 113 0.500 0.556 0.500 0.500
r 114 0.333 0.444 0.389 0.389
s 115 0.389 0.389 0.389 0.389
t 116 0.278 0.333 0.278 0.278
u 117 0.500 0.556 0.500 0.556
v 118 0.500 0.500 0.444 0.444
w 119 0.722 0.722 0.667 0.667
x 120 0.500 0.500 0.444 0.500
y 121 0.500 0.500 0.444 0.444
z 122 0.444 0.444 0.389 0.389
{ 123 0.480 0.394 0.400 0.348
| 124 0.200 0.220 0.275 0.220
} 125 0.480 0.394 0.400 0.348
~ 126 0.541 0.520 0.541 0.570
€ 128 0.500 0.500 0.500 0.500
‚ 130 0.333 0.333 0.333 0.333
ƒ 131 0.500 0.500 0.500 0.500
„ 132 0.444 0.500 0.556 0.500
… 133 1.000 1.000 0.889 1.000
† 134 0.500 0.500 0.500 0.500
‡ 135 0.500 0.500 0.500 0.500
ˆ 136 0.333 0.333 0.333 0.333
‰ 137 1.000 1.000 1.000 1.000
Š 138 0.556 0.556 0.500 0.556
‹ 139 0.333 0.333 0.333 0.333
Œ 140 0.889 1.000 0.944 0.944
Ž 142 0.611 0.667 0.556 0.611
‘ 145 0.333 0.333 0.333 0.333
’ 146 0.333 0.333 0.333 0.333
“ 147 0.444 0.500 0.556 0.500
” 148 0.444 0.500 0.556 0.500
• 149 0.350 0.350 0.350 0.350
– 150 0.500 0.500 0.500 0.500
— 151 1.000 1.000 0.889 1.000
˜ 152 0.333 0.333 0.333 0.333
™ 153 0.980 1.000 0.980 1.000
š 154 0.389 0.389 0.389 0.389
› 155 0.333 0.333 0.333 0.333
œ 156 0.722 0.722 0.667 0.722
ž 158 0.444 0.444 0.389 0.389
Ÿ 159 0.722 0.722 0.556 0.611
160 0.250 0.250 0.250 0.250
¡ 161 0.333 0.333 0.389 0.389
¢ 162 0.500 0.500 0.500 0.500
£ 163 0.500 0.500 0.500 0.500
¤ 164 0.500 0.500 0.500 0.500
¥ 165 0.500 0.500 0.500 0.500
¦ 166 0.200 0.220 0.275 0.220
§ 167 0.500 0.500 0.500 0.500
¨ 168 0.333 0.333 0.333 0.333
© 169 0.760 0.747 0.760 0.747
ª 170 0.276 0.300 0.276 0.266
« 171 0.500 0.500 0.500 0.500
¬ 172 0.564 0.570 0.675 0.606
- 173 0.333 0.333 0.333 0.333
® 174 0.760 0.747 0.760 0.747
¯ 175 0.500 0.500 0.500 0.500
° 176 0.400 0.400 0.400 0.400
± 177 0.549 0.549 0.549 0.549
² 178 0.300 0.300 0.300 0.300
³ 179 0.300 0.300 0.300 0.300
´ 180 0.333 0.333 0.333 0.333
µ 181 0.576 0.576 0.576 0.576
¶ 182 0.453 0.540 0.523 0.500
· 183 0.333 0.333 0.250 0.250
¸ 184 0.333 0.333 0.333 0.333
¹ 185 0.300 0.300 0.300 0.300
º 186 0.310 0.330 0.310 0.300
» 187 0.500 0.500 0.500 0.500
¼ 188 0.750 0.750 0.750 0.750
½ 189 0.750 0.750 0.750 0.750
¾ 190 0.750 0.750 0.750 0.750
¿ 191 0.444 0.500 0.500 0.500
À 192 0.722 0.722 0.611 0.667
Á 193 0.722 0.722 0.611 0.667
 194 0.722 0.722 0.611 0.667
à 195 0.722 0.722 0.611 0.667
Ä 196 0.722 0.722 0.611 0.667
Å 197 0.722 0.722 0.611 0.667
Æ 198 0.889 1.000 0.889 0.944
Ç 199 0.667 0.722 0.667 0.667
È 200 0.611 0.667 0.611 0.667
É 201 0.611 0.667 0.611 0.667
Ê 202 0.611 0.667 0.611 0.667
Ë 203 0.611 0.667 0.611 0.667
Ì 204 0.333 0.389 0.333 0.389
Í 205 0.333 0.389 0.333 0.389
Î 206 0.333 0.389 0.333 0.389
Ï 207 0.333 0.389 0.333 0.389
Ð 208 0.722 0.722 0.722 0.722
Ñ 209 0.722 0.722 0.667 0.722
Ò 210 0.722 0.778 0.722 0.722
Ó 211 0.722 0.778 0.722 0.722
Ô 212 0.722 0.778 0.722 0.722
Õ 213 0.722 0.778 0.722 0.722
Ö 214 0.722 0.778 0.722 0.722
× 215 0.564 0.570 0.675 0.570
Ø 216 0.722 0.778 0.722 0.722
Ù 217 0.722 0.722 0.722 0.722
Ú 218 0.722 0.722 0.722 0.722
Û 219 0.722 0.722 0.722 0.722
Ü 220 0.722 0.722 0.722 0.722
Ý 221 0.722 0.722 0.556 0.611
Þ 222 0.556 0.611 0.611 0.611
ß 223 0.500 0.556 0.500 0.500
à 224 0.444 0.500 0.500 0.500
á 225 0.444 0.500 0.500 0.500
â 226 0.444 0.500 0.500 0.500
ã 227 0.444 0.500 0.500 0.500
ä 228 0.444 0.500 0.500 0.500
å 229 0.444 0.500 0.500 0.500
æ 230 0.667 0.722 0.667 0.722
ç 231 0.444 0.444 0.444 0.444
è 232 0.444 0.444 0.444 0.444
é 233 0.444 0.444 0.444 0.444
ê 234 0.444 0.444 0.444 0.444
ë 235 0.444 0.444 0.444 0.444
ì 236 0.278 0.278 0.278 0.278
í 237 0.278 0.278 0.278 0.278
î 238 0.278 0.278 0.278 0.278
ï 239 0.278 0.278 0.278 0.278
ð 240 0.500 0.500 0.500 0.500
ñ 241 0.500 0.556 0.500 0.556
ò 242 0.500 0.500 0.500 0.500
ó 243 0.500 0.500 0.500 0.500
ô 244 0.500 0.500 0.500 0.500
õ 245 0.500 0.500 0.500 0.500
ö 246 0.500 0.500 0.500 0.500
÷ 247 0.549 0.549 0.549 0.549
ø 248 0.500 0.500 0.500 0.500
ù 249 0.500 0.556 0.500 0.556
ú 250 0.500 0.556 0.500 0.556
û 251 0.500 0.556 0.500 0.556
ü 252 0.500 0.556 0.500 0.556
ý 253 0.500 0.500 0.444 0.444
þ 254 0.500 0.556 0.500 0.500
ÿ 255 0.500 0.500 0.444 0.444
courier
All characters in every style are 0.600 (font is monospaced)
Run Code Online (Sandbox Code Playgroud)
Once you have determined the width of your string with the above table you need to convert the answer to your desired unit of measurement.
width (in inches) = (character or string width * font size) / (72 pt/in)
width (in mm) = (character or string width * font size * 25.4 mm/in) / (72 pt/in)
Run Code Online (Sandbox Code Playgroud)
The only caveat to using the above I could think of was if your output pdf was making use of kerning pairs. My output files did not appear to be using kerning pairs, so I did not explore the issue further. If for some reason this is an issue for you, you would need to adjust your widths slightly for each kerning pair as it occurred.
高度的计算要少得多.单行文本的高度就是字体大小.大小为10的文本行将占用10磅.
如果您想要计算多行文本,则需要考虑"行高"(基本上是行之间的空白量).在我的测试中,我发现jsPDF使用字体大小的115%(1.15x)的默认行高.因此,您对行高的计算将变为:
height = font size * number of lines * line height
Example:
height = 10 pt * 5 lines * 1.15
height = 57.5 pt
Run Code Online (Sandbox Code Playgroud)
然后,您只需转换为您想要的测量单位,如上所述.