简体   繁体   中英

Print a document with JavaScript

I want to open a print dialog for a file (docx,pdf,...) via a SharePoint Workflow. There I call a URL with GET and pass the URL of the file after the ? like this:

http://www.sharepoint_intranet.com/print.html?link-to-file.pdf

EDIT: I also tried:

<script type="text/javascript">
    //Get the URL of the file
    var urlOfFile = window.location.search.replace("?", "");
    document.write("<iframe id=" + "printDocument" + " src=" + "'" + urlOfFile + "'" + " width=" + "600" + " height=" + "400" + "></iframe>");

    window.frames['printDocument'].focus();
    window.frames['printDocument'].print();

</script>

The Print Dialog is opening and in the print options there is the Point "Only selected Frame" selected but when I press the print button nothing will happen.

Thanks for any help!

The issue is that the new url doesn't start loading until the current script block has finished executing. Therefore when you call w.print() , the new window is currently blank.

Try:

<script type="text/javascript">
    //Get the URL of the file
    var urlOfFile = window.location.search.replace("?", "");
    //print
    var w = window.open(urlOfFile);
    w.onload = function() {
        w.print();
    }
</script>

EDIT: I didn't read the question properly! The above technique only works for html. The way to solve this is to use an iframe and if we are using an iframe we might as well dispense with the popups entirely. The following code creates an iframe with a source set to the desired document, attaches the iframe to the page (but keeps it invisible), prints the contents of the iframe and finally removes the iframe once we've finished with it. Only tested in Chrome but I'm fairly confident that it'll work in other browsers.

<script type="text/javascript">
    //Get the URL of the file
    var urlOfFile = window.location.search.replace("?", "");
    //print
    var iframe = document.createElement('iframe');
    iframe.src = urlOfFile;
    iframe.style.display = "none";
    var iFrameLoaded = function() {
        iframe.contentWindow.print();
        iframe.parentNode.removeChild(iframe);
    };
    if (iframe.attachEvent) iframe.attachEvent('onload', iFrameLoaded); // for IE
    else if(iframe.addEventListener) iframe.addEventListener('load', iFrameLoaded, false); // for most other browsers
    else iframe.onload = iFrameLoaded; // just in case there's a browser not covered by the first two
    window.onload = function() {
        document.body.appendChild(iframe);
    };
</script>

you can put in the body of the html

<body onLoad="window.print();">

so the page is opened already prints the document.

where we can give the path of the file like("abc.doc"/"abc.xls")

var urlOfFile = window.location.search.replace("?", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM