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
Operating system and its version: macOS Catalina 10.15.5
PDF.js version: 2.4.456
Is a browser extension: No
Steps to reproduce the problem:
Load a PDF using the PDFViewer component
Extract the text of the PDF using viewer.getPageView(i).textLayer.textContentItemsStr for each page
What is the expected behavior?
What went wrong?
Link to a viewer: N/A
As we can see the dots are properly displayed in the <canvas> but they are not present in the text layer, which prevents extracting them using the method I described. It is very important for me to retrieve those dots because I'm doing natural-language processing on the PDF text to detect the sentences, but if there are no dots to close the sentences they are considered to be one big sentence instead of several ones.
Extract the text of the PDF using viewer.getPageView(i).textLayer.textContentItemsStr for each page
Please don't use that, since that's internal functionality which is not guaranteed to work (it could theoretically stop working tomorrow) and it's also not guaranteed to even be defined in general. E.g. not all pages even have a textLayer, the textLayer property is only defined after a page has finished rendering, and finally note that the actual textLayer-content is built asynchronously as well.
The correct approach here would be use this API method:
As for the text-selection/text-extraction issue at hand: This is a bug in the /ToUnicode data of the font that's used for this text, since it explicitly maps . to the space-character.
As for the text-selection/text-extraction issue at hand: This is a bug in the /ToUnicode data of the font that's used for this text, since it explicitly maps . to the space-character.
Thank you for your answer. I have used pdf2htmlEX (https://github.com/coolwanglu/pdf2htmlEX) on this PDF and it worked well, the dots were correctly parsed. When I check the output of pdf2htmlEX I see that there are indeed problems with the font, but in order to work properly it drops the font for a better one, and honestly I think that the result is seamless.
Simply ignoring the entire /ToUnicode data is (most likely) bound to lead to much worse text-selection behaviour in general, even if you manage to limit it to "bad" data (which you'd need to detect without false positives), hence I don't think that it's a good idea. Also, it's not always possible to tell with code if a particular /ToUnicode data is "bad", without a human doing the actual checking.
Do you know how the detection of bad /ToUnicode data was made for pdf2htmlEX then?
No, since that's a completely different project which we don't know anything about here :-)
Generally speaking: We're currently attempting to parse as much as possible of the /ToUnicode data, which is most likely improving text-selection in some documents. Hence it's unfortunately quite unlikely that we'd accept a patch which limits this, since that'd risk regressing text-selection in other documents (and we may not have sufficient test-coverage to catch that).
Ok thank you for your help, I guess I cannot use PDF.js then because extracting the dots is critical for my project.
But I don't really understand why you wouldn't accept outright a patch that fixes this issue for PDF.js the same way that pdf2htmlEX handles it. You admitted that you don't know how it works, so maybe it deals with bad /ToUnicode problems in a really clever way. I get that maybe this solution improves text-selection only for the PDF I gave but for other PDFs it performs worse, but having used both technologies I must say that pdf2htmlEX has always done a proper job for me.
But if you want to be sure of this, you could create a dataset of several PDFs associated with their true text content. You can test this dataset using the 2 methods: the current PDF.js method and the pdf2htmlEX method. The method that gives the extracted text the closest to the true text content is the best one. This test method could be useful to check every time there is a modification to the text parsing algorithm of PDF.js, to see if it improved or not the performance.
you could create a dataset of several PDFs associated with their true text content.
We already have reference-tests of the text kind, which will thus catch text-selection regressions; my point was simply that our test-coverage may be limited and that dealing with broken PDF documents is tricky in general.
Sorry to drag this out, but I checked the same PDF with PDFium, the PDF renderer of Chrome, and all the characters (including the dots) were parsed perfectly. The problem is, unlike PDF.js, PDFium does not expose an API for developers. What I'm trying to say is that there is a solution to the problem of bad /ToUnicode maps, and it is not OCR. I'm just a bit disappointed that Firefox is a bit behind Chrome for this particular issue.
The reason that it works with other software is that the content stream of this PDF uses "/ActualText" to repair the mess done by a (probably) incomplete /ToUnicode stream in that font.
/Span << /Lang (en-US) /MCID 106 >> BDC
BT
0.259 0.302 0.337 rg
/T1_0 1 Tf
9 0 0 9 152.5669 726.2613 Tm
(, also known as APT15, is a threat group believed to be operating out of China) Tj
/Span << /ActualText (\376\377\000.) >> BDC
(.) Tj
EMC
[ ( Its) 20 ( ) ] TJ
ET
EMC
@THausherr Yes thank you, I had a similar answer when I asked how Acrobat Reader does to correctly detect all the characters. And will this feature be implemented in PDF.js in a foreseeable future?
@TimotheAlbouy I'm not with this project, I just hang around here, and look at weird PDFs for "my" PDF project (which has the same problem with this file).
Activity
Snuffleupagus commentedon Jul 17, 2020
Please don't use that, since that's internal functionality which is not guaranteed to work (it could theoretically stop working tomorrow) and it's also not guaranteed to even be defined in general. E.g. not all pages even have a
textLayer, thetextLayerproperty is only defined after a page has finished rendering, and finally note that the actualtextLayer-content is built asynchronously as well.The correct approach here would be use this API method:
pdf.js/src/display/api.js
Lines 1199 to 1204 in a604973
as is e.g. done when searching in the default viewer:
pdf.js/web/pdf_find_controller.js
Lines 464 to 472 in a604973
As for the text-selection/text-extraction issue at hand: This is a bug in the /ToUnicode data of the font that's used for this text, since it explicitly maps
.to thespace-character.TimotheAlbouy commentedon Jul 17, 2020
Thank you for your answer. I have used
pdf2htmlEX(https://github.com/coolwanglu/pdf2htmlEX) on this PDF and it worked well, the dots were correctly parsed. When I check the output ofpdf2htmlEXI see that there are indeed problems with the font, but in order to work properly it drops the font for a better one, and honestly I think that the result is seamless.Is it possible to do that with PDF.JS as well?
Snuffleupagus commentedon Jul 17, 2020
Simply ignoring the entire /ToUnicode data is (most likely) bound to lead to much worse text-selection behaviour in general, even if you manage to limit it to "bad" data (which you'd need to detect without false positives), hence I don't think that it's a good idea. Also, it's not always possible to tell with code if a particular /ToUnicode data is "bad", without a human doing the actual checking.
TimotheAlbouy commentedon Jul 17, 2020
Do you know how the detection of bad /ToUnicode data was made for pdf2htmlEX then?
Snuffleupagus commentedon Jul 17, 2020
No, since that's a completely different project which we don't know anything about here :-)
Generally speaking: We're currently attempting to parse as much as possible of the /ToUnicode data, which is most likely improving text-selection in some documents. Hence it's unfortunately quite unlikely that we'd accept a patch which limits this, since that'd risk regressing text-selection in other documents (and we may not have sufficient test-coverage to catch that).
TimotheAlbouy commentedon Jul 17, 2020
Ok thank you for your help, I guess I cannot use PDF.js then because extracting the dots is critical for my project.
But I don't really understand why you wouldn't accept outright a patch that fixes this issue for PDF.js the same way that pdf2htmlEX handles it. You admitted that you don't know how it works, so maybe it deals with bad /ToUnicode problems in a really clever way. I get that maybe this solution improves text-selection only for the PDF I gave but for other PDFs it performs worse, but having used both technologies I must say that pdf2htmlEX has always done a proper job for me.
But if you want to be sure of this, you could create a dataset of several PDFs associated with their true text content. You can test this dataset using the 2 methods: the current PDF.js method and the pdf2htmlEX method. The method that gives the extracted text the closest to the true text content is the best one. This test method could be useful to check every time there is a modification to the text parsing algorithm of PDF.js, to see if it improved or not the performance.
Snuffleupagus commentedon Jul 17, 2020
We don't (generally) accept any patches which regress existing test-cases. If you're able to fix this issue, without regressing anything else, there's nothing stopping you from submitting a PR (although it's impossible to guarantee beforehand that it'll be accepted); please see https://github.com/mozilla/pdf.js/wiki/Contributing#4-run-lint-and-testing
We already have reference-tests of the
textkind, which will thus catch text-selection regressions; my point was simply that our test-coverage may be limited and that dealing with broken PDF documents is tricky in general.TimotheAlbouy commentedon Jul 18, 2020
Sorry to drag this out, but I checked the same PDF with PDFium, the PDF renderer of Chrome, and all the characters (including the dots) were parsed perfectly. The problem is, unlike PDF.js, PDFium does not expose an API for developers. What I'm trying to say is that there is a solution to the problem of bad /ToUnicode maps, and it is not OCR. I'm just a bit disappointed that Firefox is a bit behind Chrome for this particular issue.
THausherr commentedon Jul 20, 2020
The reason that it works with other software is that the content stream of this PDF uses "/ActualText" to repair the mess done by a (probably) incomplete /ToUnicode stream in that font.
TimotheAlbouy commentedon Jul 20, 2020
@THausherr Yes thank you, I had a similar answer when I asked how Acrobat Reader does to correctly detect all the characters. And will this feature be implemented in PDF.js in a foreseeable future?
THausherr commentedon Jul 20, 2020
@TimotheAlbouy I'm not with this project, I just hang around here, and look at weird PDFs for "my" PDF project (which has the same problem with this file).
timvandermeij commentedon Aug 18, 2020
Closing in favor of #12237 which has more details.