首页 > 解决方案 > html页面javascript/jQuery的pdf预览

问题描述

我正在尝试通过单击按钮在 HTML 页面的 javascript 中的新页面上创建 pdf 预览。

我想要的是,通过单击页面上的此按钮,它将在浏览器上打开一个新选项卡,其中包含源页面的 pdf 预览,然后我将从那里下载 pdf。

此刻我已经使用了这段代码

function printopen() {
    var w = window.open();
    var html = $("#divtoprint").html();
    $(w.document.body).html(html);
    w.print();
}

这会打开一个新的空白页面和弹出窗口。我不想显示弹出窗口,但相应的下载按钮和页面应该包含 pdf 预览。

实现这一目标的正确方法是什么?

标签: javascripthtmljquery

解决方案


这将在新选项卡中打开 pdf 预览,您也可以下载它。

在您的 window.open 中,将您的资源的路径作为参数https://developer.mozilla.org/en-US/docs/Web/API/Window/open

 function printopen(){

                var w = window.open("path_to_your_file");
                var html = $("#divtoprint").html();
                  $(w.document.body).html(html);
                  w.print();
              }

但请记住,不能保证用户可以决定选项卡是否会出现在新选项卡中。


推荐阅读