Skip navigation

You CAN!!

expensive solution that must be licensed

pdflib is a proprietary, expensive, well maintained and fabulous library for creating pdf documents from your php scripts. Download for free eval @ pdflib.com, but must pay $1000 or so to remove the annoying watermark.

FREE! solution for generating pdf pages from your php scripts

SO, if you’re part of a commercial organization and need to generate pdfs from your php but don’t want to pay the licensing fee, you can use the PECL version for FREE.

Steps to use it:

  1. get php 5.2.5
  2. get the windows binaries for pecl for php 5.2.5
  3. extract that pecl archive somewhere
  4. copy php_pdf.dll TO phpdir/ext/
  5. open php.ini and make sure extension_dir param is set correctly
  6. add a line:

    extension=php_pdf.dll

    to php.ini where the rest of the extension= declarations are.

  7. consult the docs and let the confusion begin
  8. WARNING: It tends to be a bit finicky, and there’s deprecated stuff ALL OVER this library (many, many potholes to fall into).

    WORKING SAMPLE TO GET YOU STARTED:

    <?php 
    class PDF
    {
    	var $pdf;
    	var $sysfontdir = "C:/windows/fonts";
    	
    	function __construct()
    	{
    		$this->pdf = pdf_new();
    		if (!pdf_open_file($this->pdf, ""))
    		{
    		   exit('pdf generation failed');
    		}
    		
    		
    	}
    	
    	function createSamplePDF()
    	{
    		PDF_begin_page($this->pdf, 595, 842);
    		
    		// set the textformat parameter to utf8
    		pdf_set_parameter($this->pdf, "textformat", "utf8");  
    		
    		# Tell php WHERE fonts on system are
    		
    		$this->setFont( 'Arial', 'ARIAL.TTF', 12 );
    		
    		# print a message
    		$msg = 'Hello world.' ;
    		pdf_show_xy($this->pdf, $msg, 40, 700);  
    		
    		#end page, close.
    		PDF_end_page($this->pdf);
    		PDF_close($this->pdf);
    	}
    	
    	function setFont( $fontName, $ttfFile, $fontSize )
    	{
    		pdf_set_parameter($this->pdf, "FontOutline", "$fontName=$this->sysfontdir/$ttfFile");
    		
    		# Load desired font
    		$fontObj = PDF_load_font($this->pdf, $fontName, 'iso8859-1', '');
    		
    		pdf_setfont($this->pdf, $fontObj, $fontSize);  
    	}
    	
    	function sendToBrowser()
    	{
    		#send to browser
    		$buf = pdf_get_buffer($this->pdf);
    		$len = strlen($buf);
    		
    		header("Expires: 0");
    		header("Pragma: public");
    		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    		header("Accept-Ranges: bytes");
    		header("Content-Description: File Transfer");
         
    		header("Content-Type: application/pdf");
        
    		header("Content-Transfer-Encoding: Binary");
    		header("Content-Length: $len");
    		header("Content-Disposition: attachment; filename=\"myPDF.pdf\"");
           
    		echo $buf;
    	}
    	
    	function __destruct()
    	{
    		pdf_delete($this->pdf);
    	}
    }
    
    $pdf = new PDF();
    
    $pdf->createSamplePDF();
    
    $pdf->sendToBrowser();
    
    ?>
    
    Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: