You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone,
I'm encountering an issue while using the pdf-lib library to create form fields on a multi-page PDF. Although the code appears to be correct, only the form fields on the first page are present in the generated PDF file. I'm seeking assistance to resolve this problem.
Here's my code:
import{FormFieldType}from"@/types";import{PDFDocument,rgb,StandardFonts}from"pdf-lib";exportasyncfunctionhandleSaveAndExport(pdfFile: File|null,formFields: FormFieldType[]){if(!pdfFile)return;constpdfBytes=awaitpdfFile.arrayBuffer();constpdfDoc=awaitPDFDocument.load(pdfBytes);constform=pdfDoc.getForm();constpages=pdfDoc.getPages();console.log(pages);// Output shows correct number of pagesconsthelveticaFont=awaitpdfDoc.embedFont(StandardFonts.Helvetica);formFields.forEach((field)=>{constpage=pages[field.page-1];const{ height }=page.getSize();console.log(`Processing field ${field.key} on page ${field.page}`);// This log shows fields being added to different pagesconsttextField=form.createTextField(field.key);textField.addToPage(page,{x: field.x,y: height-field.y-field.height,// Adjusting y coordinate as PDF coordinate system starts from bottomwidth: field.width,height: field.height,borderWidth: 0,backgroundColor: rgb(1,1,1),});textField.setFontSize(12);textField.setText(field.key);textField.enableReadOnly();textField.updateAppearances(helveticaFont);});constpdfBytesWithForm=awaitpdfDoc.save();// ... code to save the file ...}
Problem Description:
Console output shows the correct number of pages in the PDF document.
Logs indicate that fields are correctly added to different pages.
However, in the generated PDF file, only the first page contains form fields.
I've already tried the following:
Opening the generated file with different PDF viewers (including Adobe Acrobat Reader).
Checking the size of the generated PDF file to confirm it's larger than the original.
Printing field information for each page before and after saving to confirm fields are correctly added.
I suspect the issue might be in the PDF saving or rendering process, but I'm not sure about the exact cause.
Can anyone help me identify the problem? Or do you have any suggestions for further debugging this issue?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
I'm encountering an issue while using the pdf-lib library to create form fields on a multi-page PDF. Although the code appears to be correct, only the form fields on the first page are present in the generated PDF file. I'm seeking assistance to resolve this problem.
Here's my code:
Problem Description:
I've already tried the following:
I suspect the issue might be in the PDF saving or rendering process, but I'm not sure about the exact cause.
Can anyone help me identify the problem? Or do you have any suggestions for further debugging this issue?
Thank you very much for your help!
Beta Was this translation helpful? Give feedback.
All reactions