Open PDF in new browser full window

后端 未结 4 766
日久生厌
日久生厌 2020-12-13 14:16

How do I open PDF document in new browser window?

The window should be full and withouth menu.

Just a PDF document in a clean full window with native Javasc

相关标签:
4条回答
  • 2020-12-13 14:39
    <a href="#" onclick="window.open('MyPDF.pdf', '_blank', 'fullscreen=yes'); return false;">MyPDF</a>
    

    The above link will open the PDF in full screen mode, that's the best you can achieve.

    0 讨论(0)
  • 2020-12-13 14:40
    var pdf = MyPdf.pdf;
    window.open(pdf);
    

    This will open the pdf document in a full window from JavaScript

    A function to open windows would look like this:

    function openPDF(pdf){
      window.open(pdf);
      return false;
    }
    
    0 讨论(0)
  • 2020-12-13 14:41

    I'm going to take a chance here and actually advise against this. I suspect that people wanting to view your PDFs will already have their viewers set up the way they want, and will not take kindly to you taking that choice away from them :-)

    Why not just stream down the content with the correct content specifier?

    That way, newbies will get whatever their browser developer has a a useful default, and those of us that know how to configure such things will see it as we want to.

    0 讨论(0)
  • 2020-12-13 14:50

    To do it from a Base64 encoding you can use the following function:

    function base64ToArrayBuffer(data) {
      const bString = window.atob(data);
      const bLength = bString.length;
      const bytes = new Uint8Array(bLength);
      for (let i = 0; i < bLength; i++) {
          bytes[i] = bString.charCodeAt(i);
      }
      return bytes;
    }
    function base64toPDF(base64EncodedData, fileName = 'file') {
      const bufferArray = base64ToArrayBuffer(base64EncodedData);
      const blobStore = new Blob([bufferArray], { type: 'application/pdf' });
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
          window.navigator.msSaveOrOpenBlob(blobStore);
          return;
      }
      const data = window.URL.createObjectURL(blobStore);
      const link = document.createElement('a');
      document.body.appendChild(link);
      link.href = data;
      link.download = `${fileName}.pdf`;
      link.click();
      window.URL.revokeObjectURL(data);
      link.remove();
    }
    
    0 讨论(0)
提交回复
热议问题