fix(planting-service): 签名图片保持原始尺寸放置
- 不再缩放签名图片适应按钮大小 - 直接在按钮位置绘制原始尺寸的签名 - 避免签名被压扁变形 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
666be6ea60
commit
6c017d2086
|
|
@ -382,48 +382,31 @@ export class PdfGeneratorService {
|
||||||
const form = pdfDoc.getForm();
|
const form = pdfDoc.getForm();
|
||||||
const signatureButton = form.getButton(FORM_FIELDS.SIGNATURE);
|
const signatureButton = form.getButton(FORM_FIELDS.SIGNATURE);
|
||||||
|
|
||||||
// 获取按钮的 widget 和尺寸
|
// 获取按钮的 widget 和位置
|
||||||
const widgets = signatureButton.acroField.getWidgets();
|
const widgets = signatureButton.acroField.getWidgets();
|
||||||
if (widgets.length > 0) {
|
if (widgets.length > 0) {
|
||||||
const widget = widgets[0];
|
const widget = widgets[0];
|
||||||
const rect = widget.getRectangle();
|
const rect = widget.getRectangle();
|
||||||
const { width: fieldWidth, height: fieldHeight } = rect;
|
|
||||||
|
|
||||||
// 计算图片缩放尺寸(保持宽高比,适应字段大小)
|
// 获取签名图片原始尺寸
|
||||||
const imgDims = signatureImage.scale(1);
|
const imgDims = signatureImage.scale(1);
|
||||||
let scale = Math.min(fieldWidth / imgDims.width, fieldHeight / imgDims.height);
|
const imgWidth = imgDims.width;
|
||||||
const scaledWidth = imgDims.width * scale;
|
const imgHeight = imgDims.height;
|
||||||
const scaledHeight = imgDims.height * scale;
|
|
||||||
|
|
||||||
// 计算居中位置
|
// 直接在按钮位置绘制原始尺寸的签名图片
|
||||||
const x = (fieldWidth - scaledWidth) / 2;
|
// 获取按钮所在的页面
|
||||||
const y = (fieldHeight - scaledHeight) / 2;
|
const pages = pdfDoc.getPages();
|
||||||
|
const page = pages[5]; // 第6页(签名区域)
|
||||||
|
|
||||||
// 创建外观流内容
|
// 在按钮的左下角位置绘制签名图片(保持原始尺寸)
|
||||||
const appearanceStream = `q ${scaledWidth} 0 0 ${scaledHeight} ${x} ${y} cm /Img Do Q`;
|
page.drawImage(signatureImage, {
|
||||||
|
x: rect.x,
|
||||||
// 创建 XObject Form 作为外观
|
y: rect.y,
|
||||||
const context = pdfDoc.context;
|
width: imgWidth,
|
||||||
const imageXObjectName = 'Img';
|
height: imgHeight,
|
||||||
|
|
||||||
const Resources = context.obj({
|
|
||||||
XObject: { [imageXObjectName]: signatureImage.ref },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const appearanceStreamRef = context.register(
|
this.logger.log(`Signature embedded at (${rect.x}, ${rect.y}) with original size ${imgWidth}x${imgHeight}`);
|
||||||
context.stream(appearanceStream, {
|
|
||||||
Type: 'XObject',
|
|
||||||
Subtype: 'Form',
|
|
||||||
FormType: 1,
|
|
||||||
BBox: [0, 0, fieldWidth, fieldHeight],
|
|
||||||
Resources,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
// 设置外观到 widget
|
|
||||||
widget.setNormalAppearance(appearanceStreamRef);
|
|
||||||
|
|
||||||
this.logger.log('Signature embedded using custom appearance stream');
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No widgets found for signature button');
|
throw new Error('No widgets found for signature button');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue