Skip to content

Commit e8a9a3b

Browse files
committed
fix: Fixed issue generating mapped values in PDF files where field number exceeds maximum field width
1 parent 603c835 commit e8a9a3b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/map.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ async function map(pdfFile, options = {}) {
7373

7474
// update the form with each input ID `key` filled as unique index `i`
7575
log('calling fillFormWithFlattenAsync', pdfFile);
76-
await pdfDoc.fillForm(template, filledFile);
76+
await pdfDoc.fillForm(template);
77+
78+
await pdfDoc.save(filledFile, false);
7779

7880
return {
7981
map: mapFile,

src/pdf.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,18 @@ export default class PDF {
6363
try {
6464
field.setText(filledTemplate[key]);
6565
} catch (ex) {
66-
if (`${ex}`.indexOf(' must be of type `string`') >= 0) {
66+
const msg = `${ex}`;
67+
if (msg.indexOf(' must be of type `string`') >= 0) {
6768
// automatically convert numbers to strings
6869
field.setText(`${filledTemplate[key]}`);
70+
} else if (msg.indexOf('Attempted to set text with length=')) {
71+
// the field value is too big for the field. truncate.
72+
// TODO: there should probably be two modes. when filling examples
73+
// this can happen, but when filling for real, it should error.
74+
const matched = /maxLength=(\d+) /.exec(msg);
75+
const limit = parseInt(matched[1], 10);
76+
const value = `${filledTemplate[key]}`;
77+
field.setText(value.slice(0, limit));
6978
} else {
7079
throw Error(
7180
`${ex}, form=${this.name} key=${key}, value="${filledTemplate[key]}"`);

0 commit comments

Comments
 (0)