Optional module · pdftoolkit/html-to-pdf 0.4.0
Explicit inputs
The library accepts UTF-8 HTML and optional CSS strings. HtmlSource::stylesheet
is an author stylesheet. Inline style, HTML style elements and linked styles
participate in the documented cascade. An external stylesheet is a lookup in
ResourceCatalog, never an implicit request.
use PDF\HtmlToPdf\ConversionOptions;
use PDF\HtmlToPdf\Converter;
use PDF\HtmlToPdf\HtmlSource;
use PDF\HtmlToPdf\Layout\PageSettings;
use PDF\HtmlToPdf\Resource\ImageAsset;
use PDF\HtmlToPdf\Resource\ResourceCatalog;
use PDF\HtmlToPdf\Style\Edges;
$resources = new ResourceCatalog(
images: ['cover.png' => new ImageAsset($pngBytes)],
stylesheets: ['print.css' => 'p { color: #176a80; }'],
);
$options = new ConversionOptions(
fonts: $fonts,
page: new PageSettings(width: 595.275590551, height: 841.88976378,
margin: new Edges(36, 36, 36, 36)),
resources: $resources,
);
$result = (new Converter())->convert(new HtmlSource($html), $options);
$fonts is an explicit FontSet as in the README. $pngBytes and $html are
caller-supplied bytes. Image keys remain exact, case-sensitive names. Stylesheet
references are resolved to catalog keys as described below. A URL-shaped key
works only when explicitly present in the catalog: resolving a CSS reference
never performs HTTP, filesystem or data: loading. Repeated image uses share
their PDF embedding. alt="" explicitly marks an image decorative; provide
useful alternative text for meaningful figures.
ASCII http, https and mailto text links become PDF URI annotations with
structure references and are not followed during conversion. Relative targets
require an explicit HtmlSource::baseUri; their fragment stays in the external
URI. A relative target without a supported base is rejected. Internal destinations,
JavaScript actions and form submission are outside this release's profile.
Stylesheet imports and source identity
@import "colors.css"; and @import url("colors.css") print; load only bytes
already registered in ResourceCatalog::stylesheets. Imports must precede other
rules at the top level of their sheet. Their rules precede the importing sheet's
own rules, while specificity and !important keep their normal precedence.
A repeated import is processed again at its new source position.
$resources = new ResourceCatalog(stylesheets: [
'https://example.invalid/styles/print.css' =>
'@import "colors.css"; p { font-size: 12pt; }',
'https://example.invalid/styles/colors.css' => 'p { color: #176a80; }',
]);
$source = new HtmlSource(
'<link rel="stylesheet" href="../styles/print.css"><p>Catalog-only CSS.</p>',
baseUri: 'https://example.invalid/documents/report.html',
);
$result = (new Converter())->convert($source,
new ConversionOptions(fonts: $fonts, resources: $resources));
HtmlSource::baseUri is optional and applies to stylesheet references and text
link targets, not image keys. A linked sheet becomes the base for its own imports;
embedded style and HtmlSource::stylesheet use the supplied document base.
Without a base, relative names address the catalog root. Dot segments are
normalized, fragments are excluded from stylesheet identity and percent-encoded
path characters stay encoded. The resulting key must exist exactly in the
catalog. These identifiers do not grant access to their apparent URL scheme.
Only the supported print/all media profile activates an import. A screen-only import is not looked up. An active cycle is rejected by default; Report records it and continues with later rules. Misplaced imports are likewise rejected or reported and skipped. Missing resources fail in both modes. Import counts, depth and cumulative CSS bytes remain bounded, including repeated imports; inactive imports still consume the import-count budget.
Pages, fonts and CSS
Page geometry is expressed in points. A4 with 36-point margins is the default.
The CSS conversion is 1px = 0.75pt; millimetres, centimetres, inches, points and
supported font-relative values are converted before layout. Page size/margins
are configured by PageSettings, not @page in this release.
Supply static TrueType glyf fonts with embedding rights. Font parsing enforces
embedding restrictions and explicit FontLimits. Actual variants are required;
this release supports horizontal Latin, Greek and Cyrillic text plus supported
Common/Inherited characters. It does not implement Unicode bidi, script
itemization, Arabic/Indic shaping, CJK layout or vertical writing. A glyph outside
the supplied face or supported profile fails explicitly, even in Report mode.
Tables require table-layout: fixed in strict mode. Choose explicit first-row
column widths or equal columns. See coverage for the precise
fragmentation, whitespace and box restrictions.
Explicit font families
CSS font-family lists select the first family registered in a FontCatalog.
Named families are case-insensitive; quoted names remain distinct from generic
keywords. Bind generic families deliberately rather than relying on system fonts:
use PDF\HtmlToPdf\Font\FontCatalog;
use PDF\HtmlToPdf\Font\FontFace;
use PDF\HtmlToPdf\Font\FontSet;
$regular = new FontFace($regularBytes);
$sans = new FontSet($regular, bold: new FontFace($boldBytes));
$fonts = new FontSet($regular, catalog: new FontCatalog(
families: ['Report Sans' => $sans],
genericFamilies: ['sans-serif' => $sans],
));
// CSS: font: bold 10px/1.3 "Report Sans", sans-serif;
The default FontSet still serves documents without a CSS family declaration.
An explicit list with no registered match fails. After family selection the
requested variant and every glyph must exist in that supplied family; there is
no per-character fallback, synthetic variant or filesystem lookup. The supported
font shorthand expands atomically and resets omitted components. Small caps,
size keywords, system-font keywords and relative weight matching remain absent.
Text spacing and first-line indentation
p { word-spacing: .2em; letter-spacing: 1px; text-indent: 10%; }
p.compact { word-spacing: -1px; letter-spacing: -.02em; text-indent: -12px; }
word-spacing and letter-spacing accept normal, zero and signed lengths in
px, pt, pc, mm, cm, in or em; the existing rem extension also works.
Percentages are rejected for spacing. text-indent accepts the same lengths and
signed percentages, with zero as its initial value. ex remains unsupported.
Invalid values are rejected before cascade, so a later invalid value cannot erase
an earlier valid declaration in Report mode.
All three properties inherit. Relative lengths are computed using the element's
font size and then inherited as absolute points; increasing a child's font size
does not recompute its inherited em value. Percentage indentation remains a
percentage until layout and uses the content width of the block containing the
lines. It is independent of a float's temporary line exclusion. Only the first
eligible formatted line is indented: br, a new page and later anonymous inline
fragments after an in-flow block do not restart the parent's indent. Descendant
blocks still apply their own inherited or specified first-line indent.
Word spacing applies to spaces U+0020 and nonbreaking spaces U+00A0 remaining
after whitespace processing. NBSP remains unbreakable. Nonzero letter spacing
adds an advance after each grapheme, including a final trailing advance, matching
the selected Chromium/Firefox witnesses. It leaves combining marks attached and
disables optional ligatures. normal and zero preserve the original shaping.
These are bounded horizontal Latin/Greek/Cyrillic text capabilities, not a claim
of complete CSS Text support. A negative total run advance is rejected. A nonzero
letter spacing on a grapheme split between inline spans is also rejected, since shaping across those
run boundaries is not implemented. An indent that leaves no positive line width
fails explicitly. Preserved tabs with nonzero letter-spacing or word-spacing
raise UnsupportedFeature: the existing normal-spacing tab profile is retained,
without a claim of exact CSS tab-stop placement. The existing shaping, command
and output budgets still apply.
The same shaped advances determine wrapping, PDF text, inline backgrounds,
underlines and link bounds. ComputedStyle::textSpacing exposes immutable
TextSpacing values in points (null means normal);
ComputedStyle::textIndent exposes a signed TextIndent with its percentage flag.
The text example and independent checks
show the implemented behavior. Justification, text transformation, full font
matching and additional decorations remain outside this milestone.
Bounded floats and static controls
float:left/right requires an explicit width, including supported percentages.
Nested floats establish their own context; inline text uses the remaining line
space, and clear:left/right/both moves following content below the matching
floats. The entire document containing visible floats must fit on one page.
There is no float fragmentation, intrinsic float sizing or float/table combination.
Parent-child margin collapse within float contexts is rejected; explicit border
or padding can separate those margins. Fixed-height overflow stays visible in
this profile, while the total painted extent must still fit the page. A normal-flow
block with a background or border may not overlap an earlier float. A float
introduced after text on the same inline line is also rejected; these paint-order
and line-reflow contexts are not implemented.
Root html backgrounds paint the page canvas. When that background is transparent,
the body background is propagated to the canvas instead of painted twice. Body
box borders and margins retain their own geometry. dl/dt/dd remain distinct
HTML source nodes and project to Sect/P/Sect in PDF; this is not a richer
PDF definition-list model. Plain inline wrappers may split around block children;
decorations or link/language scopes spanning those blocks are rejected.
Form elements and controls are rejected by default. An explicit static snapshot permits radio controls, preserving checked/disabled state as a vector appearance:
use PDF\HtmlToPdf\FormControls;
$options = new ConversionOptions(
fonts: $fonts,
formControls: FormControls::StaticSnapshot,
);
The output contains logical Form/PrintField attributes, but no interactive PDF field, submission action or JavaScript. Other control types, custom radio box sizes/decorations and block/floating radios are unsupported. Report mode records a diagnostic and omits unsupported controls; strict mode rejects them. See the historical CSS1 example for the complete explicit setup.
Declaration validation, recovery and borders
The original value text is validated before it can replace a cascaded value.
In Report mode, color: red; color: invalid keeps red; a malformed shorthand
cannot apply only some of its sides. RGB clipping operates on the original
numeric components, rather than a parser's reserialized color. CSS escaped
identifiers are supported in selectors, but escaped declaration values such as
color: r\65 d remain outside the profile and produce a diagnostic or exception.
Reject remains the default for malformed or unsupported CSS. Report diagnoses
selected syntax errors and reparses the bounded source so neighboring valid
declarations and following rules can survive. This is not complete browser
syntax recovery. A selector list containing an invalid or unsupported selector
is discarded as a whole; for example p, a:hover does not style p in Report.
Dynamic states, pseudo-elements and user-origin stylesheets remain unsupported.
Stylesheet bytes must already be UTF-8. One initial @charset "UTF-8"; is
accepted; unknown or different encodings, repeated or misplaced directives are
rejected before parsing. Report records css.charset and ignores that complete
stylesheet, preserving other supplied stylesheets. No encoding conversion occurs.
Borders have independent top/right/bottom/left width, style and color, with
solid, dotted, dashed, double, groove, ridge, inset, outset and
none. Shorthands expand before cascade, omitted components reset to their
initial values, and inherit uses the parent's computed sides. none has zero
used width; a transparent visible-style border keeps its geometric width.
This implementation chooses thin = 1px, medium = 3px, thick = 5px, or
0.75/2.25/3.75 PDF points. These are package choices, not universal CSS-mandated
widths. Border decorations on inline boxes remain unsupported, as do collapsed
table borders. See the CSS1 example for the supported block/cell profile.
For callers inspecting computed styles, use ComputedStyle::borderEdges() and
the immutable Borders/BorderSide objects. The legacy borderWidth and
borderColor fields remain available as top-side projections in compiled styles;
for a transparent top side, the color projection falls back to the text color.
They cannot represent all four sides or transparency. A manually constructed
0.1 style without borders still describes a uniform solid border.
Unsupported input
UnsupportedPolicy::Reject is the default. UnsupportedFeature stops conversion
when a recognized unsupported feature is encountered. To inspect a bounded
approximation intentionally, pass unsupported: UnsupportedPolicy::Report and
review every ConversionResult::diagnostics entry (code, message, nodeId).
Report mode is not a fidelity promise and does not waive missing resources,
unsafe links, missing glyphs, indivisible overflow or resource limits. Errors
produce no successful partial result. JavaScript is never executed.
Limits
Each conversion owns its budgets; previous conversions cannot exhaust or reset the next one's counters. Defaults are explicit and configurable:
ConversionLimits |
Default |
|---|---|
| HTML bytes | 1 MiB |
| Cumulative parsed CSS bytes | 256 KiB |
| HTML/semantic nodes | 10,000 |
| HTML/CSS nesting and selector-tree depth | 64 |
| Text characters | 100,000 |
| CSS rule/declaration scan budget | 1,000 |
| Selector work (selector bytes × candidate elements) | 500,000 |
CSS imports (maxImports) |
128 |
Nested CSS imports (maxImportDepth) |
16 |
| Pages | 200 |
| Drawing commands | 50,000 |
| Catalog bytes, including unused resources | 16 MiB |
| Cumulative catalog image pixels | 16,000,000 |
| Serialized PDF bytes | 32 MiB |
ImageAsset separately checks its byte and pixel limits before embedding.
FontLimits separately bounds font parsing and each shaping run. Output length
is checked after serialization; it is not a process memory limit. Applications
converting untrusted documents should also use their usual worker memory/time
limits. Raising a limit does not add support for an absent feature.
The complete Lato examples use three prepared faces and are run with
php -d memory_limit=512M examples/generate.php _build/examples. Parsed OpenType
tables use more memory than compressed font bytes: the measured Lato fixture
needs about 52 MiB per prepared face. Unused variants are prepared lazily.