You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PDF.js does not take into account /ActualText #12237
I'm using PDF.js 2.4.456. The issue I'm describing here is independent of the OS and the web browser.
To reproduce the problem, open the attached document with PDF.js (a quick way to reproduce the bug is to open the document in Firefox), then copy and paste some of the text. In the extracted text, the period characters are missing for the main font:
The Ke3chang group, also known as APT15, is a threat group believed to be operating out of China Its attacks were first reported in 2012,
This is due to a bad mapping in the /ToUnicode property of the font: the 0x2E code (i.e. the period character in ASCII) in the text is mapped to U+0020 (a space in UTF-16).
But when we copy-paste the content of the PDF using Adobe Acrobat, we extract all the period characters correctly. It is because Acrobat takes into account the /ActualText marked text properties inside the PDF.
This is what we see when we open the document using a PDF inspector like PDFDebugger of PDFBox:
(, also known as APT15, is a threat group believed to be operating out of China)Tj
/Span<</ActualText<FEFF002E>>> BDC
(.)Tj
EMC
We see that the 0x2E code in (.)Tj, which according to the /ToUnicode map represents a space character, is marked to actually represent U+FEFF (a BOM) and U+002E in, a period character.
Thus, Acrobat Reader extract the periods correctly in the given report because it takes into account the /ActualText content, whereas PDF.js doesn't extract the periods because it only considers the /ToUnicode map.
Is there an ongoing effort to fix this problem?
Reacted by Erik Scholz, Max Chernoff and Scott Willeke
It wasn't clear that the issue came from the ActualText property before, we thought that PDF.js did the job right because it followed the ToUnicode map. I would rather close/delete the old issue and leave this one which is clearer, if you agree.
hi @TimotheAlbouy would you think it's possible to do a preliminary work to fix the unicode mapping problem of the pdf file itself? then may the pdf.js would render the text layer normally.
@Mowmowj of course this would be possible, but it would not address the root problem: currently PDF.js cannot extract some text that Acrobat Reader can. You wouldn't tell a visually impaired person to change their book instead of replacing their faulty glasses.
I ran into this issue when I noticed that PDF.js can't find text strings such as fi, tt, etc if these are represented as ligatures in the PDF documents. I noticed that the PDF viewer Evince is able to find search text even in PDFs that do contain ligatures. Since evince is open source, maybe there is something in their approach that could be useful for PDF.js as well? I'm not familiar with exactly what that would be, but a brief search in their repo shows that they e.g. do some normalization of the text in the PDF https://gitlab.gnome.org/GNOME/evince/-/commit/9de1152cd935d9f00f2709052d25d42b18cb1b0f
I ran into this issue when I noticed that PDF.js can't find text strings such as fi, tt, etc if these are represented as ligatures in the PDF documents. I noticed that the PDF viewer Evince is able to find search text even in PDFs that do contain ligatures. Since evince is open source, maybe there is something in their approach that could be useful for PDF.js as well? I'm not familiar with exactly what that would be, but a brief search in their repo shows that they e.g. do some normalization of the text in the PDF https://gitlab.gnome.org/GNOME/evince/-/commit/9de1152cd935d9f00f2709052d25d42b18cb1b0f
Good point, looks like it's use the Glib, BTW for inspiration, the mupdf viewer also supports the special string. but i'm not sure how they figure out it.
The issue of /ActualText being ignored is also important for CJK language users. There is a related issue in Chromium where /ActualText with CJK characters was ignored, that has recently been fixed. See the Chromium issue:
As described in that issue, PDFs generated by Chromium have a problem where some CJK characters are replaced with Kangxi radicals if the PDF viewer does not handle ActualText properly.
The /ActualText in PDF is an essential feature for accessibility, and I hope PDF.js can handle it as well.
To get some idea of the issues one might run into while implementing this I created a patch bungeman@6a52783 . This simply reads and stores the /ActualText and then adds it as an attribute to the span in the text layer. However, it isn't clear exactly how emit the /ActualText into the text layer.
Another case: Inter's case-sensitive forms of (, ) and - (glyph IDs 0x544, 0x545, 0x55E) have no Unicode mapping in the embedded font's ToUnicode, cmap or post tables, so getTextContent returns U+0000 for them. The PDF wraps each glyph in an /ActualText span with the intended character, and other readers extract it correctly. I reproduced this on 6.3.289. I can't share the file, but I can put together a minimal repro if that would help.
Same root cause as @olyapop's case, with a minimal reproducer and a patch.
Chrome up to version 128 prints a variable font (Inter with a wght axis, in our case) as Type3 glyphs whose /ToUnicode maps digits, spaces and punctuation to U+0000, and wraps each of them in /Span << /ActualText (7) >> BDC … EMC. PDFium, MuPDF and Preview read the ActualText and copy an offer's number, date and amount as printed. pdf.js 6.3.289 and 6.4.299 return U+0000 for every digit, space and punctuation mark, so an amount copies as €: getTextContent reads only MCID from the property dictionary and never ActualText.
Reproducer (in the PR as test/pdfs/actualtext_type3.pdf, 1.5 KB, hand-written): "Total: " in Helvetica, one Type3 glyph drawn as a 7 and mapped to U+0000, inside /Span << /ActualText (7) >>, then " EUR". PDFium extracts Total: 7 EUR; pdf.js extracts Total: EUR with U+0000 where the 7 should be.
On the open question from @bungeman's prototype (how to emit it into the text layer): what works for us in production is to hand the text along on the beginMarkedContentProps item and let the consumer apply it. The span's text goes to its first non-empty text item, the remaining items of the span become empty, and the empty line-start items (hasEOL) inside the span are left alone, so the amount still copies as one line. #22315 adds the first half (the property); applying it inside getTextContent itself could follow once the text-layer semantics are agreed.
Activity
Snuffleupagus commentedon Aug 18, 2020
Duplicate of #12100 (please don't knowingly open duplicate issues).
TimotheAlbouy commentedon Aug 18, 2020
It wasn't clear that the issue came from the ActualText property before, we thought that PDF.js did the job right because it followed the ToUnicode map. I would rather close/delete the old issue and leave this one which is clearer, if you agree.
timvandermeij commentedon Aug 18, 2020
I closed the other issue since this one contains a bit more details, but we should indeed keep the discussion limited to this issue from now on.
Mowmowj commentedon Feb 21, 2024
hi @TimotheAlbouy would you think it's possible to do a preliminary work to fix the unicode mapping problem of the pdf file itself? then may the pdf.js would render the text layer normally.
TimotheAlbouy commentedon Feb 21, 2024
@Mowmowj of course this would be possible, but it would not address the root problem: currently PDF.js cannot extract some text that Acrobat Reader can. You wouldn't tell a visually impaired person to change their book instead of replacing their faulty glasses.
joelostblom commentedon Jul 8, 2024
I ran into this issue when I noticed that PDF.js can't find text strings such as
fi,tt, etc if these are represented as ligatures in the PDF documents. I noticed that the PDF viewer Evince is able to find search text even in PDFs that do contain ligatures. Since evince is open source, maybe there is something in their approach that could be useful for PDF.js as well? I'm not familiar with exactly what that would be, but a brief search in their repo shows that they e.g. do some normalization of the text in the PDF https://gitlab.gnome.org/GNOME/evince/-/commit/9de1152cd935d9f00f2709052d25d42b18cb1b0fMowmowj commentedon Jul 8, 2024
Good point, looks like it's use the Glib, BTW for inspiration, the mupdf viewer also supports the special string. but i'm not sure how they figure out it.
MurakamiShinyu commentedon Jun 5, 2025
The issue of /ActualText being ignored is also important for CJK language users. There is a related issue in Chromium where /ActualText with CJK characters was ignored, that has recently been fixed. See the Chromium issue:
As described in that issue, PDFs generated by Chromium have a problem where some CJK characters are replaced with Kangxi radicals if the PDF viewer does not handle ActualText properly.
The /ActualText in PDF is an essential feature for accessibility, and I hope PDF.js can handle it as well.
bungeman commentedon Jun 9, 2025
To get some idea of the issues one might run into while implementing this I created a patch bungeman@6a52783 . This simply reads and stores the
/ActualTextand then adds it as an attribute to thespanin the text layer. However, it isn't clear exactly how emit the/ActualTextinto the text layer.olyapop commentedon Sep 19, 2026
Another case: Inter's case-sensitive forms of
(,)and-(glyph IDs 0x544, 0x545, 0x55E) have no Unicode mapping in the embedded font's ToUnicode, cmap or post tables, so getTextContent returns U+0000 for them. The PDF wraps each glyph in an /ActualText span with the intended character, and other readers extract it correctly. I reproduced this on 6.3.289. I can't share the file, but I can put together a minimal repro if that would help.abgrad commentedon Oct 23, 2026
Same root cause as @olyapop's case, with a minimal reproducer and a patch.
Chrome up to version 128 prints a variable font (Inter with a
wghtaxis, in our case) as Type3 glyphs whose/ToUnicodemaps digits, spaces and punctuation to U+0000, and wraps each of them in/Span << /ActualText (7) >> BDC … EMC. PDFium, MuPDF and Preview read the ActualText and copy an offer's number, date and amount as printed. pdf.js 6.3.289 and 6.4.299 return U+0000 for every digit, space and punctuation mark, so an amount copies as€:getTextContentreads onlyMCIDfrom the property dictionary and neverActualText.Reproducer (in the PR as
test/pdfs/actualtext_type3.pdf, 1.5 KB, hand-written): "Total: " in Helvetica, one Type3 glyph drawn as a 7 and mapped to U+0000, inside/Span << /ActualText (7) >>, then " EUR". PDFium extractsTotal: 7 EUR; pdf.js extractsTotal: EURwith U+0000 where the 7 should be.On the open question from @bungeman's prototype (how to emit it into the text layer): what works for us in production is to hand the text along on the
beginMarkedContentPropsitem and let the consumer apply it. The span's text goes to its first non-empty text item, the remaining items of the span become empty, and the empty line-start items (hasEOL) inside the span are left alone, so the amount still copies as one line. #22315 adds the first half (the property); applying it insidegetTextContentitself could follow once the text-layer semantics are agreed.