flashman
← All posts

Markdown PDF heading filename debugging

Debug a Markdown PDF file name that is not the heading you see: the first h1 is kept, punctuation is dropped, and the file stem stops at forty characters.

2026-10-22 · 6 min read · Rahul Chitturi

  • markdown
  • pdf
  • debugging

The Markdown to PDF page builds a file name from the first level-one heading, then downloads an A4 PDF of the sanitized HTML. The button stays disabled while the box trims to nothing. Pressing export in that state sets the message Add Markdown content before exporting.

The heading you see in the preview can still be the wrong file name. The stem is a separate pass over the source, and it keeps a shorter, plainer string than the h1 element.

Take the first line that starts with one hash

The match is a line whose first character is # and whose next characters are whitespace, anywhere in the file. # Flashman Markdown, the sample's first heading, becomes Flashman Markdown.pdf. An introduction paragraph and then # Real title still names the file Real title.pdf. ## Not a title does not count. A later # Yes does, so the file is Yes.pdf.

A heading of only # and a space becomes document.pdf, because the stem is empty after trim. A file with no level-one heading, including a thematic break written as ***, also becomes document.pdf. That break still exports. Its HTML is a horizontal rule.

  • Put the file name in the first h1.
  • Leave a space after the hash.
  • Do not rely on an h2.
  • Expect document.pdf when that h1 is missing or blank.

Drop punctuation, then stop at forty characters

Every character that is not an ASCII letter, digit, underscore, space, or hyphen is deleted. # Hello, world! becomes Hello world.pdf. # Title with émoji 🙂 becomes Title with moji.pdf, because é and the emoji are removed and the extra space is trimmed. # Hello_world-2 stays Hello_world-2.pdf. # --- stays ---.pdf.

The stem is then cut to 40 characters. Fifty A's after the hash become a file whose stem is forty A's. The h1 in the PDF still has all fifty. The page width used for the capture is 794 pixels, the canvas scale is 2, and the page is A4 portrait with a 10 millimetre margin.

Export the HTML the preview already sanitized

The PDF is rendered from the same sanitized HTML as the white preview. A script tag is not in that HTML. A javascript: link keeps the visible text and loses the URL. target=_blank is kept when the HTML has it. Marked does not add that attribute for you.

Use the PDF tool for the download, the Markdown editor to copy the HTML you are about to export, and the viewer to read the source heading that supplied the name. Rename the file after download if the stem dropped a character you needed. The h1 inside the pages is unchanged.

Try these tools