您的位置:首页 > 其它

[mobile开发碎碎念]手机页面上显示PDF文件

2013-12-11 16:00 399 查看
demo:http://mozilla.github.io/pdf.js/web/viewer.html

项目地址:https://github.com/mozilla/pdf.js

<script type="text/javascript">
//
// NOTE:
// Modifying the URL below to another server will likely *NOT* work. Because of browser
// security restrictions, we have to use a file server with special headers
// (CORS) - most servers don't support cross-origin browser requests.
//
var url = 'aaa.pdf';

//
// Disable workers to avoid yet another cross-origin issue (workers need the URL of
// the script to be loaded, and dynamically loading a cross-origin script does
// not work)
//
PDFJS.disableWorker = true;

//
// Asynchronous download PDF as an ArrayBuffer
//
PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
//
// Fetch the first page
//
pdf.getPage(1).then(function getPageHelloWorld(page) {
var scale = 1.0;
var viewport = page.getViewport(($(window).width()-4) / page.getViewport(1.0).width);

//
// 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
//
page.render({canvasContext: context, viewport: viewport});
});
});
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: