use PDF::Tk; my $pdftk=PDF::Tk->new(pdftk=>'/usr/bin/pdftk'); ... # instead of filling @pdfLines from my $pdf_with_bg = ""; # to be filled by pdftk my $pdf_in_memory; { local $/ = undef; $pdf_in_memory = ; } # whole file as one big string $pdftk->call_pdftk(\$pdf_in_memory, \$pdf_with_bg, 'background', '/path/to/background.pdf'); print "Content-Type: application/pdf\n"; print "Content-Disposition: attachment; filename=record.pdf\n\n"; print $pdf_with_bg;