最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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>
Post a comment

comment list (0)

  1. No comments so far