Spaces:
Running
Running
crie uma pagina html reponsive que exiba o conteudo de um arquivo pdf como se fosse uma pagina html. por exemplo o codigo a vir parametrizado o arquivo pdf e ao abrir a pagina esse arquivo ser exibido direto sem nehuma pergunta adicional
8b0066f verified Download script.js from Deepdomarcio/pdf-magic-viewer: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/spaces/Deepdomarcio/pdf-magic-viewer/resolve/main/script.js
- Command line
-
hf download hf://spaces/Deepdomarcio/pdf-magic-viewer/script.js
-
curl -L -o script.js https://huggingface.co/spaces/Deepdomarcio/pdf-magic-viewer/resolve/main/script.js
2.54 kB
| let pdfDoc = null, | |
| pageNum = 1, | |
| pageRendering = false, | |
| pageNumPending = null, | |
| scale = 1.0, | |
| canvas = document.getElementById('pdfCanvas'), | |
| ctx = canvas.getContext('2d'); | |
| /** | |
| * Get page info from document, resize canvas accordingly, and render page. | |
| * @param num Page number. | |
| */ | |
| function renderPage(num) { | |
| pageRendering = true; | |
| pdfDoc.getPage(num).then(function(page) { | |
| const viewport = page.getViewport({ scale: scale }); | |
| canvas.height = viewport.height; | |
| canvas.width = viewport.width; | |
| const renderContext = { | |
| canvasContext: ctx, | |
| viewport: viewport | |
| }; | |
| const renderTask = page.render(renderContext); | |
| renderTask.promise.then(function() { | |
| pageRendering = false; | |
| if (pageNumPending !== null) { | |
| renderPage(pageNumPending); | |
| pageNumPending = null; | |
| } | |
| }); | |
| }); | |
| document.getElementById('pageNum').textContent = num + ' / ' + pdfDoc.numPages; | |
| } | |
| /** | |
| * If another page rendering in progress, waits until the rendering is | |
| * finished. Otherwise, executes rendering immediately. | |
| */ | |
| function queueRenderPage(num) { | |
| if (pageRendering) { | |
| pageNumPending = num; | |
| } else { | |
| renderPage(num); | |
| } | |
| } | |
| /** | |
| * Displays previous page. | |
| */ | |
| function onPrevPage() { | |
| if (pageNum <= 1) { | |
| return; | |
| } | |
| pageNum--; | |
| queueRenderPage(pageNum); | |
| } | |
| /** | |
| * Displays next page. | |
| */ | |
| function onNextPage() { | |
| if (pageNum >= pdfDoc.numPages) { | |
| return; | |
| } | |
| pageNum++; | |
| queueRenderPage(pageNum); | |
| } | |
| /** | |
| * Zooms in the PDF. | |
| */ | |
| function zoomIn() { | |
| scale += 0.25; | |
| queueRenderPage(pageNum); | |
| } | |
| /** | |
| * Zooms out the PDF. | |
| */ | |
| function zoomOut() { | |
| if (scale <= 0.25) return; | |
| scale -= 0.25; | |
| queueRenderPage(pageNum); | |
| } | |
| /** | |
| * Load PDF document. | |
| */ | |
| function loadPDF(url) { | |
| pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) { | |
| pdfDoc = pdfDoc_; | |
| document.getElementById('pageNum').textContent = pageNum + ' / ' + pdfDoc.numPages; | |
| renderPage(pageNum); | |
| }).catch(function(error) { | |
| alert('Error loading PDF: ' + error.message); | |
| }); | |
| } | |
| // Event listeners | |
| document.getElementById('prevPage').addEventListener('click', onPrevPage); | |
| document.getElementById('nextPage').addEventListener('click', onNextPage); | |
| document.getElementById('zoomIn').addEventListener('click', zoomIn); | |
| document.getElementById('zoomOut').addEventListener('click', zoomOut); |