在 PHP 中的 FPDF 库中格式化

Formatting in FPDF library in PHP

我正在使用 PHP 中的 FPDF 库生成 PDF。请考虑我的以下生成 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();

?>

现在它生成的 PDF 如下所示:

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

但我需要这样的输出:

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

我应该对我的代码进行哪些更改才能实现这种格式设置?

要实现,您可以使用 table 方法。在此处查找工作示例:http://www.fpdf.org/en/tutorial/tuto5.htm

或者尝试简单地这样做:(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();
?>

水平放置单元格,而不是垂直放置。考虑到你的页面是 210mm 宽,你可以这样写:

<?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();