-
-
Notifications
You must be signed in to change notification settings - Fork 918
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
Search Functionality #189
Comments
Hey! See: https://github.com/wojtekmaj/react-pdf/blob/master/test/Test.jsx#L116-L125 |
You might have canvas auto-scaled down to fit your container. Make sure to use proper Page width. |
Thanks for all your help, was hoping you might have some insight on a wabpack worker issue I am having now. I am seeing the following error when starting my application [WARN] 404 - GET /b898199c77b935089e6c.worker.js (127.0.0.1) 314 bytes This causes the viewer to sit at the "Loading Pdf" message when attempting to load a document. I am using the create-react-app and importing via import { Document, Page } from 'react-pdf/dist/entry.webpack'; |
Hey, please keep one topic on one issue, otherwise we can't possibly have any control over it. Please see #164 - it seems like a similar issue. |
Hey @GlenBrauer! How did it go with the search and highlight feature? I'm thinking of building the same. Programatically search and highlight in the PDF. |
See also #212 |
"You might have canvas auto-scaled down to fit your container. Make sure to use proper Page width." can you elaborate more on that pls. |
@frontr-uk If you're rendering canvas larger than its container, CSS might scale it down. You should make sure the |
For anyone looking to add search to their application, I made this simple import { useState, useEffect } from "react";
import { pdfjs } from "react-pdf";
export const usePdfTextSearch = (file, searchString) => {
const [pages, setPages] = useState([]);
const [resultsList, setResultsList] = useState([]);
useEffect(() => {
pdfjs.getDocument(file).promise.then((docData) => {
const pageCount = docData._pdfInfo.numPages;
const pagePromises = Array.from(
{ length: pageCount },
(_, pageNumber) => {
return docData.getPage(pageNumber + 1).then((pageData) => {
return pageData.getTextContent().then((textContent) => {
return textContent.items.map(({ str }) => str).join(" ");
});
});
}
);
return Promise.all(pagePromises).then((pages) => {
setPages(pages);
});
});
}, [file]);
useEffect(() => {
if (!searchString || !searchString.length) {
setResultsList([]);
return;
}
/*
Currently this regex is case-insensitive. This could be extended to be configurable.
Or could be extended to be a fuzzy search. Fuzzy search would need a more
complex return from the hook to be able to highlight the found term(s) in the view.
EX: resultsList = Array<{ pageNumber: number, matchedTerms: Array<string> }>
*/
const regex = new RegExp(`${searchString}*`, "i");
const updatedResults = [];
pages.forEach((text, index) => {
if (regex.test(text)) {
updatedResults.push(index + 1);
}
});
setResultsList(updatedResults);
}, [pages, searchString]);
return resultsList;
}; |
Is there a way to utilize the search functionality of PDF.js that you get when you click the magnifying icon from the toolbar?
The text was updated successfully, but these errors were encountered: