fix: insert horizontal field into pdf

This commit is contained in:
Ephraim Atta-Duncan
2025-10-07 21:11:58 +00:00
parent 0cb0a708d5
commit c98dff12e8

View File

@ -331,36 +331,75 @@ export const insertFieldInPDF = async (pdf: PDFDocument, field: FieldWithSignatu
}));
const selected = field.customText.split(',');
const direction = meta.data.direction ?? 'vertical';
const topPadding = 12;
const leftRadioPadding = 8;
const leftRadioLabelPadding = 12;
const radioSpaceY = 13;
for (const [index, item] of (values ?? []).entries()) {
const offsetY = index * radioSpaceY + topPadding;
if (direction === 'horizontal') {
let currentX = leftRadioPadding;
let currentY = topPadding;
const maxWidth = pageWidth - fieldX - leftRadioPadding * 2;
const radio = pdf.getForm().createRadioGroup(`radio.${field.secondaryId}.${index}`);
for (const [index, item] of (values ?? []).entries()) {
const radio = pdf.getForm().createRadioGroup(`radio.${field.secondaryId}.${index}`);
// Draw label.
page.drawText(item.value.includes('empty-value-') ? '' : item.value, {
x: fieldX + leftRadioPadding + leftRadioLabelPadding,
y: pageHeight - (fieldY + offsetY),
size: 12,
font,
rotate: degrees(pageRotationInDegrees),
});
const labelText = item.value.includes('empty-value-') ? '' : item.value;
const labelWidth = font.widthOfTextAtSize(labelText, 12);
const itemWidth = leftRadioLabelPadding + labelWidth + 16;
// Draw radio button.
radio.addOptionToPage(item.value, page, {
x: fieldX + leftRadioPadding,
y: pageHeight - (fieldY + offsetY),
height: 8,
width: 8,
});
if (currentX + itemWidth > maxWidth && index > 0) {
currentX = leftRadioPadding;
currentY += radioSpaceY;
}
if (selected.includes(item.value)) {
radio.select(item.value);
page.drawText(labelText, {
x: fieldX + currentX + leftRadioLabelPadding,
y: pageHeight - (fieldY + currentY),
size: 12,
font,
rotate: degrees(pageRotationInDegrees),
});
radio.addOptionToPage(item.value, page, {
x: fieldX + currentX,
y: pageHeight - (fieldY + currentY),
height: 8,
width: 8,
});
if (selected.includes(item.value)) {
radio.select(item.value);
}
currentX += itemWidth;
}
} else {
for (const [index, item] of (values ?? []).entries()) {
const offsetY = index * radioSpaceY + topPadding;
const radio = pdf.getForm().createRadioGroup(`radio.${field.secondaryId}.${index}`);
page.drawText(item.value.includes('empty-value-') ? '' : item.value, {
x: fieldX + leftRadioPadding + leftRadioLabelPadding,
y: pageHeight - (fieldY + offsetY),
size: 12,
font,
rotate: degrees(pageRotationInDegrees),
});
radio.addOptionToPage(item.value, page, {
x: fieldX + leftRadioPadding,
y: pageHeight - (fieldY + offsetY),
height: 8,
width: 8,
});
if (selected.includes(item.value)) {
radio.select(item.value);
}
}
}
})