KP Joy
KP Joy

Reputation: 525

Formatting in FPDF library in PHP

I am using FPDF library in PHP to generated PDFs. Please consider my following code which generates a PDF:

<?php
require("fpdf181/fpdf.php");
class PDF extends FPDF
{
    //Page header
    function Header()
    {

    }

    //Page footer
    function Footer()
    {

    }

}
$pdf = new PDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();

$pdf->SetFont('Arial','U',14);
$pdf->MultiCell(90, 8, "Student Details", 0, 'L');

$pdf->SetFont('Arial','B',12);
$pdf->Cell(32, 8, "Username: ");
$pdf->SetFont('Arial','',12);
$pdf->Cell(10, 8, 'abc123', 0, 1);

$pdf->SetFont('Arial','B',12);
$pdf->Cell(32, 8, "Name: ");
$pdf->SetFont('Arial','',12);
$pdf->Cell(10, 8, 'John', 0, 1);

$pdf->SetFont('Arial','U',14);
$pdf->MultiCell(90, 8, "Parent Details", 0, 'L');

$pdf->SetFont('Arial','B',12);
$pdf->Cell(32, 8, "Name: ");
$pdf->SetFont('Arial','',12);
$pdf->Cell(10, 8, 'Mathew', 0, 1);

$pdf->SetFont('Arial','B',12);
$pdf->Cell(32, 8, "Occupation: ");
$pdf->SetFont('Arial','',12);
$pdf->Cell(10, 8, 'Businessman', 0, 1);

$pdf->Output();

?>

Now it generates a PDF which looks like this:

Student Details
Username: abc123
Name: John
Parent Details
Name: Mathew
Occupation: Businessman

But I need output like this:

Student Details            Parent Details
Username: abc123           Name: Mathew
Name: John                 Occupation: Businessman

What changes should I make in my code to achieve this kind of formatting?

Upvotes: 0

Views: 1416

Answers (2)

picios
picios

Reputation: 250

Place your cells horizontally not vertically. Consider your page is 210mm wide, you may write:

<?php
require("fpdf181/fpdf.php");
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();

$pdf->SetFont('Arial','U',14);
$pdf->Cell(90, 8, "Student Details", 0, 'L');
$pdf->Cell(90, 8, "Parent Details", 0, 'L');

$pdf->Ln();

$pdf->SetFont('Arial','B',12);
$pdf->Cell(35, 8, "Username: ");
$pdf->SetFont('Arial','',12);
$pdf->Cell(55, 8, 'abc123', 0);

$pdf->SetFont('Arial','B',12);
$pdf->Cell(35, 8, "Name: ");
$pdf->SetFont('Arial','',12);
$pdf->Cell(55, 8, 'Mathew', 0);

$pdf->Ln();

$pdf->Output();

Upvotes: 1

Kashyap
Kashyap

Reputation: 391

TO achieve that you can use table method. Find working example here : http://www.fpdf.org/en/tutorial/tuto5.htm

Or try Simply this : (http://www.fpdf.org/en/script/script50.php)

<?php
    require('html_table.php');

    $pdf=new PDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','',12);

    $html='<table border="0">
        <tr>
            <td width="200" height="30">cell 1</td><td width="200" height="30" bgcolor="#D0D0FF">cell 2</td>
        </tr>
        <tr>
            <td width="200" height="30">cell 3</td><td width="200" height="30">cell 4</td>
        </tr>
    </table>';

    $pdf->WriteHTML($html);
    $pdf->Output();
?>

Upvotes: 0

Related Questions