Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过ajax调用打印文档_Javascript_Jquery - Fatal编程技术网

Javascript 通过ajax调用打印文档

Javascript 通过ajax调用打印文档,javascript,jquery,Javascript,Jquery,我有一个场景,我想打印一个文档,我可以通过一个ajax调用点击一个按钮 “打印”弹出窗口显示在“单击”按钮中,但其中包含一个空文档 我检查了电话,它返回了有效的文档,但没有反映在弹出窗口中 我使用的代码是: var url = window.apiUrl; url = url + "/GetDocument"; $.ajax({ type: 'GET', cache: false, url: url,

我有一个场景,我想打印一个文档,我可以通过一个ajax调用点击一个按钮

“打印”弹出窗口显示在“单击”按钮中,但其中包含一个空文档

我检查了电话,它返回了有效的文档,但没有反映在弹出窗口中

我使用的代码是:

var url = window.apiUrl;
    url = url + "/GetDocument";
    $.ajax({
        type: 'GET',
        cache: false,
        url: url,            
    }).done(function (data) {
        try {

            var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,' +
      'resizable,screenX=50,screenY=50,width=850,height=1050';

            var htmlPop = '<embed width=100% height=100%'
                             + ' type="application/pdf"'
                             + ' src="data:application/pdf;base64,'
                             + escape(data)
                             + '"></embed>';

            var printWindow = window.open("", "PDF", winparams);
            printWindow.document.write(htmlPop);
            printWindow.print();
            printWindow.close();
        }
        catch (e) {
            console.log(e);
        }

    }).fail(function (xhr, textStatus, errorThrown) {

        console.log(xhr);
    });
var url=window.apiUrl;
url=url+“/GetDocument”;
$.ajax({
键入:“GET”,
cache:false,
url:url,
}).完成(功能(数据){
试一试{
var winparams='dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,'+
'可调整大小,屏幕X=50,屏幕Y=50,宽度=850,高度=1050';
var htmlop='';
var printWindow=window.open(“,”PDF“,winparams);
printWindow.document.write(htmlPop);
printWindow.print();
printWindow.close();
}
捕获(e){
控制台日志(e);
}
}).fail(函数(xhr、textStatus、errorshown){
console.log(xhr);
});

有人能告诉我我遗漏了什么吗。

获取所需内容的更安全方法是使用第三方库,即 我在不同的浏览器中使用过它,效果很好

一个简单的例子是:

$http({
url: "",
method: "GET",
headers: {
    "Content-type": "application/pdf"
},
responseType: "arraybuffer"
 }).success(function (data, status, headers, config) {
var pdfFile = new Blob([data], {
    type: "application/pdf"
});
var pdfUrl = URL.createObjectURL(pdfFile);
printJS(pdfUrl);
}).error(function (data, status, headers, config) {
console.log("something went wrong")
});

您正在获取pdf并希望在文档中写入?我正在获取一个文档并希望将其作为PDFHello打印,谢谢您的建议,但我不想使用任何第三方库。有没有可能像我想的那样去做。