pdfMake - open() and print() functions are not working

10,502

Solution 1

Please check that any type of ad blocker in your browser is turned off and try again.

Solution 2

I found solution for printing in same window.

In your .html file put iframe

<iframe id="printPdf" name="printPdf"></iframe>

iframe needs style to hide himself for example (I do not know why, but if I define width and height on iframe, printing will not work):

#printPdf { position: fixed; top: 0px; left: 0px; display: block;
            padding: 0px;border: 0px;margin: 0px;
            visibility: hidden; opacity: 0; 
}

Finally, just call:

    if ('safari') {
        pdfMake.createPdf(content).open({}, window.frames['printPdf']);
        setTimeout(function() {
            window.frames['printPdf'].focus();
            window.frames['printPdf'].print();
        }, 2000)
    } else {
        pdfMake.createPdf(content).print({}, window.frames['printPdf']);
    }

Tested on Chrome v72, Firefox v65, Edge v18, Safari v12

Solution 3

for the open() it's not working even without ad blocker so i converted it to base64 then blob then fileURL

                   var docDefinition = getRWABELPDF(data);
                   var createPdf = pdfMake.createPdf(docDefinition);
                   var base64data = null;

                    createPdf.getBase64(function(encodedString) {
                        base64data = encodedString;
                       console.log(base64data );


                        var byteCharacters = atob(base64data);
                        var byteNumbers = new Array(byteCharacters.length);
                        for (var i = 0; i < byteCharacters.length; i++) {
                            byteNumbers[i] = byteCharacters.charCodeAt(i);
                        }
                        var byteArray = new Uint8Array(byteNumbers);
                        var file = new Blob([byteArray], { type: 'application/pdf;base64' });
                        var fileURL = URL.createObjectURL(file);
                        window.open(fileURL);
Share:
10,502
Ritik Saxena
Author by

Ritik Saxena

Full stack developer Javascript &amp; Python enthusiast

Updated on June 04, 2022

Comments

  • Ritik Saxena
    Ritik Saxena almost 2 years

    I'm trying to learn how to use pdfMake. I'm trying to use open and print to generate or print the information respectively. But, when I click on the button which fires the event, a new tab opens for a second and vanishes. The page which opens is displayed in history as blob:http://localhost:9999/93c1600e-3c64-42fe-8b44-fe6eeb995b5e

    I'm not able to figure out the error. I'm following the official documentation of pdfMake.

    Please help.

    function print(){
      window.event.preventDefault()
      // this is just a simulation of the open event, replacing it with print produces the same result
      var docDefinition = { content: {text:'This is an sample PDF printed with pdfMake',fontSize:15} };
      pdfMake.createPdf(docDefinition).open();
    }
    <!DOCTYPE HMTL>
    <html>
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width,initial-scale=1.0" />
      <script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.27/pdfmake.min.js'></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.27/vfs_fonts.js"></script>
      <script src="js/print.js"></script>
    </head>
    <body>
      <main>
        <button onclick="print()">Print Card</button>
      </main>
    </body>
    </html>