What's new

HELP Needed! Creating Script to Fill batch PDF form and save it with customized name.

rolltoll

Addict
Joined
Jul 11, 2019
Posts
30
Reaction
3
Points
76
I want to FILL and GENERATE multiple PDF forms using JS. I have my data in a CSV file, and I am looking for someone who can help me build a script for it. Here is the flow.

Open fillable PDF > Populate fields automatically from CSV file > Saving all PDF with a customized Filename based on the form fields.

Hope someone can help me. TIA
 
Yes, I can help you with that. To achieve your desired flow, you will need to use a combination of JavaScript, a PDF library, and some file handling operations. Here's a step-by-step guide on how you can accomplish this task.

1. Set up your project:
- Create a new folder for your project and navigate to it in your command line or terminal.
- Initialize a Node.js project by running npm init and follow the prompts.
- Install the required dependencies by running npm install pdf-lib csv-parser.

2. Write the JavaScript code:
- Create a new file, let's call it script.js, and open it in a code editor.
- Import the necessary modules at the top of the file:
Code:
javascript
     const { PDFDocumentFactory, PDFDocumentWriter, PDFName } = require('pdf-lib');
     const csv = require('csv-parser');
     const fs = require('fs');
- Define the path to your CSV file and the PDF template file:
Code:
javascript
     const csvFilePath = './data.csv';
     const pdfTemplatePath = './template.pdf';
- Read the CSV file and process each row:
Code:
javascript
     fs.createReadStream(csvFilePath)
       .pipe(csv())
       .on('data', (row) => {
         // Process each row of the CSV file
         fillPDFAndSave(row);
       })
       .on('end', () => {
         console.log('All PDFs generated successfully!');
       });
- Implement the fillPDFAndSave function to fill the PDF form and save it:
Code:
javascript
     async function fillPDFAndSave(data) {
       // Load the PDF template
       const pdfDoc = await PDFDocumentFactory.load(fs.readFileSync(pdfTemplatePath));

       // Get the form fields from the PDF template
       const form = pdfDoc.getForm();
       const fields = form.getFields();

       // Populate the form fields with data from the CSV row
       for (const field of Object.values(fields)) {
         const fieldName = field.getName();

         if (data[fieldName]) {
           // Set the field value
           field.setValue(data[fieldName]);
         }
       }

       // Generate a customized filename based on the form fields
       const filename = `${data.Name.replace(' ', '_')}_${data.ID}.pdf`;

       // Save the filled PDF with the customized filename
       const pdfBytes = await PDFDocumentWriter.saveToBytes(pdfDoc);
       fs.writeFileSync(filename, pdfBytes);
     }
- Save the file.

3. Prepare your CSV file:
- Create a CSV file named data.csv with a header row containing the names of the form fields in the PDF template.
- Fill the subsequent rows with the data you want to populate in the PDF forms.

4. Prepare your PDF template:
- Create a fillable PDF form using a tool like Adobe Acrobat or any other PDF editor.
- Ensure that the field names in the PDF form match the column names in your CSV file.

5. Execute the script:
- Open your command line or terminal and navigate to the project folder.
- Run the script by executing the command node script.js.

The script will read the CSV file, fill the PDF form for each row, and save the filled PDFs with customized filenames based on the form fields. Make sure to replace the placeholders like data.Name and data.ID with the actual field names from your CSV file.

Remember to install the required dependencies using the npm install command before executing the script. Let me know if you encounter any issues or have any further questions!
 

Similar threads

Back
Top