Using javascript to print images

前端 未结 9 864
清歌不尽
清歌不尽 2020-12-02 17:40

I would like to know if it\'s possible to use javascript to open a popup window containing an image, and at the same time have the print dialog show. Once someone clicks on

相关标签:
9条回答
  • 2020-12-02 18:00

    Yea, just put the image on the screen, and then call window.print(); in javascript and it should popup.

    (This is how Google Maps/Google Calendar do printing)

    0 讨论(0)
  • 2020-12-02 18:05

    Use this in the head block

    <script type="text/javascript">
    function printImg() {
      pwin = window.open(document.getElementById("mainImg").src,"_blank");
      pwin.onload = function () {window.print();}
    }
    </script>
    

    use this in the body block

    <img src="images.jpg" id="mainImg" />
    <input type="button" value="Print Image"  onclick="printImg()" />
    
    0 讨论(0)
  • 2020-12-02 18:06

    I just spent 45 minutes on this "SIMPLE" problem, trying to get it the way I wanted it to operate.

    I had an image inside an img tag, dynamically generated by a jQuery Barcode plugin that I had to print. I wanted it to print in another window and afterwards close the window. This was all supposed to happen after the user clicked a button inside a jQuery Grid plugin, inside a jQuery-UI dialog along with jQuery-UI dialog extender applied to it.

    I adjusted everyone answers till I finally came up with this, maybe it can help someone.

    w = window.open(document.getElementById("UB-canvas").src);
    w.onload = function () { w.print(); }
    w.onbeforeunload = setTimeout(function () { w.close(); },500);
    w.onafterprint = setTimeout(function () { w.close(); },500);
    

    The setTimeout is not just for shits and giggles, it's the only way I found Firefox 42 would hit those functions. It would just simply skip the .close() functions until I added a breakpoint to it, then it worked perfectly. So I'm assuming it created those window instances before it could apply the onbeforeload event function and onafterprint event functions, or something.

    0 讨论(0)
  • 2020-12-02 18:07

    A cross browser solution printImage(document.getElementById('buzzBarcode').src)

    /**
     * Prints an image by temporarily opening a popup
     * @param {string} src - image source to load
     * @returns {void}
     */
    function printImage(src) {
        var win = window.open('about:blank', "_new");
        win.document.open();
        win.document.write([
            '<html>',
            '   <head>',
            '   </head>',
            '   <body onload="window.print()" onafterprint="window.close()">',
            '       <img src="' + src + '"/>',
            '   </body>',
            '</html>'
        ].join(''));
        win.document.close();
    }
    img {
      display: block;
      margin: 10px auto;
    }
    
    button {
      font-family: tahoma;
      font-size: 12px;
      padding: 6px;
      display: block;
      margin: 0 auto;
    }
    <img id="buzzBarcode" src="https://barcode.orcascan.com/qrcode/buzz.png?text=to infinity and beyond" width="150" height="150" />

    0 讨论(0)
  • 2020-12-02 18:07

    This works in Chrome:

      <body ><img  src="image.jpg" alt="" style="display: block; width: 100%; height: 100%;">
    
                <script type="text/javascript">
                    window.onload = function() {
                        window.print();
                        setTimeout(function() {
                            window.close();
                        }, 1);
                    };
                </script>
        </body>
    
    0 讨论(0)
  • 2020-12-02 18:10

    Another great solution!! All credit goes to Codescratcher

    <script>
    
        function ImagetoPrint(source)
        {
            return "<html><head><scri"+"pt>function step1(){\n" +
                    "setTimeout('step2()', 10);}\n" +
                    "function step2(){window.print();window.close()}\n" +
                    "</scri" + "pt></head><body onload='step1()'>\n" +
                    "<img src='" + source + "' /></body></html>";
        }
    
        function PrintImage(source)
        {
            var Pagelink = "about:blank";
            var pwa = window.open(Pagelink, "_new");
            pwa.document.open();
            pwa.document.write(ImagetoPrint(source));
            pwa.document.close();
        }
    
    </script>
    
    <a href="#" onclick="PrintImage('YOUR_IMAGE_PATH_HERE.JPG'); return false;">PRINT</a>
    

    See the full example here.

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