我在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
我应该在代码中进行哪些更改才能实现这种格式化?
答案 0 :(得分:1)
将细胞水平放置而不是垂直放置。考虑你的页面宽210毫米,你可以写:
<?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();
答案 1 :(得分:0)
要实现这一点,您可以使用表方法。在此处查找工作示例: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();
?>