How this converts markup to a document
The HTML is parsed into a document tree, walked into a list of blocks — heading, paragraph, list item, table row, quote, code — and each block is typeset onto A4 with real text. Nothing is rendered to a canvas, so the output is a genuine text PDF rather than a picture of one.
Parsing happens with DOMParser into a detached document, so nothing in the markup you paste can execute or touch the page you are on.
What you gain and what you give up
You gain selectable, searchable, accessible text and a file measured in kilobytes rather than megabytes. You give up visual fidelity: this will not reproduce a styled marketing page. For document-shaped content — an invoice, a report, an article, a set of notes — that trade is almost always the right one.
For the reverse, PDF to HTML extracts a PDF's text into markup. To tidy the HTML before converting, use the HTML beautifier. For plain text input, Text to PDF is more direct.