目錄
<!DOCTYPE html>
<html>
<head>
  <title>PDF Viewer</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
</head>
<body>
  <input type="file" id="pdf-file">
  <div id="pdf-viewer"></div>
  <script>
    const fileInput = document.getElementById('pdf-file');
    const pdfViewer = document.getElementById('pdf-viewer');
    fileInput.addEventListener('change', function() {
      const file = fileInput.files[0];
      // Load the PDF file using pdf.js library
      const fileReader = new FileReader();
      fileReader.onload = function() {
        const typedarray = new Uint8Array(this.result);
        pdfjsLib.getDocument(typedarray).promise.then(function(pdf) {
          // Get the first page of the PDF and render it
          pdf.getPage(15).then(function(page) {
            const canvas = document.createElement('canvas');
            const viewport = page.getViewport({scale: 1});
            const context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;
            page.render({canvasContext: context, viewport: viewport}).promise.then(function() {
              pdfViewer.appendChild(canvas);
            });
          });
        });
      };
      fileReader.readAsArrayBuffer(file);
    });
  </script>
</body>
</html>