Skip to content

PDF export renders markdown as literal syntax (only headers are handled) #5538

Closed

Description

@linear

Context

The write_file action description (browser_use/tools/service.py:1751) tells the model:

"For PDF files, write content in markdown format and it will be auto-converted to PDF."

PdfFile.sync_to_disk_sync (browser_use/filesystem/file_system.py:246) implements exactly three of those: #, ##, ###. Everything else — **bold**, *italic*, `code`, - bullets, [text](url), tables — is passed through verbatim and renders as literal syntax noise in the PDF. We're promising the model something we don't deliver, and it dutifully emits markdown that comes out looking like an unrendered .md file.

Why now

PR #5502 fixed a real crash: ReportLab parses Paragraph input as RML markup, so plain text like Comparison: 2 <b y & 5 > 4 raised Parse error: saw </para> instead of expected </b> and write_file returned an error instead of a PDF. The fix escapes content before handing it to Paragraph.

Correct fix, but it has a side effect worth recording: ReportLab inline markup used to pass through, and now renders literally.

input before #5502 after #5502
<b>bold text</b> renders bold literal <b>bold text</b>
<i>, <font color="red">, <super>, <br/> honored literal
&nbsp;, &#65; , A literal

That pass-through can't be restored — it's the same mechanism as the bug. The parser cannot distinguish a formatting tag from a comparison operator, so any allowlist heuristic fails on exactly the input class that filed the bug. Escaping is the only correct fix.

The right answer is to deliver the documented contract instead.

Proposed work

Escape first, then convert markdown → RML on the already-escaped string. Order matters: post-escape there are no user-supplied < left, so injecting <b> is unambiguous.

text = html.escape(text)
text = re.sub(r'\*\*(\S(?:.*?\S)?)\*\*', r'<b>\1</b>', text)
text = re.sub(r'`([^`]+)`', r'<font face="Courier">\1</font>', text)

Scope: inline emphasis, inline code, and bullet lists. Links and tables are a bigger lift — split those out if they balloon.

Traps (these are what make it a real PR, not a one-liner)

  • * is overloaded — * item bullets, 2 * 3 * 4 arithmetic, glob patterns. Needs non-greedy matching with non-space boundaries, and leading * must be excluded from emphasis.
  • _ is not italic — file_name_here, snake_case_ident. Underscore emphasis has to be dropped entirely or it mangles every identifier an agent writes. Naive implementations always get this wrong.
  • Backticks in shell snippets — `$(cmd)` inside a fenced block vs. inline code.

Each needs its own test case.

Also worth folding in

PdfFile and DocxFile carry byte-identical header parsing — # /## /### with [2:]/[3:]/[4:] offsets (file_system.py:266 and :302). A shared _split_heading(line) -> tuple[str, int | None] gives this work one place to grow and kills the duplicated offset arithmetic. #5502 already turned up one bug hiding in those offsets.

Acceptance criteria

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions