admin管理员组

文章数量:1025512

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>

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>

本文标签: javascriptHow to embed pdfjs in a fancyboxStack Overflow