francerz/exfpdf

此包的最新版本(v1.0.29)没有可用的许可信息。

免费的PDF扩展库

v1.0.29 2023-07-21 20:55 UTC

README

Packagist Build Status

此库扩展了FPDF类的基功能。

FPDF是一个PHP类,允许使用纯PHP生成PDF文件。FPDF中的F代表Free:您可以使用它进行任何类型的用途,并根据需要修改它。

安装

安装ExFPDF最简单和最优的方法是使用Composer

如果您已经使用Composer,只需运行以下命令即可完全安装。

composer require francerz/ExFPDF

扩展功能

以PSR-7 ResponseInterface输出结果

ExFPDF允许将生成的PDF输出到PSR-7兼容的ResponseInterface对象。依赖于PSR-17 Http Factories来创建实例。

OutputPsr7(
  \Psr\Http\Message\ResponseFactoryInterface $responseFactory,
  \Psr\Http\Messsage\StreamFactoryInterface $streamFactory,
  string $filename,
  bool $inline = true,
  bool $isUTF8 = false
)

使用示例

$pdf = new ExFPDF();
// ... create PDF

// ... load HTTP Factories
$responseFactory = new ResponseFactory(); // from a PSR-17 compliant library
$streamFactory = new StreamFactory(); // from a PSR-17 compliant library

// Output PSR-7 file
$response = $pdf->OutputPsr7($responseFactory, $streamFactory, 'my-file.pdf');

替代简化方法 OutputPsr7WithManager

或者您可以使用HttpFactoryManager来处理多个工厂实例,以减少参数,提高可重用性。

OutputPsr7WithManager(
  \Francerz\Http\Utils\HttpFactoryManager $hfm,
  string $filename,
  bool $inline = true,
  bool $isUTF8 = false
)

使用示例

$pdf = new ExFPDF();
// ... create PDF

// Output PSR-7 Response object
$factories = new HttpFactoryManager(new ResponseFactory(), new StreamFactory());
$response = $pdf->OutputPsr7WithManager($factories, 'my-file.pdf');
使用家庭HTTP库更简单

在以下示例中使用了简化版本的家庭HTTP库 francerz/http,该库符合PSR-7、PSR-17和PSR-18。

$pdf = new ExFPDF();
// ... create PDF

// Output PSR-7 Response object
$response = $pdf->OutputPsr7WithManager(HttpFactory::getManager(), 'my-file.pdf');

相对定位和尺寸

允许使用X、Y、Width和Height作为当前页面尺寸的百分比。

// Sets Y position at 25% (one quarter) from page top.
$pdf->SetY('25%');
// Sets X position at 25% (one quarter) from page left.
$pdf->SetX('25%');

// Draws a cell with 50% width and height of current page size.
$pdf->Cell('50%','50%', '', 1);

定位和尺寸也可以相对于页面内容区域,在边距内。

// Sets Y position at top margin.
$pdf->SetY('~0');
// Sets X position at left margin.
$pdf->SetX('~0');

// Draws a cell with 25% width and 10% height of current page content.
$pdf->Cell('~25%', '~10%', '', 1);

因此,您可以使用方法CalcX($x)CalcY($y)CalcWidth($w)CalcHeight($h)来获取计算值。

偏移定位

允许增加或减少当前位置。

// translates pointer 10 units right to current X.
$pdf->OffsetX(10);

// translates pointer 20 units bottom to current Y.
$pdf->OffsetY(20);

// translates equivalent to two previous in a single line.
$pdf->OffsetXY(10, 20);

// offset may be negative and relative units
$pdf->OffsetXY(-10, '~10%');

坐标固定

可以按名称固定坐标。

MoveToPin($pinName, $axis = 'XY', $offset = 0, $offsetY = 0);
// Defines a coordinate pin at current X,Y with name 'start'.
$pdf->SetPin('start');

// Retrieves 'start' pin positions.
$x = $pdf->GetPinX('start');
$y = $pdf->GetPinY('start');

// Moves pdf position back to pin 'start'
$pdf->MoveToPin('start');

// Moves pdf X position back to pin 'start'
$pdf->MoveToPin('start','X');

// Moves pdf Y position back to pin 'start'
$pdf->MoveToPin('start','Y');

// Moves pdf X position back to pin 'start' and adds 10 units.
$pdf->MoveToPin('start', 'X', 10);

// Moves pdf Y position back to pin 'start' and adds 20 units.
$pdf->MoveToPin('start', 'Y', 20);

// Moves pdf position back to pin 'start' and adds X: 10 units, Y: 20 units.
$pdf->MoveToPin('start', 'XY', 10, 20);

基于字体大小的相对单元格高度

SetLineHeight(float $size)

定义基于当前字体大小的自动行高大小。

$pdf->SetFontSize(10);
$pdf->SetLineHeight(1.1); // 110% of actual size (11pt)

$pdf->Cell('100%', null, 'This text is size 10pt, but Cell is 11pt height with LineHeight 1.1');

简化内容编码

SetSourceEncoding(string $encoding)

允许在不写入每个单元格的情况下内部解码字符串。

$pdf->SetSourceEncoding('UTF-8');
$pdf->Cell('100%', null, 'Benjamín pidió una bebida de kiwi y fresa; Noé, sin vergüenza, la más exquisita champaña del menú.');

右对齐单元格

CellRight($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $margin=0)

将单元格放置在页面右侧边缘对齐。可选地,可以将$margin设置为从右侧边缘移动单元格。

$pdf->CellRight(15, 5, 'Date: ', 0, 0, 'R', false, '', 30);
$pdf->CellRight(30, 5, date('Y-m-d'), 1, 1, 'C', false, '', 0);

页眉和页脚

允许使用匿名函数定义页眉和页脚。

  • SetHeader(callable $headerFunc, $headerHeight = null)
    设置一个可调用的函数,当页眉加载时将执行。如果参数$headerHeight为null,则将进行计算,并将内容替换。如果已设置,则内容将替换为$headerHeight加上顶部边距。
  • SetFooter(callable $footerFunc, $footerHeight = null)
    设置一个可调用的函数,当页脚加载时将执行。如果参数$footerHeight为null,则将进行计算,并在达到页脚内容之前执行分页操作。如果已设置,则页脚将从页面底部边缘开始移动。
$userName = "My Name";

$pdf = new ExFPDF();
$pdf->AliasNbPages();
$pdf->SetFont('Arial', '', 12);
$pdf->SetHeader(function(ExFPDF $exfpdf) use ($userName) {
    $exfpdf->Cell('~100%', 10, "Hello {$userName}", 1);
});
$pdf->SetFooter(function(ExFPDF $exfpdf) {
    $exfpdf->Cell('~100%', 10, 'Page '.$exfpdf->PageNo(), 1, 0, 'R');
});
$pdf->AddPage();

$pdf->Output('I');

注意
在调用AddPage()之前,必须调用SetHeader()SetFooter()

表格

PDF表格创建简化,并在单元格溢出到多个页面时自动溢出。

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

// Creates table with three columns with given widths
$table = $pdf->CreateTable(['~25%','~60%','~15%']);

// Sets Line Height as its used to extend cell size.
$pdf->SetLineHeight(1.2);

// Set header styling
$pdf->SetFont('','B', 14);
$pdf->SetFillColor('#CA4A0F');
$pdf->SetTextColor('#FFF');

// Creates a row with heading
$row = $table->AddRow();
$row->Cell('Price', $align='C', $fill=true, $colspan=1, $rowspan=2);  // 2 rows tall
$row->Cell('Product', $align='C', $fill=true, $colspan=2);            // 2 colums wide
$row = $table->AddRow();
$row->CellSpan($fill=true); // $rowspan still not supported this is placeholder
$row->Cell('Description', $align='C', $fill=true);
$row->Cell('Quantity', $align='C', $fill=true);

// Creates content rows
$pdf->SetFont('', '', 12);
$pdf->SetTextColor('#000');

$row = $table->AddRow();
$row->Cell('$ 340.00', $align='R');
$row->Cell('Gymbal');
$row->Cell('3', 'C');
$row = $table->AddRow();
$row->Cell('$ 970.00', $align='R');
$row->Cell('Laptop');
$row->Cell('1', 'C');
$row = $table->AddRow();
$row->Cell('$ 120.00', $align='R');
$row->Cell('Headphones');
$row->Cell('2', 'C');

// Draws all borders in table
$table->DrawBorders();

条码支持

$pdf->barcode128(string $code, $w, $h, $x = '0+', $y = '0+');

使用barcode128将给定的ASCII字符串$code放置在给定的$x$y位置。并具有给定的$w(宽度)和$h(高度)。这些尺寸与相对定位和尺寸兼容。

如果没有设置$x$y,则将使用当前的PDF位置。

$pdf->barcode39(string $code, $w, $h, $x, $y);

使用barcode39将给定的$code(字符串[-0-9A-Z. *$/+%])放置在给定的$x$y位置,宽度为$w(宽度),高度为$h(高度)。这些尺寸与相对定位和尺寸兼容。

支持QR码和数据矩阵码

$pdf->QrCode(string $data, $size, $level = 'H');
$pdf->DataMatrix(string $data, $size);
$matrix = $pdf->GetQrCodeMatrix(string $data, $level = 'H');
$pdf->DrawBinaryMatrix($matrix, $size);
$matrix = $pdf->GetDataMatrixMatrix(string $data);
$pdf->DrawBinaryMatrix($matrix, $size);