开发者

onclick print destination url

I have a link on our invoices page.

The link, points to a print friendly url for printing the invoice.

The issue is, I would like to "onclick" print the content of that URL. Not sure if this is dooable without opening the destination url first. Is it ?

Link: ( this first method doesnt work because it prints 开发者_C百科current URL )

<li class="print"><a href="https://www.mysite.com.au/pdf/invoice_parse.html?id=<?=$r['invoice'];?>" onClick="window.print();return false">Print Invoice</a></li>

Essentially, I want to onclick of the link, print the destination URL. Without leaving the page. Any suggestions please


When you use window.print(), it prints the current window. So you could open an iframe and then call window.print() inside of it.

You could probably get away with hiding the iframe by giving it position: absolute; left: -9999px.

JavaScript

Assuming these links match on protocol, domain and host.

var printInvoice = function(url) {
    
    var iframe = document.createElement('iframe'),
        iframeDocument;

    iframe.style.postion = 'absolute';
    iframe.style.left = '-9999px';
    iframe.src = url;
    document.body.appendChild(iframe);

    if ('contentWindow' in iframe) {
       iframeDocument = iframe.contentWindow;
    } else {
       iframeDocument = iframe.contentDocument;
    }
    
    var script = iframeDocument.createElement('script');

    script.type = 'text/javascript';

    script.innerHTML = 'window.print();';

    iframeDocument.getElementsByTagName('head')[0].appendChild(script);

}

Untested, but it should work :)


Just provide a link to the PDF and let your users print it from their favourite PDF viewer. Doing this sort of thing automatically is just annoying, and you won't find a decent way to do it anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜