Daniel Huckstep
Daniel Huckstep

Reputation: 5408

Mass PDF form fill

I need to fill in a single PDF template multiple times and concat the results. When I say multiple, I mean up to a few hundred times, potentially over one thousand.

I can do this with pdftk fill_form, one by one, and then use pdftk cat. We can parallelize this fairly easily.

I'm curious if this is the only option, or if there is a piece of software (Linux + OSX, command line) that will allow me to say "take this template, and these sets of fields, fill out this form, and concat the files" so I can avoid doing every one individually. Then again, if something does exist, but it's not any faster than just doing the fork parallelization method, then it's probably not worth it.

Upvotes: 2

Views: 1137

Answers (2)

NoobInLifeGeneral
NoobInLifeGeneral

Reputation: 21

I dont know if this answers your question exactly but this is how i do it.

for each objfile in colFiles
If right(objfile.Name,3) = "pdf" Then
FileFill = strFolderPath + "\" + objFile.Name
filled = strFolderPath + "\" + Replace(objFile.Name,".pdf", "_.pdf")
pdfnumber = Left(objfile.name,InStr(objFile.Name," ")-1)
set objFDF = objFSO.CreateTextFile(fdfform,true)
    objFDF.write "%FDF-1.2" & vbCrLf
    objFDF.write "%âãÏÓ" & vbCrLf
    objFDF.write "1 0 obj " & vbCrLf
    objFDF.write "<<" & vbCrLf
    objFDF.write "/FDF " & vbCrLf
    objFDF.write "<<" & vbCrLf
    objFDF.write "/Fields [" & vbCrLf
    objFDF.write "<<" & vbCrLf
    objFDF.write "/V (" & pdfnumber & ")" & vbCrLf
    objFDF.write "/T (Tekening nr)" & vbCrLf
    objFDF.write ">> " & vbCrLf
    'i have lots more inbetween
    objFDF.write ">>" & vbCrLf
    objFDF.write "endobj " & vbCrLf
    objFDF.write "trailer" & vbCrLf
    objFDF.write "" & vbCrLf
    objFDF.write "<<" & vbCrLf
    objFDF.write "/Root 1 0 R" & vbCrLf
    objFDF.write ">>" & vbCrLf
    objFDF.write "%%EOF" & vbCrLf
    objFDF.Close
    shl.run "cmd.exe /C pdftk """ & FileFill & """ fill_form """ & fdfform & """ output """ & filled & """"
    'wscript.echo "cmd.exe /C pdftk """ & FileFill & """ fill_form """ & fdfform & """ output """ & FileFill & """"
Wscript.sleep 1000
'objFSO.MoveFile strFolderPath + "\" + objFile.Name , strFolderPath + "\" + Replace(objFile.Name," ", "_")
fSO.DeleteFile(FileFill)
Wscript.sleep 1000
fSO.DeleteFile(fdfform)
Wscript.sleep 1000
Else
'nothing
end If

Next

This is just the section that handles the fill_form and cat so this code pasted in a vbs wont work but hopefully i can help you somewhat.

If you want to know how your .fdf needs to look you can run pdftk test.pdf generate_fdf output outputFile.fdf in CMD.

PS, not a programmer or engineer what so ever but im figuring this out myself so maybe i can point you in the right direction.

Upvotes: 0

Chris Dolan
Chris Dolan

Reputation: 8973

My Perl library CAM::PDF can do this. The form filling is a bit weak (it doesn't support checkboxes, for example) but the concatenation works great.

#perl -w                                                                            
use strict;
use CAM::PDF;

my $infile = 'in.pdf';
my $outfile = 'out.pdf';
my @fills = (
   { name => 'John' },
   { name => 'Fred' },
);

my $pdf = CAM::PDF->new($infile) or die $CAM::PDF::errstr;
for my $i (0 .. @fills-1) {
   my $filledPDF = $i == 0 ? $pdf : CAM::PDF->new($infile);
   $filledPDF->fillFormFields(%{$fills[$i]});
   if ($i > 0) {
      $pdf->appendPDF($filledPDF);
   }
}
$pdf->cleanoutput($outfile) or die;

Upvotes: 1

Related Questions