title: Watermarking an existing PDF in Drupal
body: <p>I'm working on a project in DRUPAL and am trying to programatically add an  image to an existing pdf - an "Approved" image for invoices. I've Frankensteined the code from the StackExchange post - <a href="https://stackoverflow.com/questions/2913934/how-i-can-add-watermark-to-existing-pdf-file-using-php">How can I add a watermark to an existing PDF file using PHP?</a> - and have managed to get it working outside of Drupal (7.0) but once I port the code over, I get nothing, no errors, no warnings, nadda .... Just wondering if someone out there can help me on this issue, it been driving me nuts for a bit now ....   Sorry about the sloppy code....</p>
  
  <pre><code>&lt;?php
  global $user;
  ob_start();
  $filename ="approved_".$user-&gt;name.".png";
  $file = "2pages.pdf";
  $op = 100;
  ////////////////////////////////////////////////////////////////////////////
  require('fpdf.php');
  require('fpdi.php');
  $text = "Text message for watermark goes here";
  $name = uniqid();
  $font_size = 5;
  $ts=explode("\n",$text);
  $width=0;
  foreach ($ts as $k=&gt;$string) {
      $width=max($width,strlen($string));
  }
  $width  = imagefontwidth($font_size)*$width;
  $height = imagefontheight($font_size)*count($ts);
  $el=imagefontheight($font_size);
  $em=imagefontwidth($font_size);
  $img = imagecreatetruecolor($width,$height);
  // Background color
  $bg = imagecolorallocate($img, 255, 255, 255);
  imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
  // Font color
  $color = imagecolorallocate($img, 0, 0, 0);
  foreach ($ts as $k=&gt;$string) {
      $len = strlen($string);
      $ypos = 0;
      for($i=0;$i&lt;$len;$i++){
          $xpos = $i * $em;
          $ypos = $k * $el;
          imagechar($img, $font_size, $xpos, $ypos, $string, $color);
          $string = substr($string, 1);      
      }
  }
  imagecolortransparent($img, $bg);
  $blank = imagecreatetruecolor($width, $height);
  $tbg = imagecolorallocate($blank, 255, 255, 255);
  imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg);
  imagecolortransparent($blank, $tbg);
  
  if ( ($op &lt; 0) OR ($op &gt;100) ){
      $op = 100;
  }
  imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op);
  $pdf = new FPDI();
  //********************************************************************
   if (file_exists($file)){
      $pagecount = $pdf-&gt;setSourceFile($file);
   } else {
      return FALSE;
   }
   /////////////////////////////////////////
   for($i=1; $i &lt;= $pagecount; $i++) {   
    $tpl = $pdf-&gt;importPage($i);    
    $pdf-&gt;addPage();     
    $pdf-&gt;useTemplate($tpl, 1, 1, 0, 0, TRUE);    
    //Put the watermark  
    $pdf-&gt;Image($filename, 0, 0, 40, 25, 'png');}   
    /////////////////////////////////////////
    return $pdf-&gt;Output('test10.pdf', 'F');
  ////////////////////////////////////////////////////////////////////////////
  ob_end_flush();
  ?&gt;
  </code></pre>
  
  <p>Of note, I am also open you anyone who may have an alternate method to achieve the same end result</p>
  
  <p>thanks in advance!</p>