Applying watermarks on pdf files when users try to download the files

ryan picture ryan · Oct 21, 2010 · Viewed 31k times · Source

The solutions for my school's assignments all have waterstamps on the PDFs with our username on it.

I was wondering if you guys know how to do something like that using PHP? Do they run a script prior to the download process?

Thanks.

Answer

symcbean picture symcbean · Oct 21, 2010

Although there are several very good PDF libs for PHP, if I were writing such a program I'd just shell out to run pdftk but you' still need to generate your watermark.

$tempfile=tempnam();
system("pdftk input_file.pdf background watermark.pdf output $tempfile dont_ask", $errcode);
if (!$errcode && $ih=fopen($tempfile, 'r')) {
    header('Content-Type: application/pdf');
    fpassthru($ih);
    fclose($ih);
} else {
    print "Whoops";
}
unlink($tempfile);