Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render Text issues when using tabs in Chrome #60

Closed
hamdi3 opened this issue Aug 7, 2024 · 16 comments · Fixed by #63
Closed

Render Text issues when using tabs in Chrome #60

hamdi3 opened this issue Aug 7, 2024 · 16 comments · Fixed by #63
Assignees
Labels

Comments

@hamdi3
Copy link

hamdi3 commented Aug 7, 2024

I've realized that when a pdf is run in tabs with render_text set to True it would only work for the selcted tab as the page loads

            paths = glob("../../*/*.pdf")
            for tab,path in zip(streamlit.tabs(paths),paths):
                with tab:
                    with streamlit.container(height=600):
                        pdf_viewer(path, width=800, render_text=True)

When trying to inspect it on the browser I've seen the following warnings on the console (for each document except the first one):

Warning: Setting up fake worker.
Warning: TT: undefined function: 32

As always thanks for the good work and support :)

@lfoppiano
Copy link
Owner

Hi @hamdi3,
thanks for reporting back of a problem.

This is indeed weird. In one of the unit tests that I wrote I check also that the PDF on the second tab contains rendered text. 🤔 Nevertheless, I tried with a code similar to your and it looks working.
Are you sure the PDF does contains text? It could be that the PDF is an image PDF? Have you tried to change the order on which the PDF are loaded?

I did a test with 3 pdf documents. How many do you load?
image

The message Warning: Setting up fake worker. is a normal pdf.js warning. I don't think is harmless.

@lfoppiano lfoppiano self-assigned this Aug 7, 2024
@hamdi3
Copy link
Author

hamdi3 commented Aug 8, 2024

I've tried again, whatever pdf I'm rendering in the selected tab, as the page loads, is the only one with rendered texts (I've tried it on all my pdfs iteratively so I'm sure it's not pdf Issue).

I've tried it with 2-3-10-15-20 pdfs and had the same issue.

I'm using Chrome Version 127.0.6533.89 and unfortunately can't test it on another browser, could this be a browser issue?

@lfoppiano
Copy link
Owner

OK. It's a problem with Chrome. 😭

@lfoppiano lfoppiano added browser:chrom* help wanted Extra attention is needed labels Aug 8, 2024
@lfoppiano
Copy link
Owner

lfoppiano commented Aug 9, 2024

First answer about the underfined function is nothing to worry about, just due to fonts.

I'll look into the other part, maybe I need the help of @t29mato who's a JS and Chrome Guru! 😅

I'm trying to catch and ignore these exception when we call the page.render, but I'm surely doing something wrong:

    try {
        const renderTask = page.render(renderContext);
        await renderTask.promise.catch(function(error){
          // alertError(error);
          // do nothing
        });
      } catch (e) {
        // do nothing
      }

@hamdi3
Copy link
Author

hamdi3 commented Aug 9, 2024

@lfoppiano As always thanks for the hard work and support :)

@lfoppiano lfoppiano changed the title Render Text issues when using tabs Render Text issues when using tabs in Chrome Aug 19, 2024
@lfoppiano lfoppiano assigned t29mato and unassigned lfoppiano Aug 30, 2024
@t29mato
Copy link
Collaborator

t29mato commented Sep 3, 2024

The PR mozilla/pdf.js#18283 added the feature to support browsers' user-configurable minimum font size. However, Google Chrome's minimum font size becomes 0 when using Streamlit's tab functionality.

PDF.js implements the text dragging feature by overlaying transparent text on the PDF. But, when the font size is set to 0, there is no text to drag, causing the feature to fail.

Code

https://github.com/mozilla/pdf.js/pull/18283/files#diff-eb5220436f55f0c6ff687c15781981b323be818cb77e49d53d66f7f372f1f23dR475-R491
ensureMinFontSizeComputed method is supposed to return more than 1, but it returns 0 when the feature doesn't work.

Temporary Solution

Downgrading to the version 4.3.136 which does not support the minimum font size, restores the drag-and-drop functionality.

Additional Information

I want to explain why the custom pdf_viewer component works when RELEASE=FALSE.

When RELEASE=FALSE, the JavaScript runs each time you click on a tab. This lets it correctly get the minimum font size, even in Google Chrome.

But when RELEASE=TRUE, the JavaScript tries to get the minimum font size for all tabs as soon as the page loads, which can cause issues.

@lfoppiano
Copy link
Owner

@t29mato can you please try to make a patch that tries to force the text characters to be greater than zero?

@t29mato
Copy link
Collaborator

t29mato commented Sep 10, 2024

@lfoppiano

db7a26d
Okay, I made a patch branch. You need to do npm install before run the application

@lfoppiano
Copy link
Owner

@t29mato I made rc1 but it doesn't seems to work.
@hamdi3 could you please give it a try to version 0.0.18-rc1?

@t29mato
Copy link
Collaborator

t29mato commented Sep 11, 2024

@lfoppiano
I fixed it by downgrading the PDFjs version from 4.5.136 to 4.3.136, so updating it further won’t fix the issue.
db7a26d

@lfoppiano
Copy link
Owner

lfoppiano commented Sep 11, 2024

@t29mato that I understood :-)
I made a release 0.0.18rc1 and tested here, but did not seems to work.

Update: you have to manually update to 0.0.18rc1

@t29mato
Copy link
Collaborator

t29mato commented Sep 11, 2024

@lfoppiano
It doesn't work on 0.0.18rc1 because it specifies the PDF-js version as ^4.6.82 here.
https://github.com/lfoppiano/streamlit-pdf-viewer/blob/v0.0.18-rc1/streamlit_pdf_viewer/frontend/package.json

Or am I misunderstanding something...

@lfoppiano
Copy link
Owner

mmm, maybe you pushed on a different branch than fix-problem-with-chrome?

@t29mato
Copy link
Collaborator

t29mato commented Sep 11, 2024

@lfoppiano
Copy link
Owner

Ok. I've released rc2, tested it works on Chome. @hamdi3 could you please try it?

@lfoppiano
Copy link
Owner

@hamdi3 if you have still problems, let us know, and feel free to reopen this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants