简体   繁体   中英

Hide print button when using PDFObject with PDF.js

I'm trying the print button and others when rendering a file using pdf.js. I tried using CSS and it works for Chrome but not Internet Explorer. For Internet Explorer I used javascript. JS works when I load one file but subsequent files are still showing the buttons.

viewer.html

<script type="text/javascript">
    $(function () {

        $('#print').hide();
        $('#viewBookmark').hide();
        $('#openFile').hide();
    });
</script>

viewer.css

button#openFile, button#print, a#viewBookmark {
        display: none;
    }

default.cshtml

$('.file').on('click touchend', function (e) {
        e.preventDefault();
        if ($(this).hasClass('disabled'))
            return;
        var path = $(this).data('path').replace("\\\\", "http://").replace("@pdfFolder", "Uploads");
        var name = $(this).data('name');
        var lastname = $(this).data('lastname');
        name = name.length > 8 ?
            name.substring(0, 5) + '...' :
            name.substring(0, 8);
        lastname = lastname.length > 8 ?
            lastname.substring(0, 5) + '...' :
            lastname.substring(0, 8);
        var tabCount = $('#tabs li').size();
        var uuid = guid();
        $(this).attr('data-position', uuid);
        $('#content').css('display', 'block');
        if (tabCount === 5) {
            $('#maximumTabsModal').modal('show');
        } else {
            $(this).addClass('disabled')
            $('<li role="presentation" data-position="' + uuid + '"><a href="#panel' + uuid + '" aria-controls="panel"' + uuid + ' role="tab" data-toggle="tab">' + name + '<span class="close"></span><br/>' + lastname + '</a></li>').appendTo('#tabs');
            $('<div class="tab-pane" id="panel' + uuid + '"><div id="pdf' + uuid + '" class="pdf"></div></div>').appendTo('.tab-content');
            $('#tabs a:last').tab('show');
            var options = {
                //pdfOpenParams: {
                //    view: "FitV"
                //},
                forcePDFJS: true,
                PDFJS_URL: "pdfjs/web/viewer.html"
            };
            var pdf = PDFObject.embed(path, '#pdf' + uuid, options);
            $('#print').hide();
            $('#viewBookmark').hide();
            $('#openFile').hide();
            $('#exd-logo').hide();
        }
    });

Unfortunately, PDFObject is not capable of hiding the print button, it only provides a mechanism for specifying PDF Open Parameters, which do not include the ability to hide the print button.

I'm no expert on PDF.js, but since it's all JS-based, and hosted on your domain (ie you have full script access), you should be able to find a way to hack it to remove the print button. Good luck!

I am shooting in the dark here because I do not know if the PDF viewer loads in an <iframe> or not, but the following code will scan over the page indefinitely and suppress the print button from showing if it finds it.

    var $printSearch = setInterval(function() {
      if ($('#print').length > 0 || $('#print').is(':visible')) {
        hidePrint();
      } else {
        //doNothing
        console.log('Searching...');
      }
    }, 150);

    function hidePrint() {
      $('div#print').css('display', 'none');
    }

If it does load in an iframe we could use the .contents() and .filter() jQuery methods to target those elusive buttons.

I was able to get the buttons to hide by handling the pagerendered event that PDF.js provides.

viewer.html

<script type="text/javascript">
    $(function () {
        document.addEventListener("pagerendered", function (e) {
            $('#print').hide();
            $('#viewBookmark').hide();
            $('#openFile').hide();
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM