本文介绍了如何使用pdf.js呈现pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我创建了一个html文件,其内容低于 index.htmlI created an html file with content as belowindex.html<html> <head> <script type="text/javascript" src="./pdf.js"></script> <script type="text/javascript" src="./hello.js"></script> </head> <body> <canvas id="the-canvas" style="border:1px solid black;"/> </body></html> hello.js 包含内容PDFJS.disableWorker = true;var pf = PDFJS.getDocument('./helloworld.pdf')pf.then(function(pdf) { pdf.getPage(1).then(function(page) { var scale = 1.5; var viewport = page.getViewport(scale); // // Prepare canvas using PDF page dimensions // var canvas = document.getElementById('the-canvas'); var context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; // // Render PDF page into canvas context // var renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext); });});但是当我将浏览器指向index.html时,pdf显示不正确。 我希望用户能够在他的计算机上选择一个pdf文件并在浏览器窗口中显示该文件。But the pdf is not shown correctly when I point the browser to index.html.I want the user to be able to select a pdf file on his computer and show that pdf in browser window.推荐答案当使用文件:协议而不是 http:或 HTTPS:。不同协议之间存在不同的安全考虑因素。It looks like you are hitting this problem when using the file: protocol rather than http: or https:. There are different security considerations in play between the different protocols.这是关于使用 XMLHttpRequest 使用本地文件和关于Mozilla Firefox门票的讨论。有几张门票(包括这个和这一个)项目可能提供指针。来自此票证的评论说:There are a few tickets (including this one and this one) on the project that may provide pointers. A comment from this ticket says: 典型的pdf.js用例需要使用Web服务器和现代HTML5浏览器。 Typical pdf.js use cases requires to use a web server and modern HTML5 browser.我建议解决您的问题您只需通过网络服务器运行此程序即可使用 http 协议。 Nginx和Apache易于安装和设置。I suggest to fix your problem you just run this through a web server to use the http protocol. Nginx and Apache are easy to install and set up.如果不起作用,使用这个。If that doesn't work generate pdf.js and pdf.worker.js for your system using this if the above step doesn't work. 这篇关于如何使用pdf.js呈现pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-26 00:16
查看更多