Control fonts and text styling over NextPDF Connect
At a glance
Section titled “At a glance”Build a document with multiple text styles by switching fonts between text
blocks. set_font sets the active typeface, style, and size. That state
applies to each following add_text call until you change it. You need
create_pdf, set_font, add_text, and output_pdf — all Core tools. Text
state parameters control glyph rendering (ISO 32000-2 §9).
Install
Section titled “Install”composer require nextpdf/serverBind a transport. You can use three built-in font families without adding
font files: helvetica, times, and courier.
Conceptual overview
Section titled “Conceptual overview”set_font takes family, style ("", "B", "I", "BI", "U"), and
size. The font you set applies to every later add_text call until you
call set_font again. There is no automatic reset. When add_text includes
font_size, that value overrides the active size for that single call
only. The set_font size returns for the next call. Text is emitted by
text-showing operators in content-stream order (ISO 32000-2 §9.4).
add_text has no colour parameter. Text colour comes from the document’s
graphics-state ink colour.
API surface
Section titled “API surface”| Tool | Role | Risk tier |
|---|---|---|
create_pdf | Open the session | Safe |
set_font | Set the active typeface/style/size | Caution |
add_text | Write text in the active font | Caution |
output_pdf | Render and return the PDF | Approval Required / Review (base64) |
The tool catalog is the catalog of record. The tools you can use depend on the installed tier.
Code sample — Quick start
Section titled “Code sample — Quick start”create_pdf(A4 portrait) →document_id.set_font(helvetica,B, 24) thenadd_text(title, centered).set_font(helvetica,"", 12) thenadd_text(justified body).set_font(courier,I, 10) thenadd_text(a code snippet).output_pdf.
Code sample — Production
Section titled “Code sample — Production”Set the font explicitly before each block where typography matters. Never
rely on inherited state across logical sections. Use the per-call
font_size override only for one-off emphasis, because it does not persist.
Build the document section by section, and verify that each add_text
returns a position.
Edge cases & gotchas
Section titled “Edge cases & gotchas”- Unknown family. Anything outside
helvetica/times/courier(for examplearial) is rejected. - Empty family.
familymust be a non-empty string. - Invalid style. Use the uppercase codes.
"bold"is rejected. - Non-positive size.
sizemust be a positive number. - Per-call size is temporary. It does not change the
set_fontstate.
Performance
Section titled “Performance”Built-in fonts add negligible weight, and output is a few KB. The profile is
structural.
Security notes
Section titled “Security notes”Base64 mode has no filesystem side effect. Built-in fonts do not ingest external font files, so this recipe has no font-parsing attack surface.
Conformance
Section titled “Conformance”| Statement | Spec | Clause | reference_id |
|---|---|---|---|
| Text is shown by text operators in stream order. | ISO 32000-2 | §9.4 | |
| Text-state parameters govern glyph rendering. | ISO 32000-2 | §9 |
Commercial context
Section titled “Commercial context”Not applicable — all tools are Core.
Transport availability
Section titled “Transport availability”| Transport | Available | Notes |
|---|---|---|
| MCP (stdio) | Yes | tools/call per tool. |
| REST | Yes | One operation per tool. |
| gRPC | Yes | Unary per tool. |
HITL risk tier
Section titled “HITL risk tier”create_pdf Safe; set_font/add_text Caution; output_pdf Approval
Required, downgraded to Review in base64 mode
(HITL risk tiers).
Confirmation gate JSON envelope
Section titled “Confirmation gate JSON envelope”Base64 output:
{ "allowed": true }