How to Print Long Code to PDF with Line Numbers

Create a readable source-code PDF with optional line numbers, landscape layout and safe long-line chunking instead of cutting code off at the page edge.

Create a code PDF

Line numbers make printed code easier to reference

When reviewers discuss a printed or shared code listing, line numbers provide stable reference points that are easier to cite than visual positions on a page.

1into1 includes a dedicated line-number gutter by default, and you can turn it off when the PDF is intended only for presentation.

Landscape gives long lines more room

Portrait pages leave less horizontal space for monospace source. Switching to landscape increases the width available to each source line.

A4 and US Letter are both supported, and the 7.5pt Compact setting fits more characters per line than 8.5pt or 10pt.

Long lines are chunked instead of clipped

The renderer calculates how many monospace characters fit in the current code area and splits wider lines into sequential chunks.

Only the first chunk displays the original source line number, so continuation chunks remain visually tied to that same source line instead of pretending to be new numbered lines.

Unicode chunks are split by grapheme when possible

Breaking a Unicode string at an arbitrary UTF-16 position can split a surrogate pair or combined character sequence. The current renderer uses the browser Intl.Segmenter grapheme API when available for Unicode lines and falls back to code-point iteration otherwise.

This reduces the risk of breaking emoji, combining marks or joined character sequences at a page-width boundary.

Syntax color stays behind the source text

The highlighter draws subtle background regions for detected keywords, strings, comments, numbers and function-like names, then writes the visible source once on top.

That design avoids reconstructing a code line from individually spaced colored tokens, which helps protect punctuation and whitespace alignment.

Official sources reviewed

  1. MDN — Handling whitespaceExplains why source-code whitespace such as spaces, tabs and line feeds needs explicit preservation rules.
  2. MDN — Intl.SegmenterDocuments locale-sensitive segmentation, including grapheme segmentation used to avoid splitting complex Unicode character sequences.