简体   繁体   English

PDF.js:如何在新选项卡中显示PDF?

[英]PDF.js : How to display a PDF in a new tab?

As a preamble, I must preside over the fact that my knowledge in web development environments is limited. 作为序言,我必须主持一个事实,即我在Web开发环境中的知识有限。 In fact, my question may seem trivial to some people. 实际上,对于某些人来说,我的问题似乎微不足道。

With an Ajax call, I generate a PDF, via TCPDF, which I retrieve in a variable. 通过Ajax调用,我通过TCPDF生成了PDF,并在变量中进行了检索。

I convert it to base64 and place it in a Uint8Array. 我将其转换为base64并将其放置在Uint8Array中。

    function create_pdf() {
    var jsonOptions = jQuery("#Form").serializeArray();

    $.ajax({
        url: 'tcpdf/samples/samples_002c.php',
        type: 'POST',
        cache: false,
        data: jsonOptions
    })
        .done(function(data) {
            var pdfData = convertDataURIToBinary('data:application/pdf;base64,' + data);
            // View PDF
            // .....
    })
        .fail(function() {
            alert('Faild');
    });

    return false;
}

function convertDataURIToBinary(dataURI) {
    var BASE64_MARKER = ';base64,';
    var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
    var base64 = dataURI.substring(base64Index);
    var raw = window.atob(base64);
    var rawLength = raw.length;
    var array = new Uint8Array(new ArrayBuffer(rawLength));

    for(i = 0; i < rawLength; i++) {
        array[i] = raw.charCodeAt(i);
    }
    return array;
}

I want to display it on the user's computer via the PDF.js viewer (the purpose being to avoid saving the PDF in a file on the server) in a new tab. 我想通过新标签中的PDF.js查看器在用户计算机上显示它(目的是避免将PDF保存在服务器上的文件中)。

I have been looking for several days to finalize this part without success. 我一直在寻找几天来完成这一部分而没有成功。

Do you have a research lead that would allow me to succeed in this project? 您是否有研究线索可以使我成功完成该项目?

Thank you, in advance, for your advice. 预先感谢您的建议。

// ADD OPEN WHIT WINDOW JAVASCRIPT
var pdfData = convertDataURIToBinary('data:application/pdf;base64,' + data);
window.open(pdfData);

One solution is to not use PDF.js and rely on browser handling of PDF with open function (as @MarcoDeveloper suggested). 一种解决方案是不使用PDF.js,而是依赖浏览器对具有open功能的PDF的处理(如@MarcoDeveloper建议)。 The other solution is to prepare separated page that will display your files with PDF.js. 另一种解决方案是准备单独的页面,该页面将显示PDF.js文件。

For this you'll need to prepare simple html page. 为此,您需要准备简单的html页面。 I would suggest to use post message, but you can also send data directly if your page is on same domain. 我建议使用帖子,但是如果您的页面在同一域中,则也可以直接发送数据。

preview.html : Preview.html

window.addEventListener("message", receiveMessage, false);
function receiveMessage(e) {
   if (event.origin === window.location.origin) {
       PDFJS.getDocument({ data: convertDataURIToBinary(e.data) }).then(function() {
       });
   }
}

index.html : index.html

var win = window.open('preview.html');
win.postMessage(dataURI, '*');

it's probably good idea to send base64 data and decode in preview.html. 发送base64数据并在Preview.html中解码可能是个好主意。

to send pdf data directly you can use (only on same domain): 直接发送pdf数据,您可以使用(仅在同一域上):

var win = window.open('preview.html');
win.PDFJS.getDocument({ data: binary_data });

the file preview.html only need to include JS file for PDF.js and it should work. 文件Preview.html只需要为PDF.js包含JS文件,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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