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.

Cannot use the same canvas during multiple render() operations

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
pooya1361commented, Oct 23, 2019

Any updates on this issue ?

1reaction
huannxdevcommented, Sep 25, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to fix this Error Cannot use the same canvas during ...
In case someone stumbles across this, the error message states the following Use different canvas or ensure previous operations were ...
Read more >
how to fix this Error Cannot use the same canvas during ...
In case someone stumbles across this, the error message states the following Use different canvas or ensure previous operations were cancelled or completed....
Read more >
pdfjs Cannot use the same canvas during multiple render ...
pdfjs Cannot use the same canvas during multiple render() operations_MHY_MHB的博客-CSDN博客
Read more >
Loading multiple pages into one canvas - Google Groups
to mozilla-d...@lists.mozilla.org. I have for example a PDF with 4 Pages, so I try to render all pages into one canvas, but how...
Read more >
Examples
The same canvas cannot be used to perform to draw two pages at the same time – the example demonstrates how to wait...
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