Adding text to PDF file download

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Adding text to PDF file download

    I was wondering if its possible to add text on the fly on a PDF download when using the below code, I've been unable to get it to work, I've tried echoing it and adding it to the var that the files being stored to with no luck, any ideas?
    Code:
    $chunksize = 1*($fileChunkSize);
    $bytes_send = 0;
    if ($file = fopen($file, 'r')) {
    	if(isset($_SERVER['HTTP_RANGE']))
    	fseek($file, $range);
    
    	while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length)) {
    		$buffer = fread($file, $chunksize);
    		print($buffer);
    		flush();
    		$bytes_send += strlen($buffer);
    	}
    	fclose($file);
    } else die('Error - can not open file.');
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Do you want to create new PDF file or do you want to add text to existing file?

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Its to add to an existing PDF, the above script allows the file to be downloaded but I need to be able to add a disclaimer to files downloaded.
      The best option if possible would be to be able to add a page at the begining of the PDF with the disclaimer info!?

      Comment

      • ziycon
        Contributor
        • Sep 2008
        • 384

        #4
        Anyone any ideas on how to go about this?

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          Search this forum, i have answered this question several time

          Comment

          • ziycon
            Contributor
            • Sep 2008
            • 384

            #6
            I've been unable to find the topic your referring to, I've search under your posts/replies and by pdf but unable to find it, any chance you could link me please?

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              Code:
              <?php
              	session_start();
              	require_once('webapp/lib/globalfunctions/common.function.php');
              	function ShowFileName($filepath)
                  {
                      preg_match('/[^?]*/', $filepath, $matches);
                      $string = $matches[0];
                      #split the string by the literal dot in the filename
                      $pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
                      #get the last dot position
                      $lastdot = $pattern[count($pattern)-1][1];
                      #now extract the filename using the basename function
                      $filename = basename(substr($string, 0, $lastdot-1));
                      #return the filename part
                      return $filename;
                  } 
              	$res=Varification();
              	if($res==false)
              	{
              		echo "Authentication failed";
              		exit;
              	}
              	if($_GET['filename']==NULL)
              	{
              		echo "Error File name";
              		exit;
              	}
               
              	$filename=$_GET['filename']; 
              	
              	$rfilename = $filename; 
              	$base=ShowFileName($filename);
              
              	$file_extension = substr(strrchr($rfilename,"."),1);
              	
              	switch( $file_extension )
              	{
              		case "pdf": $ctype="application/pdf"; break;
              		case "exe": $ctype="application/octet-stream"; break;
              		case "zip": $ctype="application/zip"; break;
              		case "doc": $ctype="application/msword"; break;
              		case "xls": $ctype="application/vnd.ms-excel"; break;
              		case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
              		case "gif": $ctype="image/gif"; break;
              		case "png": $ctype="image/png"; break;
              		case "jpe": 
              		case "jpeg":
              		case "jpg": $ctype="image/jpg"; break;
              		default: $ctype="application/force-download";
              	}
              	
              	header("Pragma: public"); // required
              	header("Expires: 0");
              	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
              	header("Cache-Control: private",false); // required for certain browsers
              	header("Content-Type: $ctype");
              	header("Content-Disposition: attachment; filename=".$base.";" );
              	header("Content-Transfer-Encoding: binary");
              	header("Content-Length: ".@filesize($rfilename));
              	@readfile($rfilename);
              	unlink($rfilename);
              This is the code i use to force the browser to open save option instead of built in action

              Comment

              Working...