You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jsPDF英寸单位下如何生成仅带细黑边框的空白矩形?

解决jsPDF生成仅边框空白矩形的问题

问题出在你调用rect()方法时,默认会使用填充模式,再加上之前设置了黑色填充色,导致矩形被完全涂黑。要生成仅带细黑边框的空白矩形,有以下几种直接的解决办法:

方法1:修改rect方法的绘制模式

rect方法的第五个参数可以指定绘制样式,传入'S'表示仅描边(Stroke),默认值是'F'(填充Fill)。同时不需要设置填充色:

const pdf = new jsPDF({
  unit: 'in',
  format: 'letter'
});
const x = 1;   
const y = 1;  
const width = 4;  
const height = 2; 
pdf.rect(x, y, width, height, 'S'); // 最后一个参数指定为'S'仅绘制边框

方法2:使用strokeRect方法直接绘制描边矩形

jsPDF专门提供了strokeRect方法,作用就是只绘制矩形边框,无需额外设置样式参数:

const pdf = new jsPDF({
  unit: 'in',
  format: 'letter'
});
const x = 1;   
const y = 1;  
const width = 4;  
const height = 2; 
pdf.strokeRect(x, y, width, height); // 直接生成仅带边框的空白矩形

方法3:自定义边框粗细(可选)

如果需要调整边框的粗细,可以用setLineWidth方法,单位和你设置的英寸保持一致,比如设置0.01英寸的细边框:

const pdf = new jsPDF({
  unit: 'in',
  format: 'letter'
});
const x = 1;   
const y = 1;  
const width = 4;  
const height = 2; 
pdf.setLineWidth(0.01); // 设置细边框宽度
pdf.strokeRect(x, y, width, height);

内容的提问来源于stack exchange,提问作者Jerry Duron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 13:32:37