Typed Enums

Backed enums exposed by tc-lib-pdf and the companion packages, and how they coexist with the string API

Options that used to be documented as “magic strings” — page units, conformance modes, alignment codes, blend modes, barcode types, filter names, signature profiles — are now also available as backed enums. The enums make the accepted values discoverable from the IDE and checkable by static analysis, without breaking any existing code.

The enum-with-union-type Pattern

Every affected parameter is declared as a string|Enum union, and the backing value of each enum case is exactly the string that was accepted before:

enum Unit: string
{
    case Point = 'pt';
    case Millimeter = 'mm';
    case Centimeter = 'cm';
    case Inch = 'in';
}

That means the two calls below are equivalent, and a codebase can migrate one call site at a time:

use Com\Tecnick\Pdf\Page\Unit;

$pdf = new \Com\Tecnick\Pdf\Tcpdf(unit: 'mm');
$pdf = new \Com\Tecnick\Pdf\Tcpdf(unit: Unit::Millimeter);

Where an option is passed inside a configuration array rather than as a typed parameter (for example the signature()->configure() options), the array still carries strings: use Enum::Case->value to reference them symbolically.

tc-lib-pdf

EnumValuesUsed for
\Com\Tecnick\Pdf\PdfConformance'', pdfa1, pdfa1a, pdfa1b, pdfa2, pdfa2a, pdfa2b, pdfa2u, pdfa3, pdfa3a, pdfa3b, pdfa3u, pdfx, pdfx1a, pdfx3, pdfx4, pdfx5, pdfua, pdfua1, pdfua2The mode constructor argument. See /docs/standards/.
\Com\Tecnick\Pdf\TextHAlignL, C, R, JHorizontal text alignment in cells.
\Com\Tecnick\Pdf\TextVAlignT, C, B, A, L, DVertical text alignment in cells (top, center, bottom, ascent, baseline, descent).
\Com\Tecnick\Pdf\TextFitMode'', T, S, FOverflow handling: off, truncate, stretch, shrink font.
\Com\Tecnick\Pdf\DisplayZoomfullpage, fullwidth, real, defaultViewer zoom preference.
\Com\Tecnick\Pdf\AFRelationshipSource, Data, Alternative, Supplement, UnspecifiedEmbedded-file relationship (Factur-X / ZUGFeRD).
\Com\Tecnick\Pdf\Cache\CacheTypefont, imageCacheable subsystems. See /docs/cache/.
\Com\Tecnick\Pdf\Signature\SignatureAppearanceModeN, R, DSignature appearance stream (normal, rollover, down).
\Com\Tecnick\Pdf\Signature\ExternalSignatureEncodingbinary, base64, hexPayload encoding for external/HSM signing.

Text direction is expressed with \Com\Tecnick\Unicode\TextDirection ('' auto, R, L), accepted by the forcedir argument of the text methods.

Companion Packages

PackageEnums
tc-lib-pdf-pageUnit, Orientation, PageBoxType, PageLayout, PageDisplayMode, TransparencyGroupMode
tc-lib-pdf-graphBlendMode, PathPaintOp
tc-lib-pdf-fontFontType
tc-lib-pdf-filterFilterType
tc-lib-pdf-signSignatureProfile, DigestAlgorithm
tc-lib-colorColorModelType
tc-lib-barcodeBarcodeType, QrEccLevel, QrEncodingMode, DatamatrixShape, DatamatrixEncoding, AztecHint, AztecRange
tc-lib-unicodeTextDirection
tc-lib-unicode-dataBidiClass

Example

use Com\Tecnick\Barcode\BarcodeType;
use Com\Tecnick\Pdf\PdfConformance;
use Com\Tecnick\Pdf\TextHAlign;
use Com\Tecnick\Pdf\TextVAlign;
use Com\Tecnick\Pdf\Page\Unit;

$pdf = new \Com\Tecnick\Pdf\Tcpdf(
    unit: Unit::Millimeter,
    mode: PdfConformance::Pdfua1,
);

$pdf->addTextCell(
    txt: 'Hello, PDF!',
    posx: 15,
    posy: 20,
    width: 90,
    height: 10,
    valign: TextVAlign::Center,
    halign: TextHAlign::Left,
);

$barcode = new \Com\Tecnick\Barcode\Barcode();
$bobj = $barcode->getBarcodeObj(BarcodeType::QRCODE, 'https://tcpdf.org');

Migration Guidance

  • No migration is required: string arguments remain supported and are not deprecated.
  • Prefer enums for new code and for options with a small closed set of values; they turn a typo into a compile-time or static-analysis error instead of a runtime exception.
  • When comparing a stored option against an enum, compare the backing values ($mode === PdfConformance::Pdfua1->value), because the libraries normalize typed parameters to their string form internally.

Previous: /docs/development/

Overview: /docs/

Next: /docs/remote-resources/

 

© 2004-2026 – Nicola Asuni - Tecnick.com - All rights reserved.
sponsor - legal notice - privacy