PDF & Documents

Making a PDF out of HTML, plain text and a form you filled in

A web page has no pages. Turning one into a PDF means inventing page breaks, and where they land is the whole problem.

4 minute read

Three different jobs get called "make a PDF": turn this web page into one, turn this text file into one, and give me back the form I just filled in. They share an output format and almost nothing else.

HTML has no pages

This is the root of every surprise in the first job. A web page is one continuous column of unknown length. A PDF is a sequence of fixed rectangles. Converting means deciding where to cut, and nothing in the HTML says where a cut would be acceptable.

So a table splits across a page boundary with its header left behind. A heading lands as the last line of a page with its paragraph overleaf. A chart is bisected.

HTML to PDF has to make those decisions, and a few things make its job easier:

  • Choose the page size yourself. A4 and US Letter differ by about 18mm of height, which is enough to move every break in a long document.
  • Expect backgrounds to be dropped. Browsers omit background colours and images when printing unless told otherwise, which is why a dark-themed page often converts to black text on white.
  • Fixed and sticky positioning has no meaning. A sticky header belongs to a viewport, and there is no viewport. It usually renders once at the top or repeats on every page, and neither is what the page looked like.
  • Links survive as links. Anchors become clickable PDF link annotations, which is useful and often unexpected.

If what you actually want is a readable archive of an article rather than a facsimile of the page, strip it to the content first. Fewer elements means fewer things to break across a boundary.

Plain text is the easy case

Text to PDF has no layout to preserve, so the only decisions are page size, margins, font and size. That makes it reliable, and useful for the specific job of turning a log, a transcript or an exported note into something that paginates and prints predictably.

The one thing to watch is line length. Text files often contain lines far wider than a page. They either wrap, which is usually fine, or overflow, which is not. If the content is code or tabular output where wrapping destroys the meaning, landscape orientation and a smaller monospace size is the fix.

Filling a form is not drawing on a page

A PDF form has real fields: named objects with types, values and validation, defined in the document's AcroForm structure. Filling them properly means setting those values.

The alternative people fall into is typing text on top of the page as an annotation or a drawing. It looks the same on screen and behaves completely differently. The values are not in the fields, so anything that reads the form programmatically, which is the point of a form, sees an empty document.

Fill PDF forms sets the field values. Then, before you send it:

Watermarks: content or annotation

A watermark can be added two ways, and the difference decides whether it is worth anything.

As page content it is drawn into the content stream, the same layer as the text. Removing it means editing the page.

As an annotation it sits in a separate layer, and any viewer with an annotation panel deletes it in two clicks.

If the watermark is decorative, either is fine. If it says DRAFT or CONFIDENTIAL and you care that it stays, it needs to be content. Even then, be clear about what it does: a watermark marks a document, it does not protect it. Anyone determined enough can re-render the pages. It is a label, useful for stopping a draft being mistaken for a final, and not a control.

When the file will not open at all

A PDF ends with a cross-reference table saying where every object lives. It is the part most often damaged by a truncated download, a failed sync or a crash mid-save, and a viewer that cannot read it refuses the whole file even though the page content is usually intact.

Repairing a PDF means scanning the file for object definitions and rebuilding that index from what is found. It often works, because the damage is to the map rather than the territory, and desktop tools such as qpdf and Ghostscript can attempt it.

What it cannot do is invent bytes that are not there. If a download stopped at 60%, the last 40% of the pages do not exist and no repair recovers them. What you usually get back is the pages that made it, which is worth having.

Further reading

Do it hereHTML to PDFConvert HTML content into a paginated PDF document in your browserOpen

Written for NextTool. Every statement about a tool is checked against that tool's code, and corrections are dated above. How guides are written

pdfhtmlforms