How to create a pdf viewer in html?

114 views Asked by At

I just need a page to view pdf files in a Firefox browser. The files will be in any folder at my choosing. Kinda like what Firefox already does (like the picture attached), but a like to do this: when a select the file, the pdf pages are show to the right side. Sorry if it's too much to ask, but I couldn't find anything online, and I'm just starting to code, and I don't know how to do it...

I tried to do it with the code below... It didn't work...



<!DOCTYPE html>

   <html lang="pt-br">

   <head>

       <meta charset="UTF-8">

       <meta name="viewport" content="width=device-width, initial-scale=1.0">

       <title>Visualizador PDF</title>

       <style>

           css

   body, html {

       height: 100%;

       margin: 0;

       font-family: Arial, sans-serif;

   }

   .container {

       display: flex;

       height: 100%;

   }

   .lista {

       width: 30%;

       overflow-y: auto;

       border-right: 1px solid #ccc;

       padding: 10px;

   }

   .visualizador {

       width: 70%;

       padding: 10px;

   }

       </style>

   </head>

   <body>

       ok

       <div class="container">

       <div class="lista">

           <ul id="listaArquivos">

               

               <script>javascript

document.addEventListener('DOMContentLoaded', function() {

    const listaArquivos = document.getElementById('listaArquivos');

    const pdfViewer = document.getElementById('pdfViewer');

    // Ajuste o caminho para a pasta pdfs localizada na sua área de trabalho

    const diretorio = "file:///C:/Users/F170593/Desktop/pdfs/"; // Atualize com seu nome de usuário

    fetch(diretorio).then(response => response.text()).then(text => {

        const parser = new DOMParser();

        const xmlDoc = parser.parseFromString(text, "text/html");

        const items = xmlDoc.querySelectorAll('a');  // Use querySelectorAll para obter todos os links

        items.forEach(item => {

            const nomeArquivo = item.textContent;

            if (nomeArquivo.endsWith('.pdf')) {

                const li = document.createElement('li');

                li.textContent = nomeArquivo;

                li.addEventListener('click', function() {

                    pdfViewer.src = diretorio + nomeArquivo;

                });

                listaArquivos.appendChild(li);

            }

        });

    });

});

</script>

           </ul>

       </div>

       <div class="visualizador">

           <iframe id="pdfViewer" width="100%" height="100%" src="" frameborder="0"></iframe>

       </div>

   </div>

   </body>

   </html>
2

There are 2 answers

1
K J On

When accessing files in Windows, then MS Edge has a system access advantage over other browsers as you can split the screen and open system folders in one half and use the other for viewing files such as ePub or PDF.

Here I am viewing #47 and you can see I am in the middle dragging 48.pdf to view that next.

enter image description here

You can start the process in a given folder from a shortcut
"C:\Program Files\Microsoft\Edge\Application\msedge.exe" %userprofile%/documents
or simpler drag the starting folder into MS Edge

enter image description here

Multiple tabs or multiple Dual Windows no problem. enter image description here

0
K J On

If you cannot use newer MS Edge (version 111 or above) there is almost a similar functionality in Firefox (Here I am using a Portable Copy). It requires Mozillas own extender (side-view) Where you can add folders and there add favourite files by drag and drop, it may be possible to bulk generate a loadable set of bookmarks too.

enter image description here