Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy PDFJS object?

I need functionality for book library and for that I have used:

  • Turn.js, which is used for flipbook effect (only 3rd release working, 4th release not working with it, if someone have similar functionality with 4th release of turn.js, then please share your code).

  • pdf.js, which converts PDF to HTML on client side

This is a reference link, that I have followed.

I have modified one function for using that script dynamically, which adds PDF's path into that function and according to that link, books open in popup.

Here is the JavaScript function for that:

function display_book(path){

    var url = path;
    PDFJS.disableWorker = false;

    PDFJS.getDocument(url).then(function(pdfDoc) {
        numberOfPages = pdfDoc.numPages;
        pdf = pdfDoc;
        $('#book').turn.pages = numberOfPages;

        $('#book').turn({acceleration: false,
            pages: numberOfPages,
            elevation: 50,
            gradients: !$.isTouch,
            // display: 'single',
            when: {
                turning: function(e, page, view) {

                    // Gets the range of pages that the book needs right now
                    var range = $(this).turn('range', page);

                    // Check if each page is within the book
                    for (page = range[0]; page<=range[1]; page++) {
                        addPage(page, $(this));
                        //renderPage(page);
                    };

                },

                turned: function(e, page) {
                    $('#page-number').val(page);

                    if (firstPagesRendered) {
                        var range = $(this).turn('range', page);
                        for (page = range[0]; page<=range[1]; page++) {
                            if (!rendered[page]) {
                                renderPage(page);
                                rendered[page] = true;
                            }
                        };
                    }
                }

            }
        });


        $("button.close").click(function(){
            //code for destroy pdfjs object
            $(".modal").css({"display":"none"});
        });
    });
}

on that popup close event, I want to destroy object of PDFJS (to release memory). In this code turn.js 3rd released version is used, and if I replace that version with 4th release then code doesn't work.

like image 265
Learning Edx Avatar asked Dec 04 '14 06:12

Learning Edx


1 Answers

You just need to call destroyon the pdfDoc instance.

In your code sample, it looks like pdfDoc is assign to the global variable pdf. So, this should do what you want:

pdf.destroy();
like image 79
Nicolas BADIA Avatar answered Nov 04 '22 02:11

Nicolas BADIA