Skip to content

Commit 4962d3b

Browse files
committed
Fix customTextRenderer called more times than there are textContent items
Related to #1151
1 parent 5cdd8a0 commit 4962d3b

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ Displays a page. Should be placed inside `<Document />`. Alternatively, it can h
405405
| canvasBackground | Canvas background color. Any valid `canvas.fillStyle` can be used. If you set `renderMode` to `"svg"` this prop will be ignored. | n/a | `"transparent"` |
406406
| canvasRef | A prop that behaves like [ref](https://reactjs.org/docs/refs-and-the-dom.html), but it's passed to `<canvas>` rendered by `<PageCanvas>` component. If you set `renderMode` to `"svg"` this prop will be ignored. | n/a | <ul><li>Function:<br />`(ref) => { this.myPage = ref; }`</li><li>Ref created using `React.createRef`:<br />`this.ref = React.createRef();`<br />…<br />`inputRef={this.ref}`</li><li>Ref created using `React.useRef`:<br />`const ref = React.useRef();`<br />…<br />`inputRef={ref}`</li></ul> |
407407
| className | Class name(s) that will be added to rendered element along with the default `react-pdf__Page`. | n/a | <ul><li>String:<br />`"custom-class-name-1 custom-class-name-2"`</li><li>Array of strings:<br />`["custom-class-name-1", "custom-class-name-2"]`</li></ul> |
408-
| customTextRenderer | Function that customizes how a text layer is rendered. | n/a | `` ({ str, itemIndex }) => str && str.replace(/ipsum/g, `<mark>ipsum</mark>`) `` |
408+
| customTextRenderer | Function that customizes how a text layer is rendered. | n/a | `` ({ str, itemIndex }) => str.replace(/ipsum/g, `<mark>ipsum</mark>`) `` |
409409
| error | What the component should display in case of an error. | `"Failed to load the page."` | <ul><li>String:<br />`"An error occurred!"`</li><li>React element:<br />`<div>An error occurred!</div>`</li><li>Function:<br />`this.renderError`</li></ul> |
410410
| height | Page height. If neither `height` nor `width` are defined, page will be rendered at the size defined in PDF. If you define `width` and `height` at the same time, `height` will be ignored. If you define `height` and `scale` at the same time, the height will be multiplied by a given factor. | Page's default height | `300` |
411411
| imageResourcesPath | The path used to prefix the src attributes of annotation SVGs. | n/a (pdf.js will fallback to an empty string) | `"/public/images/"` |

src/Page/TextLayer.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,18 @@ export class TextLayerInternal extends PureComponent {
141141
cancellable.promise
142142
.then(() => {
143143
if (customTextRenderer) {
144-
Array.from(this.layerElement.current.children).forEach((element, elementIndex) => {
144+
textContent.items.forEach((item, itemIndex) => {
145+
const child = this.layerElement.current.children[itemIndex];
146+
145147
const content = customTextRenderer({
146-
itemIndex: elementIndex,
147-
...textContent.items[elementIndex],
148+
itemIndex,
149+
...item,
148150
});
149-
element.innerHTML = content;
151+
152+
child.innerHTML = content;
150153
});
151154
}
155+
152156
this.onRenderSuccess();
153157
})
154158
.catch((error) => {

test/Test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default function Test() {
183183
renderTextLayer,
184184
scale: pageScale,
185185
width: pageWidth,
186-
customTextRenderer: ({ str }) => str && str.replace(/ipsum/g, `<mark>ipsum</mark>`),
186+
customTextRenderer: ({ str }) => str.replace(/ipsum/g, `<mark>ipsum</mark>`),
187187
};
188188
}
189189

0 commit comments

Comments
 (0)