Comments (7)
Use str.includes() is pretty optimistic. Not sure that this way it is possible to find anything compare to original search controller:
https://github.com/mozilla/pdf.js/blob/master/web/pdf_find_controller.js
Ideally it would be great to integrate PDFFindController and mimic linkService, eventBus and search layer interfaces and functions. But.. it will require some work.
from vue-pdf.
Text searching is usually a default feature of browsers, highlighting as well, i don't get the why the package should do it.
from vue-pdf.
Consider the case of displaying one page at a time. The browser can only search the displayed pages.
from vue-pdf.
In that case, this workaround could works:
const { pdf, pages } = usePDF("https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf")
async function searchText(text) {
const documentProxy = await pdf.value?.promise
const occurrences = {}
for (let page = 1; page <= pages.value; page++) {
const pageProxy = await documentProxy.getPage(page)
const textContent = await pageProxy.getTextContent()
const filteredItems = textContent.items.filter(value => value.str.includes(text))
if (filteredItems.length > 0)
occurrences[page] = filteredItems
}
return occurrences
}
For example, calling searchText('Trace Trees')
will return this object with the text being found on page 4 and 5:
{
"4":[
{
"str":"Trace Trees",
"dir":"ltr",
"width":54.476691899999985,
"height":10.9589,
"transform":[
10.9589,
0,
0,
10.9589,
73.17807499999991,
535.4919000000002
],
"fontName":"g_d0_f1",
"hasEOL":false
}
],
"5":[
{
"str":"Trace Trees",
"dir":"ltr",
"width":44.57197440000001,
"height":8.9664,
"transform":[
8.9664,
0,
0,
8.9664,
74.17440000000002,
228.93460000000024
],
"fontName":"g_d0_f1",
"hasEOL":false
}
]
}
from vue-pdf.
Thanks @TaTo30! I have a use case that requires me to search keywords and highlight the results on the pdf. Would this be possible or is there a workaround for this too?
from vue-pdf.
Would be possible by searching the html element and replace the content but i know that is awful to do. I think both features would be good to implement, i'll think about it
from vue-pdf.
In that case, this workaround could works:
const { pdf, pages } = usePDF("https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf") async function searchText(text) { const documentProxy = await pdf.value?.promise const occurrences = {} for (let page = 1; page <= pages.value; page++) { const pageProxy = await documentProxy.getPage(page) const textContent = await pageProxy.getTextContent() const filteredItems = textContent.items.filter(value => value.str.includes(text)) if (filteredItems.length > 0) occurrences[page] = filteredItems } return occurrences }For example, calling
searchText('Trace Trees')
will return this object with the text being found on page 4 and 5:{ "4":[ { "str":"Trace Trees", "dir":"ltr", "width":54.476691899999985, "height":10.9589, "transform":[ 10.9589, 0, 0, 10.9589, 73.17807499999991, 535.4919000000002 ], "fontName":"g_d0_f1", "hasEOL":false } ], "5":[ { "str":"Trace Trees", "dir":"ltr", "width":44.57197440000001, "height":8.9664, "transform":[ 8.9664, 0, 0, 8.9664, 74.17440000000002, 228.93460000000024 ], "fontName":"g_d0_f1", "hasEOL":false } ] }
这种方式,只能匹配关键字,无法搜索匹配比较长的内容,因为items里的str 是很短的内容。有其它办法吗?
from vue-pdf.
Related Issues (20)
- Save rotation angle HOT 1
- Style.css not exported for Text Layer Implementation HOT 2
- Switching between multiple PDFs causes crash HOT 1
- Allow VuePDF component to also handle data loading state HOT 1
- Security Advisory in pdf.js HOT 2
- Resizing with reload method not working
- Resizing with reload method not working HOT 3
- Errors when reloading PDF HOT 9
- How can I use this library without upgrading my Vue version? HOT 1
- Can it support on-demand loading and virtual scrolling to solve the loading and memory consumption issues of large PDF files? HOT 1
- Support for `pdfjs` v4
- Viewer options HOT 1
- Highlight a piece of text HOT 2
- Chinese Text Not Displaying in vue-pdf HOT 3
- There is a "fit-parent" example, but not in menu HOT 1
- require() of ES Module HOT 1
- 输出警告后导致vue3页面空白 HOT 3
- Add a demo site to this project HOT 2
- How to highlight multiple lines of text HOT 8
- div "container" overflow:hidden prevents horizontal scroll HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-pdf.