1

我想通过 SharePoint 工作流打开文件(docx、pdf、...)的打印对话框。在那里我用 GET 调用 URL 并在 ? 之后传递文件的 URL。像这样:

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

编辑:我也试过:

<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>

打印对话框正在打开,在打印选项中选择了“仅选定的帧”点,但是当我按下打印按钮时,什么都不会发生。

谢谢你的帮助!

4

3 回答 3

1

问题是在当前脚本块完成执行之前,新的 url 不会开始加载。因此,当您调用 时w.print(),新窗口当前为空白。

尝试:

<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>

编辑:我没有正确阅读问题!上述技术仅适用于 html。解决这个问题的方法是使用 iframe,如果我们使用 iframe,我们不妨完全放弃弹出窗口。以下代码创建了一个 iframe,其源设置为所需的文档,将 iframe 附加到页面(但使其不可见),打印 iframe 的内容,最后在我们完成后删除 iframe。仅在 Chrome 中进行了测试,但我相当有信心它可以在其他浏览器中运行。

<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>
于 2013-05-21T10:07:43.160 回答
1

您可以放入 html 的正文

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

所以打开的页面已经打印了文档。

于 2013-05-21T13:06:23.823 回答
-1

我们可以在哪里给出文件的路径 like("abc.doc"/"abc.xls")

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

于 2013-10-01T13:06:46.523 回答