Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pdf viewer example with fullscreen support #126

Open
limonte opened this issue Sep 9, 2020 · 1 comment
Open

Add pdf viewer example with fullscreen support #126

limonte opened this issue Sep 9, 2020 · 1 comment
Labels

Comments

@limonte
Copy link
Member

limonte commented Sep 9, 2020

https://mozilla.github.io/pdf.js/

sweetalert2/sweetalert2#1781

@limonte limonte added the recipes label Sep 9, 2020
@limonte
Copy link
Member Author

limonte commented Oct 13, 2024

      pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.mjs'

      // SweetAlert2 popup
      Swal.fire({
        title: 'PDF Preview',
        html: '<canvas id="pdfCanvas"></canvas>',
        width: 'auto',
        didOpen: async () => {
          const pdfUrl = 'https://pdfobject.com/pdf/sample.pdf'
          const loadingTask = pdfjsLib.getDocument(pdfUrl)
          const pdf = await loadingTask.promise
          // Fetch the first page
          pdf.getPage(1).then(function (page) {
            var viewport = page.getViewport({ scale: 1.5 })

            // Prepare canvas using PDF page dimensions
            var canvas = document.getElementById('pdfCanvas')
            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)
          })
        },
      })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant