Trigger print preview of base64 encoded PDF from javascript

前端 未结 2 1506
情深已故
情深已故 2020-12-19 03:15

I\'ve looked around stackoverflow trying to find a way to do this for a while now, and can\'t find a suitable answer. I need to be able to load a PDF in either a new window

相关标签:
2条回答
  • 2020-12-19 03:48

    you can use this,

    function "printPreview(binaryPDFData)" to get print preview dialog of binary pdf data.

    printPreview = (data, type = 'application/pdf') => {
        let blob = null;
        blob = this.b64toBlob(data, type);
        const blobURL = URL.createObjectURL(blob);
        const theWindow = window.open(blobURL);
        const theDoc = theWindow.document;
        const theScript = document.createElement('script');
        function injectThis() {
            window.print();
        }
        theScript.innerHTML = `window.onload = ${injectThis.toString()};`;
        theDoc.body.appendChild(theScript);
    };
    
    b64toBlob = (content, contentType) => {
        contentType = contentType || '';
        const sliceSize = 512;
         // method which converts base64 to binary
        const byteCharacters = window.atob(content); 
    
        const byteArrays = [];
        for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            const slice = byteCharacters.slice(offset, offset + sliceSize);
            const byteNumbers = new Array(slice.length);
            for (let i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }
            const byteArray = new Uint8Array(byteNumbers);
            byteArrays.push(byteArray);
        }
        const blob = new Blob(byteArrays, {
            type: contentType
        }); // statement which creates the blob
        return blob;
    };
    
    0 讨论(0)
  • 2020-12-19 03:50

    Here is a solution for point #3:

    Open a new window with only an iframe element like the one in #2 and calling a print on the whole window. This also shows a blank white page.

    In your case, it's throwing CORS error because it looks like for iframe src you are giving the base64String not the URL. Here is what you can do

    1. Take your base64String, convert it to a Blob
    2. Generate a URL from the Blob
    3. Provide the generated URL to iframe.
    4. After this you can print the content using iframe.contentWindow.print();

    Here is the code to convert base64 to Blob

    'use strict';
    
    const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
        const byteCharacters = atob(b64Data);
        const byteArrays = [];
    
        for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            const slice = byteCharacters.slice(offset, offset + sliceSize),
                byteNumbers = new Array(slice.length);
            for (let i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }
            const byteArray = new Uint8Array(byteNumbers);
    
            byteArrays.push(byteArray);
        }
    
        const blob = new Blob(byteArrays, { type: contentType });
        return blob;
    }
    
    
    const contentType = "application/pdf",
        b64Data = "YourBase64PdfString", //Replace this with your base64String
        blob = this.b64toBlob(b64Data, contentType),
        blobUrl = URL.createObjectURL(blob);
    

    Use blobUrl to the src of Iframe, once it's done, you can call print() on iframe as shown below

    const iframeEle = document.getElementById("Iframe");
    if (iframeEle) {
        iframeEle.contentWindow.print();
    }
    

    Hope this helps...

    More details on base64 to Blob is here Creating a Blob from a base64 string in JavaScript

    0 讨论(0)
提交回复
热议问题