Hi

Can anyone tell me how to do the force download in FPDF ?

I can create a pdf file and save it with

output($filename,"F");

This works great. But when I put D instead of F for the download I get an error.

Do I need to put something in the header of the document ?

Please help

    U know that FPDF is a lib made in php that creates PDF, I know couse I made a long time ago some PDF's using that lib, but how many people do u think use this lib?

    As I remember this lib has a good documentation, why dont u read it or if u dont understand post the part of the documentation about the output method of that FPDF class and maybe someone will explain u what that method do ...

      Thanks for the reply Bogu

      I'm trying to create a download of a pdf file. On my web site I am showing information and would like the visitor to be able to download the information as a pdf file.

      So when they click "download this information" the fpdf creates a pdf file and forces a download. I can create the file but I'm stuck at the command "output".

      It is made of 2 parts
      string Output([string name [, string dest]])

      Destination is

      I: send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
      D: send to the browser and force a file download with the name given by name.
      F: save to a local file with the name given by name.
      S: return the document as a string. name is ignored.

      When I do Output($filename,"F") it is saved on the server but when I do "D" nothing happens.

      I've found this function for outputs with different browsers.

      function output($filename)
      {
      if ($this->_state < 3) { // If document not yet closed
      $this->close(); // close it now.
      }

      /* Make sure no content already sent. */ 
      if (headers_sent()) { 
          die('Unable to send PDF file, some data has already been output to browser.'); 
      } 
      
      /* Offer file for download and do some browser checks 
       * for correct download. */ 
      $agent = trim($_SERVER['HTTP_USER_AGENT']); 
      if ((preg_match('|MSIE ([0-9.]+)|', $agent, $version)) || 
          (preg_match('|Internet Explorer/([0-9.]+)|', $agent, $version))) { 
          header('Content-Type: application/x-msdownload'); 
          Header('Content-Length: ' . strlen($this->_buffer)); 
          if ($version == '5.5') { 
              header('Content-Disposition: filename="' . $filename . '"'); 
          } else { 
              header('Content-Disposition: attachment; filename="' . $filename . '"'); 
          } 
      } else { 
          Header('Content-Type: application/pdf'); 
          Header('Content-Length: ' . strlen($this->_buffer)); 
          Header('Content-disposition: attachment; filename=' . $filename); 
      } 
      echo $this->_buffer; 

      }

      The manual explains about headers (and footers) for the page content not headers for the file types etc.

      I need help with this please.

        Well, your output method dont have the second param, so is not good, maybe your fpdf lib is a later version or it has been modified, here is the output method from fpdf ...

        function Output($name='',$dest='')
        {
        	//Output PDF to some destination
        	global $HTTP_SERVER_VARS;
        
        //Finish document if necessary
        if($this->state<3)
        	$this->Close();
        //Normalize parameters
        if(is_bool($dest))
        	$dest=$dest ? 'D' : 'F';
        $dest=strtoupper($dest);
        if($dest=='')
        {
        	if($name=='')
        	{
        		$name='doc.pdf';
        		$dest='I';
        	}
        	else
        		$dest='F';
        }
        switch($dest)
        {
        	case 'I':
        		//Send to standard output
        		if(isset($HTTP_SERVER_VARS['SERVER_NAME']))
        		{
        			//We send to a browser
        			Header('Content-Type: application/pdf');
        			if(headers_sent())
        				$this->Error('Some data has already been output to browser, can\'t send PDF file');
        			Header('Content-Length: '.strlen($this->buffer));
        			Header('Content-disposition: inline; filename='.$name);
        		}
        		echo $this->buffer;
        		break;
        	case 'D':
        		//Download file
        		if(isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) and strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'],'MSIE'))
        			Header('Content-Type: application/force-download');
        		else
        			Header('Content-Type: application/octet-stream');
        		if(headers_sent())
        			$this->Error('Some data has already been output to browser, can\'t send PDF file');
        		Header('Content-Length: '.strlen($this->buffer));
        		Header('Content-disposition: attachment; filename='.$name);
        		echo $this->buffer;
        		break;
        	case 'F':
        		//Save to local file
        		$f=fopen($name,'wb');
        		if(!$f)
        			$this->Error('Unable to create output file: '.$name);
        		fwrite($f,$this->buffer,strlen($this->buffer));
        		fclose($f);
        		break;
        	case 'S':
        		//Return as a string
        		return $this->buffer;
        	default:
        		$this->Error('Incorrect output destination: '.$dest);
        }
        return '';
        } 

        I attache u the fpdf.php file here ...

          Thanks Bogu

          Only just realised that the header information is already in the fpdf.php file. I thought I had to add them to the script.

          But still it wont work. Have tried D, I, and F but only F will work.

          Tried the 1.52 version that you sent me (I was using 1.53) Now I'm getting

          Warning: setfont(helvetica.php): failed to open stream

          on line 551.

          Shall stick with it as it's only been 3 days, so whats a few more eh ?

          Edit: Just had a thought at the start of the php file Im using to generate the pdf file I'm using

          class PDF extends FPDF
          {
          //Page header
          function Header()
          {
          and then function Footer(). these are only to put my logo on the pages.

          Does this effect the Header function in the fpdf.php script ?

          Still couldn't get your 1.52 version to work so Returned to 1.53.

            Getting closer

            Started with the Hello World example again ! (start from basics and all that) Then moved onto lesson 2 (again). These can be downloaded no problem with Output($filename,"D").

            So me thinks its my script (joke)

            2 things: I have it on a php page with a template and mysql. So I have head and body tags in my document.

            Ive tried putting the fpdf stuff in the body with the mysql search and the output() outside at the very end. Unfortunately all the examples on the fpdf website are straight forward php scripts without real world tags and templates

              OK sorted

              Thanks for your help (again) bogu.

              It was the tags in the script. I was using dreamweaver and inserting a php script as per the manual. Took out all the head, body and html page stuff and all the php breaks. So I ended up with just one continuous php script.

              Works great now. Now to see if I need to .zip or .sit the file before sending it.

                GL coding...
                Remember to mark thread as RESOLVED ...

                  Write a Reply...