# Pymupdf4llm: byte order marks in TOC prevent recognition of the document structure

**URL:** <https://forum.mupdf.com/t/pymupdf4llm-byte-order-marks-in-toc-prevent-recognition-of-the-document-structure/178>\
**Category:** PyMuPDF\
**Created:** [October 2, 2025, 6:31am UTC](https://forum.mupdf.com/t/pymupdf4llm-byte-order-marks-in-toc-prevent-recognition-of-the-document-structure/178 "2025-10-02T06:31:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![soelderer](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@soelderer](https://forum.mupdf.com/u/soelderer)\
**Post date:** [October 2, 2025, 6:31am UTC](https://forum.mupdf.com/t/pymupdf4llm-byte-order-marks-in-toc-prevent-recognition-of-the-document-structure/178/1 "2025-10-02T06:31:02Z")

</div>

Hey,

I’ve already posted it on GitHub, but then saw the recommendation to use the forum. I’m extracting [this paper](https://www.nature.com/articles/s41598-025-14895-z) to markdown like this:

```auto
doc = pymupdf.open(absolute_path)
headers = pymupdf4llm.TocHeaders(doc)
text = pymupdf4llm.to_markdown(doc, hdr_info=headers)

```

I noticed that the TocHeaders start with UTF-8 byte order marks: `'\ufeffEffects of open-label placebos across populations and outcomes: an updated systematic review and meta-analysis of randomized controlled trials'`. This prevents recognising the document structure, because `title.startswith(text)` fails.

For a quick fix, you could just strip the BOM in get\_header\_id. Recognition also fails if there are non-breaking spaces (\xa0), I suggest to replace them. See my pull request [#309](https://github.com/pymupdf/pymupdf4llm/pull/309).

Greetings,  
Paul

---

<div class="post-metadata">

**Author:** ![Jamie\_Lemon](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/jamie_lemon/32/5_2.png) [@Jamie\_Lemon](https://forum.mupdf.com/u/Jamie_Lemon)\
**Post date:** [October 2, 2025, 1:07pm UTC](https://forum.mupdf.com/t/pymupdf4llm-byte-order-marks-in-toc-prevent-recognition-of-the-document-structure/178/2 "2025-10-02T13:07:59Z")

</div>

Hi @soelderer - thanks for the post and I see the issue. Your PR seems reasonable enough to me - @HaraldLieder What do you think?

---

<div class="post-metadata">

**Author:** ![HaraldLieder](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/haraldlieder/32/14_2.png) [@HaraldLieder](https://forum.mupdf.com/u/HaraldLieder)\
**Post date:** [October 2, 2025, 5:40pm UTC](https://forum.mupdf.com/t/pymupdf4llm-byte-order-marks-in-toc-prevent-recognition-of-the-document-structure/178/3 "2025-10-02T17:40:29Z")

</div>

I fully agree. Just hadn’t the time to respond properly. The fix is non-toxic as it does nothing if the string doesn’t start with a BOM. Similar is true for replacing the unbreakable space by a normal one.  
Definitely the next version will include all that.
