← Blog

viewBox vs width/height — what determines the PDF page size

Three SVG attributes; their interaction determines the rendered size and the coordinate system Three relevant attributes width / height physical size at which the SVG should display viewBox internal coordinate system: x, y, w, h preserveAspectRatio fitting rule when viewBox aspect ≠ width/height aspect Page size in the output PDF Derived from width/height (or viewBox when those are absent), then bounded by A4 (~792 × 1123 pt). SVGs larger than A4 are scaled down to fit; smaller SVGs keep their natural size.

SVG's sizing model has two layers: the physical size (how big the rendered drawing should be on a page or screen) and the coordinate system (the numerical space inside which shapes are drawn). They're set by separate attributes that interact in non-obvious ways. SVGtoPDF has to resolve them to a single PDF page size.

The three attributes

How SVGtoPDF picks a page size

The rule (matching the SVG 1.1 spec):

  1. If width and height are present and have explicit units (mm, in, pt), use them directly. PDF page = converted to points.
  2. If width and height are present but unitless, treat them as CSS pixels. PDF page = (width × 72 / 96) × (height × 72 / 96) points.
  3. If viewBox is present but width/height are not, use the viewBox's width and height as point dimensions. PDF page = (viewBox_w) × (viewBox_h) points.
  4. If only width/height are present without viewBox, use them and assume the viewBox matches.
  5. If none are present, fall back to a default (typically 300 × 150 pixels, per the spec).

Worked examples

preserveAspectRatio — what happens with mismatched aspects

When the viewBox aspect ratio doesn't match the width/height aspect, the SVG renderer needs a rule for fitting. preserveAspectRatio gives nine options:

Default is "xMidYMid meet": center, letterbox.

SVGtoPDF respects preserveAspectRatio when determining how content is drawn but uses width/height (or viewBox if width/height are absent) for the actual PDF page size. The page is the outer rectangle; content is placed inside per the rule.

The most common case

Designers exporting SVGs from modern design tools typically set width and height in pixels matching the viewBox dimensions:

<svg width="200" height="100" viewBox="0 0 200 100">

This produces a 200 × 100 CSS-pixel canvas with a 1:1 mapping between viewBox coordinates and pixels. SVGtoPDF converts to 200 × 72/96 = 150 pt × 75 pt — a 2.08 × 1.04 inch PDF page. For most practical SVGs (icons, small diagrams), this is fine.

For larger output (logos meant to print at 8 inches wide), set explicit physical units:

<svg width="8in" height="2in" viewBox="0 0 200 50">

The PDF page becomes exactly 8 × 2 inches; the internal coordinate system stays a 200 × 50 grid.

When the resulting PDF feels too small

A common surprise: an SVG looks fine in a browser but produces a tiny PDF page. Cause: the SVG declared no width / height, only a viewBox like "0 0 24 24" (typical for icon SVGs from icon libraries). SVGtoPDF treats viewBox dimensions as points, producing a 24 × 24 pt page = roughly 1/3 × 1/3 inch.

Fix:

  1. Add explicit width/height to the SVG before upload: <svg width="240" height="240" viewBox="0 0 24 24"> for a 10× larger page.
  2. Or set physical units: <svg width="50mm" height="50mm" viewBox="0 0 24 24">.

When the resulting PDF feels too large

The opposite: an SVG with width="2000" height="2000" produces a 1500 × 1500 pt PDF page (about 21 × 21 inches), much bigger than expected. Cause: the SVG was authored at high pixel density for screen, and the unitless values are interpreted as pixels.

Fix: reduce the width/height to the size you actually want printed. The viewBox can stay the same — it sets the coordinate system, not the page size.

Inspecting an SVG

Open the SVG in a desktop SVG editor (Inkscape, Illustrator, Affinity Designer) or simply in a text editor — the root <svg> tag is right at the top and shows the width, height, and viewBox attributes. A few examples:

<svg width="200" height="100" viewBox="0 0 200 100">        — explicit, matches viewBox
<svg viewBox="0 0 24 24">                                 — no size (icon style)
<svg width="100mm" height="50mm" viewBox="0 0 200 100">   — explicit physical size

If you want to see the rendered dimensions before uploading, drop the SVG into a browser tab — the page title bar or zoom indicator usually reports the natural dimensions.