javascript - How to embed pdf.js in a fancybox - Stack Overflow

Fancybox is great but the default pdf implementation does not work very well because support, look and

Fancybox is great but the default pdf implementation does not work very well because support, look and feel depends on OS, browser and adobe installation. PDF.js solves that by using a web standards-based platform for parsing and rendering PDFs. How to embed pdf.js viewer inside a fancybox?

Fancybox is great but the default pdf implementation does not work very well because support, look and feel depends on OS, browser and adobe installation. PDF.js solves that by using a web standards-based platform for parsing and rendering PDFs. How to embed pdf.js viewer inside a fancybox?

Share Improve this question asked Oct 27, 2016 at 7:30 PaintoshiPaintoshi 98612 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Here's my solution how you can use pdf.js together with fancybox. It works x-browsers (at least the ones I tried), automatically & dynamically scales to browser view size and looks nice! The exact loading behavior can be a bit different between browsers though.

You will need to include jquery, fancybox and pdf.js:

  • http://jquery./download/
  • http://fancyapps./fancybox/
  • https://github./mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website

If you want to embed pdf.js inside a static div instead of a fancybox, please have a look at https://pdfobject./

<html>
<head>
    <!-- Add jQuery library -->
    <script type="text/javascript" src="./includes/js/jquery-3.1.1.min.js"></script>
    <!-- Add fancyBox main JS and CSS files -->
    <script type="text/javascript" src="./includes/js/fancybox/jquery.fancybox.js?v=2.1.5"></script>
    <link rel="stylesheet" type="text/css" href="./includes/js/fancybox/jquery.fancybox.css?v=2.1.5" media="screen" />
    <script type="text/javascript">
        var pdfOpenParams = "#page=1&pagemode=none"; //pagemode=none,bookmarks,thumbs

        $(document).ready(function () {
            //Fancybox settings
            $(".fancybox-pdf").fancybox({
                type: 'iframe',
                padding: 0,
                openEffect: 'fade',
                openSpeed: 150,
                closeEffect: 'fade',
                closeSpeed: 150,
                closeClick: true,
                width: '60%',
                height: '100%',
                maxWidth: '100%',
                maxHeight: '100%',
                iframe: {
                    preload: false
                },
                //Define PDF.js viewer with optional parameters
                beforeLoad: function () {
                    var url = $(this.element).attr("href");
                    this.href = "./includes/js/pdfjs/web/viewer.html?file=" + url + pdfOpenParams;
                }
            });
        });
    </script>
</head>
<body>
    <a class="fancybox-pdf" href="http://localhost/pdf/pdf-sample.pdf">View PDF</a>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625286a4636767.html

相关推荐

  • javascript - How to embed pdf.js in a fancybox - Stack Overflow

    Fancybox is great but the default pdf implementation does not work very well because support, look and

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信