from-home-de/pdf-label

TCPDF的PDF标签包装器

v1.1.0 2021-06-08 08:56 UTC

This package is auto-updated.

Last update: 2024-09-15 19:11:24 UTC


README

pdf-label

PDF标签

这是一个TCPDF的标签包装器。

灵感来源: https://github.com/Uskur/PdfLabel

用法

此类扩展了TCPDF类,其API在此描述

通过预定义标签类型

<?php declare(strict_types=1);

use FromHome\Pdf\Label;

$pdf = Label::newFromLabelType(Label::TYPE_L7161);

for($i= 0; $i < $pdf->getLabelsCount(); $i++)
{
    $pdf->addLabel('Label text ' . $i);
}

$pdf->save('/path/to/Labels.pdf');

查看标签类中的LABELS类常量,了解所有可用的预定义格式。

通过自定义格式定义

<?php declare(strict_types=1);

use FromHome\Pdf\Label;

$formatDefinition = [
    'paper-size' => 'A4',
    'unit'       => Label::UNIT_MILLIMETER,
    'marginLeft' => 7,
    'marginTop'  => 10.5,
    'NX'         => 2,
    'NY'         => 2,
    'SpaceX'     => 0,
    'SpaceY'     => 0,
    'width'      => 98,
    'height'     => 138,
    'cutLines'   => true,
];

$pdf = Label::newFromFormatDefinition($formatDefinition);

for($i= 0; $i < $pdf->getLabelsCount(); $i++)
{
    $pdf->addLabel('Label text ' . $i);
}
$pdf->save('/path/to/Labels.pdf');

将PDF保存为文件

$pdf->save('/path/to/Labels.pdf');

在浏览器中内联服务PDF

$pdf->serveInline('Labels.pdf');

在浏览器中作为下载服务PDF

$pdf->serveAsDownload('Labels.pdf');

获取PDF内容的字符串

$content = $pdf->getContent();

自定义

您可以通过从标签类扩展来自定义文档设置、页眉和页脚,如下所示

<?php

use FromHome\Pdf\Label;

final class MyLabel extends Label
{
    public function setUpDocument() : void
    {
        # setup stuff like default font and font-size here         
    }

    public function Header() : void
    {
        # Create a custom header here
        # See: https://tcpdf.org/examples/example_003/
    }
    public function Footer() : void
    {
        # Create a custom footer here
        # See: https://tcpdf.org/examples/example_003/
    }
}