React-pdf: Strange font behavior when trying to highlight text in the annotation layer with the mark tag.

Created on 21 Jun 2019  ·  4Comments  ·  Source: wojtekmaj/react-pdf

Description

Strange font behavior when trying to highlight text in the annotation layer with the mark tag.

I'm displaying a pdf in a react-bootstrap modal dialog. I'm trying to get highlight search terms on the page using the customTextRenderer prop of the Page component. The highlighted text is rendered in a different font and not quite at the right location.

Steps to reproduce

Steps to reproduce the behavior:
The code looks like this:

<Modal show={this.state.showPDF}

    onHide={this.handleClose} dialogClassName="modal-100w">
    <Modal.Header closeButton>
    </Modal.Header>
    <Modal.Body style={{margin: "auto"}}>
    <Document file={this.state.pdfURL} onLoadSuccess={(page) => this.onDocumentLoadSuccess(page)}>
        <Page  renderMode="svg" scale={1.0} pageNumber={1} customTextRenderer={(textItem) => this.customTextRenderer(textItem)} onLoadSuccess={(page) => this.onPageLoadSuccess(page)} />
        </Document>
    </Modal.Body>
</Modal>

and customTextRenderer looks like this:

return textItem.str
              .split(this.terms[0])
              .reduce((strArray, currentValue, currentIndex) => (
                currentIndex === 0
                  ? ([...strArray, currentValue])
                  : ([...strArray, (
                    // eslint-disable-next-line react/no-array-index-key
                    <mark key={currentIndex}>
                      {this.terms[0]}
                    </mark>
                  ), currentValue])
    ), []);

    }

(taken mainly from one of your samples, I think)

Expected behavior

The highlighted text show should in the same location and in the same font and at the same location.

Additional information
I've tried with both svg and canvas but see the same behavior.

The same behavior was encountered in #189, but I've not had any luck with any workarounds. I've also looked at #212.

Screen Shot 2019-06-21 at 11 33 00 AM

Environment

  • Browser (if applicable) [e.g. Chrome 57, Firefox 59]: I've tested in the latest of both chrome at firefox. The behavior is glitchy in both.
  • React-PDF version [e.g. 3.0.4]:
  • React version [e.g. 16.3.0]:
  • Webpack version (if applicable) [e.g. 4.16.2]:
question

Most helpful comment

That's what I use in my own test suite BTW :) Feel free to copy and paste!

https://github.com/wojtekmaj/react-pdf/blob/5f9a1928a4ce49f97bca2b5d5e68df63c99725c8/test/Test.less#L126-L132

All 4 comments

Hi,
due to how PDFs are rendered, it's not always possible for the text layer to be exactly matching the text underneath. I'm sorry about that :( There is a lot of tricks Mozilla and I are using for this text to align. What I can suggest is to implement <mark> similarly to how Mozilla does it, so with transparent text, and semi-transparent background. This way, the properly aligned, properly rendered text will "shine through" the selection.

I'll give that a shot. thanks for the suggestion.

That's what I use in my own test suite BTW :) Feel free to copy and paste!

https://github.com/wojtekmaj/react-pdf/blob/5f9a1928a4ce49f97bca2b5d5e68df63c99725c8/test/Test.less#L126-L132

This worked perfectly. Thanks so much for the suggestion, for some reason hadn't thought enough about the CSS. And thanks to for your work on the library! It's been a pleasure to use it.

Was this page helpful?
0 / 5 - 0 ratings