Jump to content

Watermarking PDF files before download or view


al

Recommended Posts

Hi PHP Community,

I have this class in my DOCMAN script and would like to add aditional functionality to watermark existing PDF files:

 

class DOCMAN_File
{
/**
* @access public
* @var string
*/
    var $path 			= null;
/**
* @access public
* @var string
*/
    var $name 			= null;
/**
* @access public
* @var string
*/
    var $mime 			= null;
    /**
* @access public
* @var string
*/
    var $ext 			= null;
    
    /**
* @access public
* @var string
*/
    var $size			= null;
    
    /**
* @access public
* @var string
*/
    var $date			= null;
    
    /**
* @access private
* @var string
*/
    var $_err    		= null;

/**
 * @access private
 * @var boolean
 */
var $_isLink;

function DOCMAN_File($name, $path)
{
	$path = mosPathName( $path );
	if (!is_dir( $path )) {
		$path = dirname( $path );
	}

	$this->name = trim($name);
	$this->path = $path;

	if( strcasecmp( substr( $this->name , 0, _DM_DOCUMENT_LINK_LNG ) , _DM_DOCUMENT_LINK )==0){
		$this->_isLink = true;
		$this->size    = 0;
		$this->mime    = 'link';
	}else{
		$this->_isLink = false;
		$this->size = filesize($this->path."/".$this->name);
		$this->mime	= DOCMAN_MIME_Magic::filenameToMIME($this->name, false);
	}

	$this->ext  = $this->getExtension();
}


/**
*    Download or view a file from the server
*    @desc This is the function handling files downloading using HTTP protocol
*    @param void
*    @return void
*/

    function download()
    {
	$user_agent = (isset($_SERVER["HTTP_USER_AGENT"]) ) ? 
	 				$_SERVER["HTTP_USER_AGENT"] : $HTTP_USER_AGENT;

	//Fixed [#2534] clean all output buffers (needs PHP > 4.2.0) 
		/* while (ob_get_level()) {
     	 *	ob_end_clean();
   		}*/
	// Fix [3164]
	while (@ob_end_clean());

	if( $this->_isLink ){
		header( "Location: " . substr( $this->name , 6 ) );		
		return;
	}

	$fsize = filesize($this->path."/".$this->name);
	$mod_date = date('r', filemtime( $this->path.'/'.$this->name ) );

	$cont_dis = 'attachment;'; 
	if(isset($_REQUEST['mode']))
		$cont_dis = ($_REQUEST['mode'] == 'view') ? 'inline;' : 'attachment;';

        header("Pragma: public");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Expires: 0");

        header("Content-Transfer-Encoding: binary");
	header('Content-Disposition:' . $cont_dis .'";'
		. ' filename="' . $this->name . '";' 
		. ' modification-date="' . $mod_date . '";'
		. ' size=' . $fsize .';'
		); //RFC2183
        header("Content-Location: ". $this->path . '/' . $this->name );
        header("Content-Type: "    . $this->mime );			// MIME type
        header("Content-Length: "  . $fsize);

		// No encoding - we aren't using compression... (RFC1945)
	// header("Content-Encoding: ");

        readfile($this->path."/".$this->name);
        // The caller MUST 'die();'
    }
    
 function exists() {
	if( $this->_isLink ){
		return true;
	}
    return file_exists($this->path."/".$this->name);
    }

function isLink(){
	return $this->_isLink ;
}
    
    /**
*    Get file size
*
*    @desc Gets the file size and convert it to friendly format
*    @param void
*    @return string Returns filesize in a friendly format.
*/

function getSize()
    {	
	if( $this->_isLink ){
		return 'Link';
	}
        $kb = 1024;
        $mb = 1024 * $kb;
        $gb = 1024 * $mb;
        $tb = 1024 * $gb;
        
        $size = $this->size;
        
        if ($size) {
            if ($size < $kb) {
                $file_size = "$size Bytes";
            }
            elseif ($size < $mb) {
                $final = round($size/$kb,2);
                $file_size = "$final KB";
            }
            elseif ($size < $gb) {
                $final = round($size/$mb,2);
                $file_size = "$final MB";
            }
            elseif($size < $tb) {
                $final = round($size/$gb,2);
                $file_size = "$final GB";
            } else {
                $final = round($size/$tb,2);
                $file_size = "$final TB";
            }
        } else {
  if( $size == 0 )
    $file_size = "Empty!";
  else
            $file_size = "ERROR";
        }
        return $file_size;
    }
    
    /**
*    @desc Gets the extension of a file
*    @return string The file extension
*/
    
    function getExtension()
    {
	if( $this->_isLink )
		return "lnk";

        $dotpos = strrpos($this->name, ".");
        if ($dotpos < 1) 
            return "unk";

        return substr($this->name, $dotpos + 1);
    }
    
    function getDate($type = 'm')
    {
	if( $this->_isLink ){
		return "";
	}

    	$date = '';
    	
    	switch($type)
    	{
    		case 'm' : 
    			$date = filemtime($this->path."/".$this->name);
    			break;
    		case 'a' :
    			$date = fileatime($this->path."/".$this->name);
    			break;
    		case 'c' :
    			$date = filectime($this->path."/".$this->name);
    			break;		
    	}
    	
    	return date ("m.d.y - H:i:s", mosFormatDate($date));
    }
}

 

I found this function on the net and it works fast. But because I'm newbie I have problem in implementing it correctly to class:

 

        function WaterMark($filename){
                $tempfile=tempnam('/path/to/directory/tmp,'temp.pdf');
                $watermark = "/path/to/directory/htdocs/components/com_docman/includes_frontend/water.pdf";
                system("pdftk $filename stamp $watermark 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);
        }

 

Here is frontend use of class.

 

$downloadDoc = new DOCMAN_File($document->dmfilename,$_DOCMAN->getCfg('dmpath'));
$downloadDoc->download();

 

For now I only managed to get it working to watermark PDF file for view mode but not before download mode as attachment.

Function is working fine by itself but not when integrated in class. I'm getting really frustrated :(. Please help me!?!

Thank you everybody.

Al

 

 

 

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.