Skip to content

fix(filesystem): escape plain text in generated PDFs - #5502

Merged
sauravpanda merged 5 commits into
browser-use:mainfrom
primorLee:codex/fix-pdf-plain-text-rendering
Aug 24, 2026
Merged

sauravpanda merged 5 commits into
browser-use:mainfrom
primorLee:codex/fix-pdf-plain-text-rendering

Conversation

@primorLee

@primorLee primorLee commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Why

PdfFile documents its input as plain text, but each line was passed directly to ReportLab's Paragraph parser. Text such as Comparison: 2 <b y & 5 > 4 was interpreted as malformed markup, so write_file returned an error instead of creating the PDF.

What changed

  • Escape plain-text content before passing it to ReportLab
  • Keep the existing Markdown heading style selection
  • Add a regression test through the real FileSystem.write_file path and verify the generated PDF text with PdfReader

Before / after

  • Before: the regression failed with a ReportLab parse error expecting a closing </b> tag
  • After: the PDF is created successfully and extracts back to the exact original text

A screenshot is not applicable to this generated-file failure; the regression validates the resulting PDF content directly.

Testing

  • Focused regression: RED on the base commit, then 1 passed after the fix
  • python -m pytest -o addopts='' -p no:cacheprovider --confcutdir=tests/ci/infrastructure -q tests/ci/infrastructure/test_filesystem.py (81 passed)
  • ./bin/lint.sh --staged (Ruff, Pyright, and other pre-commit hooks passed)

Summary by cubic

Escape plain text and strip only leading Markdown header markers when generating PDFs so markup-like characters no longer break creation and extracted text matches input. Previously lines were passed to reportlab Paragraph as markup and could error; now we slice header markers to select styles and escape the remaining text.

  • Render: detect # , ## , ### to choose the style, then pass html.escape(text) to Paragraph; applies to both FileSystem.write_file and append_file.
  • Tests: end-to-end via FileSystem.write_file/append_file, validate with pypdf PdfReader using a whitespace-collapsed blob to tolerate wrapping; assert only leading header markers are stripped; cover literal # in content, a non-header starting with #, and quotes/angle brackets. No migration; heading styles unchanged.

Written for commit e6b92be. Summary will update on new commits.

Review in cubic

@CLAassistant

CLAassistant commented Aug 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

Escaping first only worked because '#' and ' ' are not escapable, so the
prefix length happened to survive html.escape and the [2:]/[3:]/[4:]
offsets still landed correctly. Slice first, escape once at the point of
use, and cover the three header branches in the regression test.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/ci/infrastructure/test_filesystem.py">

<violation number="1" location="tests/ci/infrastructure/test_filesystem.py:240">
P3: This blanket `'#' not in extracted_text` check rejects valid plain-text hashes even though the renderer only strips leading heading markers. Narrow the assertion to the actual `# ` / `## ` / `### ` prefixes.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

… absent

`'#' not in extracted_text` claimed more than the renderer promises: only
a leading `# `/`## `/`### ` marker is stripped, hashes anywhere else are
plain text. Assert each header's source line is gone and its rendered
text survives, and cover a literal `#1` in header content.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 1 file (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

A substring check passes with a leftover marker: 'Notes <i> #1' matches
inside '# Notes <i> #1', so an H3 branch using the H1 offset would slip
through. Compare against whole extracted lines instead, and cover the
gaps that turned up alongside it: a non-header line starting with '#'
(pins the 'startswith("# ")' boundary against an lstrip-style rewrite)
and quote characters (html.escape emits &#x27;/&quot;, which ReportLab
must unescape back). Rename to match what the test now covers.
… path

Whole-line matching broke on any content long enough to wrap, so compare
against a whitespace-collapsed blob instead and check leftover header
markers separately against the rendered lines. Verified against headers
and body lines that wrap over 2-3 lines.

Add an append regression: append_file re-renders the whole document, so
put the markup that trips ReportLab in the appended half to exercise
that path on its own. Both PDF tests fail without the escape fix.

Also condense the stale three-line comment above the render loop.
@sauravpanda
sauravpanda merged commit 9a2db2d into browser-use:main Aug 24, 2026
108 checks passed
MagMueller added a commit that referenced this pull request Aug 27, 2026
`write_file` tells the model that PDF content is markdown, but after
#5502 only `#` / `##` / `###` headings were styled. Bold, italic, inline
code, and bullets printed as literal `**syntax**`.

Keep the #5502 escape-first behavior (raw ReportLab markup cannot be
restored — that is the parse bug). After `html.escape`, convert a small
markdown subset to RML. Underscores stay literal so `snake_case` names
are not italicized. `*` is only emphasis when it actually wraps a span —
`2 * 3 * 4` and globs are left alone. Fenced blocks skip inline
conversion so shell backticks survive.

Heading offsets are shared with `DocxFile` via `_split_heading`.

Fixes #5538

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Render markdown emphasis in PDF exports from `write_file` so PDFs match
the documented markdown contract. Previously only `#`/`##`/`###`
headings rendered; now bold, italic, inline code, and bullets format
correctly while we keep the escape-first fix to avoid `ReportLab` parse
issues.

- Convert `**bold**`, `*italic*`, `` `inline` ``, and list bullets
(`-`/`*`) to RML; code fences bypass inline conversion; underscores
never italicize.
- Keep glob tokens and arithmetic stars literal (e.g., `*.txt`,
`**/foo/**`, `2 * 3 * 4`); inline code protects emphasis markers.
- Restore inline code via collision-safe placeholders so user text
cannot be mistaken for stash tokens.
- Share heading parsing with `DocxFile` to align `#`/`##`/`###` levels;
add tests for emphasis, bullets, fences, globs, and edge cases.

<sup>Written for commit f168606.
Summary will update on new commits.</sup>

<a
href="https://cubic.dev/pr/browser-use/browser-use/pull/5540?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>

<!-- End of auto-generated description by cubic. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants