FPDF – Creating A Basic PDF

FPDF – Creating A Basic PDF

I recently introduced you to a wonderful piece of software name FPDF. If you have not downloaded and began playing with its capabilities you definitely should. As a Developer we must always be looking for new additions to our toolbox.

In order to hopefully entice you into trying FPDF I thought it might be a good idea to show you jest exactly how easy it is to create a basic Dynamic Pdf.

Download your preferred version of FPDF and unzip it into the preferred location.

*IMPORTANT make sure the folder containing FPDF as well as its subfolder have write access. Otherwise the pdf will crash while trying to save itself causing a possible troubleshooting headache.

>PHP Version

In the parent directory containing the PFDF folder create a file named mypdf.php

In mypdf.php write

<?php
    Require(‘fpdf.php’);
    $pdf = new FPDF(“p”, “mm”,”A4”);
    $pdf->AddPage();
    $pdf->SetFont(‘Arial’,’B’,16);
    $pdf->Cell(40,10,’My First PDF!’);
    $pdf->Output(“/public/mypdf.pdf”);
?>

Then open mypdf.php in your browser and voila your very first dynamic pdf.

>ASP Version

In the parent directory containing the PFDF folder create a file named mypdf.asp

In mypdf.asp write

<!--#include virtual=”/public/fpdf.asp”-->
<%
    Set pdf=CreateJsObject(“FPDF”)
    pdf.CreatePDF “P”, “mm”,”A4”
    pdf.SetPath(“fpdf/”)
    pdf.AddPage()
    pdf.SetFont “Arial”,”B”,16
    pdf.Open()
    pdf.cell 40,10,’My First PDF!’,1,0,,1
    pdf.Close()
    filewrite=server.MapPath(“/public/mypdf.pdf”)
    pdf.Output filewrite
 %>

Add comment