1

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!

3 Answers 3

1

you can put in the body of the html

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

so the page is opened already prints the document.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

6 Comments

it opens the file, but there is no print dialog
When I tried it on my computer (in Chrome) it also failed but that turned out to be the popup blocker. Allowing the popups on an individual basis didn't work as there were not allowed by the time onload was executed. The solution was to set the popup blocker to always allow popups from the domain. Maybe your issue is similar? Clearly that still leaves the task of educating your users - but you're always going to run into these sorts of issues with popups
I turned out the blocker but there is still no print Dialog (IE10 and FF21). For a empty site it works fine but for documents not...
Sorry, I hadn't read your question properly and didn't notice you were printing pdfs. The approach I've used so far will only work for html. However, I believe you can solve the issue with an iframe. I'll modify my answer accordingly
for Chrome it works fine but not for ie(is used in Company, so it should work for it) and ff
|
-1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.