Skip to content
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

Cannot use the same canvas during multiple render() operations #10018

Closed
atnsm opened this issue Aug 28, 2018 · 16 comments
Closed

Cannot use the same canvas during multiple render() operations #10018

atnsm opened this issue Aug 28, 2018 · 16 comments
Labels

Comments

@atnsm
Copy link

atnsm commented Aug 28, 2018

Using latest pdfjs version (2.0.550) I get the following error:

Cannot use the same canvas during multiple render() operations. Use different canvas or ensure previous operations were cancelled or completed.

The code I am using for rendering is as follow:

function renderPage(pageNumber, renderOptions) {
                var documentId = renderOptions.documentId;
                var pdfDocument = renderOptions.pdfDocument;
                var scale = renderOptions.scale;
                var rotate = renderOptions.rotate;
                // Load the page and annotations
                return Promise.all([pdfDocument.getPage(pageNumber), _PDFJSAnnotate2.default.getAnnotations(documentId, pageNumber)]).then(function (_ref) {
                    var _ref2 = _slicedToArray(_ref, 2);
                    var pdfPage = _ref2[0];
                    var annotations = _ref2[1];
                    var page = document.getElementById('pageContainer' + pageNumber);
                    var svg = page.querySelector('.annotationLayer');
                    var canvas = page.querySelector('.canvasWrapper canvas');
                    var canvasContext = canvas.getContext('2d', {alpha: false});
                    var viewport = pdfPage.getViewport(scale, rotate);
                    var transform = scalePage(pageNumber, viewport, canvasContext);
                    var renderContext = {
                        canvasContext: canvasContext,
                        viewport: viewport,
                        transform: transform
                    };
                    // Render the page
                    return Promise.all([pdfPage.render(renderContext), _PDFJSAnnotate2.default.render(svg, viewport, annotations)]).then(function () {
                        // Text content is needed for a11y, but is also necessary for creating
                        // highlight and strikeout annotations which require selecting text.
                        return pdfPage.getTextContent({normalizeWhitespace: true}).then(function (textContent) {
                            return new Promise(function (resolve, reject) {// Render text layer for a11y of text content
                                var textLayer = page.querySelector('.textLayer');
                                var textLayerFactory = new pdfjsViewer.DefaultTextLayerFactory();
                                var textLayerBuilder = textLayerFactory.createTextLayerBuilder(textLayer, pageNumber - 1, viewport);
                                textLayerBuilder.setTextContent(textContent);
                                textLayerBuilder.render();// Enable a11y for annotations
                                // Timeout is needed to wait for `textLayerBuilder.render`
                                setTimeout(function () {
                                    try {
                                        (0, _renderScreenReaderHints2.default)(annotations.annotations);
                                        resolve();
                                    } catch (e) {
                                        reject(e);
                                    }
                                });
                            });
                        });
                    }).then(function () {// Indicate that the page was loaded
                        page.setAttribute('data-loaded', 'true');
                        return[pdfPage, annotations];
                    });
                });
            }

That part of the code is from annotation js project:
https://github.com/instructure/pdf-annotate.js/blob/master/docs/index.js

As you can also see every pages hast it own canvas. So, I don't know why I still get such error.

What is going wrong and where I need to look into?

@kishandonepudi
Copy link

@atnsm bro can i know whether you're using PDF-annotate in Angular 2+?

@atnsm
Copy link
Author

atnsm commented Sep 11, 2018

I do not think so. Here is the list of all dependencies:
https://github.com/instructure/pdf-annotate.js/blob/master/package.json

@gwl002
Copy link

gwl002 commented Sep 17, 2018

Same problem,how to fix this.

@andreieftimie
Copy link

Seeing the same error with the react-pdf-js which is a wrapper around pdfjs-dist.

@Snuffleupagus
Copy link
Collaborator

Using latest pdfjs version (2.0.550) I get the following error:

It's quite possible that this issue is a duplicate of #9456, in which case it's been fixed by PR #9853 (which didn't land in time to be included in version 2.0.550).

@timvandermeij
Copy link
Contributor

I would also say so. Let's close this and we can always reopen if the final 2.0 release is done and the problem remains.

@nise
Copy link

nise commented Feb 26, 2019

I run into the same problem. Help needed.

@GregoireDucharme
Copy link

Any updates on this issue ?

@huannxdev
Copy link

I got this problems when i use pdfjs in angular 8. hope can help someone:

public ngOnDestroy() {
if (this._pdf) {
this._pdf.destroy();
}
if (this.renderTask) {
this.renderTask.cancel();
}
}
in render function, you should get canvas element by: this.element.nativeElement.children[0] instead of query with dom document

@pooya1361
Copy link

Any updates on this issue ?

@Alexeir7
Copy link

Alexeir7 commented Nov 3, 2020

I'm having the same issue, using a single canvas to show the documents and the user can select one of multiple documents to show

@huannxdev
Copy link

I'm having the same issue, using a single canvas to show the documents and the user can select one of multiple documents to show

You should cancel then destroy render pdf process before you can render new pdf file

@ndnenkov
Copy link

ndnenkov commented Nov 13, 2020

Could someone explain the proper way to cancel and re-render the same page?

From other issues I've seen, you shouldn't use the same canvas to run two render tasks at once. And you have to either wait for the previous one to finish (which I don't want to do) or cancel it.

But calling cancel on the previous task still doesn't resolve the issue where sometimes a page is rendered upside down, which supposedly happens if you're running two render tasks on the same canvas.

@huannxdev 's solution also requires you to destroy the pdf. But I don't want to have to reload it just to re-render a single page.

@jiangxiaoqiang
Copy link

I am facing the same issue, version 3.9.179

@xadamxk
Copy link

xadamxk commented Aug 28, 2023

I would also say so. Let's close this and we can always reopen if the final 2.0 release is done and the problem remains.

Running into the issue now that React's strict mode wants to render our PDF component twice. Is this an issue with my implementation or this library? Thank you and any advice would be greatly appreciated!

@superdorvil
Copy link

Sadly, I am having the same issue, i'm just glad someone is having the problem in 2023 and I'm not crazy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests