question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

some special texts can't be matched when I use Page.customTextRenderer try to match and highlight that user inputed keyword

See original GitHub issue

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo

Description

image This is displayed in Chrome

image This is displayed by react-pdf

This is part of codes:

{selFile && !!selFile.isPdf && (
              <Document
                file={selFile.attachment_path}
                options={options}
                onLoadSuccess={onDocumentLoadSuccess}
                onPassword={onDocumentPassword}
                loading={<Spin size="large" />}
              >
                <div
                  className={cs('content', 'pdf', {
                    'zoom-in': canZoomByGesture && scale < maxScale,
                    'zoom-out': canZoomByGesture && scale >= maxScale
                  })}
                  onWheelCapture={canZoomByGesture ? onWheelCapture : undefined}
                  style={childStyle}
                >
                  <div className='insideImgWr'>
                    <Page
                      pageNumber={page}
                      scale={scale + 0.4}
                      loading={<Spin size="large" />}
                      canvasRef={canvasRef}
                      customTextRenderer={customTextRenderer}
                    />
                    <WaterMarkConsumer>
                      <div className='insideWatermark' />
                    </WaterMarkConsumer>
                  </div>
                </div>
              </Document>
            )}


  const customTextRenderer = useCallback((textItem) => {
    if (!textItem) return null;
    return highlightPattern(textItem.str, resultsList && resultsList.length ? searchString || '' : '');
  }, [resultsList, searchString]);



function highlightPattern(text: string, pattern: string) {
  const splitText = text.split(pattern);

  console.log('xxx highlightPattern: ', text);
  

  if (splitText.length <= 1) {
    return text;
  }

  const matches = text.match(pattern);

  return splitText.reduce(
    (arr, element, index) =>
      // @ts-ignore
      matches[index]
        ? [...arr, element, <mark key={index} className='gallery-hightlight'>{matches[index]}</mark>]
        : [...arr, element],
    []
  );
}

Steps to reproduce

compare between Chrome PDF viewer and react-pdf of displaying a Chinese PDF. One row content has part missing disaplayed, but it is displayed correctly at Chrome PDF viewer.

Expected behavior

displayed correctly

Actual behavior

missing displayed

Additional information

No response

Environment

  • Browser (if applicable):Chrome
  • React-PDF version:5.6.0
  • React version:17.0.2
  • Webpack version (if applicable):5.65.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
XingToYangcommented, Apr 1, 2022
import { pdfjs, Document, Page } from 'react-pdf/dist/esm/entry.webpack';
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';

const options = {
  cMapUrl: 'cmaps/',
  cMapPacked: true,
  standardFontDataUrl: 'standard_fonts/'
};

// unset pdfjs.GlobalWorkerOptions.workerSrc, then the special texts now is displayed
// pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

<Document
                file={selFile.attachment_path}
                options={options}
                onLoadSuccess={onDocumentLoadSuccess}
                onPassword={onDocumentPassword}
                loading={<Spin size="large" />}
              >

I’ve configured cMaps and when I unset pdfjs.GlobalWorkerOptions.workerSrc, the not-displayed texts now is displayed

0reactions
github-actions[bot]commented, Aug 1, 2022

This issue was closed because it has been stalled for 14 days with no activity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-pdf "Highlight text on the page" is not working
I am trying to implement React-pdf pdf highlighting feature: import React, { useMemo, useState } from 'react'; // import { Document, Page } ......
Read more >
Create A React Search Bar That Highlights Your Results
Takes a text input; Filters the results containing said text; Highlighting that very text. I'd made something similar to this a long time ......
Read more >
Highlight Searched Text With Javascript | HTML, CSS & JS
... highlight words on search using Javascript. The user enters some text in the search box and hits the search button. All the...
Read more >
Viewing online file analysis results for 'tmp53_x2rlm'
String Context Stream UID https://api.mojang.com Domain/IP reference 850444b606a5c7f3fae0c2057... https://meteorclient.com/api/online/ping Domain/IP reference 850444b606a5c7f3fae0c2057... os.name Domain/IP reference 850444b606a5c7f3fae0c2057...
Read more >
Working with PDF in React - Medium
But any further manipulations with the PDF becomes… ... text matching each other , the mark tag returns the match that gets highlighted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found