Thursday, 11 July 2013

Creating PDF files using PHP

Create PDF files using your own PHP code


Welcome ,

I'm sure you would have struggled to convert your Document(doc) file into Portable Document File(pdf) by searching for a converter software and other mean.

Why don't you create a PDF file by your own .

YES you can do it using PHP , want to try ??? Its very simple !!!!!! 

For this purpose we make use of  FPDF --->FPDF is a PHP class using which you can  generate PDF files with pure PHP, that is without using the PDFlib library where F stands for Free

FPDF has a lot of advantages and excellent features .

You can download the FPDF files  and  from here .

To get some more  idea about the functions below , you can refer this manual



Here goes the code !!!!!!

PDF Creation using PHP

AddPage(), convert doc file into pdf, Creating PDF files using PHP, FPDF, Image(), MultiCell(), PageNo(), PDF, PHP, SetFont(), SetY(),

 

/********************************************************************/
 <?php

require('fpdf.php');
class wetechiesPDF extends FPDF {

function Header() {
    $this->SetFont('Times','B',18); //prototype of the setFont function is SetFont(string family [, string style [, float size]])
    $this->SetY(0.5);// Moves the current abscissa back to the left margin and sets the ordinate. If the passed value is negative, it is relative to the bottom of the page.
    $this->Cell(0, .25, "wetechies ".$this->PageNo(), 'T', 2, "R");
    //reset Y
    $this->SetY(1);
}

function Footer() {
//This is the footer; it's repeated on each page.

    $this->Image("wetechies.jpg", (8.5/2)-1.5, 9.8, 3, 1, "JPG", "http://wetechies.blogspot.in");//prototype --> Image(string file [, float x [, float y [, float w [, float h [, string type [, mixed link]]]]]])
}

}

//class instantiation
$pdf=new wetechiesPDF("P","in","A4");//prototype-->
//FPDF([string orientation [, string unit [, mixed size]]]) here P - portrait ,  //in - inches , A4 - size
$pdf->SetMargins(1.5,1.5,1.5);

$pdf->AddPage();
$pdf->SetFont('Times','I',16);

$wetechies1="Group of techies have come together to share there experience in various domains.";
 
$wetechies2="Wetechies is a group of techies post about some technology/domain in
which they are interested, any new thing they learn they will share here ,";
 
$wetechies3 ="its not related to single techi but any one can post here if you want to
share your knowledge and express yourself.";
 
$pdf->SetFillColor(240, 100, 100);

$pdf->SetFont('Times','BU',12);
 

$pdf->Cell(0, .25, "wetechies", 1, 2, "C", 1);
 
$pdf->SetFont('Times','B',12);

//MultiCell(float width, float height, string txt [, mixed border [, string align [, boolean fill]]])

$pdf->MultiCell(0, 0.25,$wetechies1, 1, "J");
$pdf->MultiCell(0, 0.25,$wetechies2, 1, "R");
$pdf->MultiCell(0, 0.25,$wetechies3, 1, "J");

$pdf->AddPage();

$pdf->Write(0.5, $wetechies1.$wetechies2.$wetechies3);
 
$pdf->Output();

?>

/********************************************************************/

You can download the source code  here

Please leave your comments and doubts and suggestions in the comments section :-)

4 comments:

  1. Replies
    1. Thanks dude :-)

      Here's another such post


      http://wetechies.blogspot.in/2013/07/creating-and-extracting-zip-files-using.html

      Delete
  2. Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this. We are most leading IT & Software company in India

    ReplyDelete
  3. Good day. I was impressed with your article. Keep it up . You can also visit my site if you have time. Thank you and Bless you always.
    Yii Framework Team in India

    ReplyDelete