Printing contents of another page

后端 未结 6 1412
执笔经年
执笔经年 2021-01-02 04:15

In the following link

Print\"


        
相关标签:
6条回答
  • 2021-01-02 04:55

    I think you can print the contents of the current page not external page.

    0 讨论(0)
  • 2021-01-02 05:03

    I know it´s an old question, but you can do it like this:

    function printExternal(url) {
        var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
        printWindow.addEventListener('load', function(){
            printWindow.print();
            printWindow.close();
        }, true);
    }
    

    Tested in Firefox and Chrome. IE9 doesn´t work.


    Edit 2020-04-03

    No longer work on Chrome, code adapted from Coder answer here:

    function printExternal(url) {
        var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
    
        printWindow.addEventListener('load', function() {
            if (Boolean(printWindow.chrome)) {
                printWindow.print();
                setTimeout(function(){
                    printWindow.close();
                }, 500);
            } else {
                printWindow.print();
                printWindow.close();
            }
        }, true);
    }
    
    0 讨论(0)
  • 2021-01-02 05:10

    An alternative is to link to the page with a get variable and then call the print function.

    For your link -

    <a href="print-page.php?print=1">Print other page</a>
    

    Then on your print page (or all pages)

    <script type="text/javascript">
    <? if(isset($_GET['print'])) { ?>
    window.print();
    <? } ?>
    </script>
    
    0 讨论(0)
  • 2021-01-02 05:10

    You can print external page by creating window object of another page and calling print method of that page.
    URL can be passed in argument or can be hardcoded in function depends on use case

    function print(url) {
        var printWindow = window.open( url);
        printWindow.print();
    };
    
    0 讨论(0)
  • 2021-01-02 05:15

    Think about the security/embarrassment issues that would exist if this was possible. Thankfully, browsers won't allow you to do that.

    The closest you can get is fetching the page via AJAX, replacing the current DOM with the new page, and printing with JS's normal print() method.

    0 讨论(0)
  • 2021-01-02 05:16

    If you already have an external page( letterprint.php ), put that page in a hidden iframe and print the content of iframe by using onclick attribute in a button.

    <iframe src="letterprint.php" style="display:none;" name="frame"></iframe>
    
    <input type="button" onclick="frames['frame'].print()" value="printletter">
    
    0 讨论(0)
提交回复
热议问题