Last active
February 12, 2025 23:11
-
-
Save hotsoft-desenv2/be32da749ebd1447c1fb10dd8f8c32c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="viewer-container" style="height: 100%; width: 100%; border: 0" class="w-100 h-100 border-0"> | |
| <div id="download-link-container" style="display: none; text-align: center; margin-bottom: 10px;"></div> | |
| <iframe id="pdf-iframe" type="application/pdf" class="w-100 h-100 border-0"></iframe> | |
| <object id="pdf-viewer" type="application/pdf" class="d-none"> | |
| <param id="base64-param" value="<%= @file_base64 %>"> | |
| </object> | |
| </div> | |
| <script src="https://cdn.jsdelivr.net/npm/jszip/dist/jszip.min.js" type="text/javascript"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js" type="text/javascript"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.4/pako.min.js" type="text/javascript"></script> | |
| <script> | |
| document.addEventListener('DOMContentLoaded', function () { | |
| var base64Data = document.getElementById('base64-param').getAttribute('value'); | |
| var pdfIframe = document.getElementById('pdf-iframe'); | |
| var laudoNumber = new URLSearchParams(window.location.search).get('laudo'); | |
| var downloadContainer = document.getElementById('download-link-container'); | |
| function isBase64PDF(base64) { | |
| return base64.startsWith('JVBERi0'); | |
| } | |
| function base64ToArrayBuffer(base64) { | |
| var binaryString = window.atob(base64); | |
| var bytes = new Uint8Array(binaryString.length); | |
| for (var i = 0; i < binaryString.length; i++) { | |
| bytes[i] = binaryString.charCodeAt(i); | |
| } | |
| return bytes.buffer; | |
| } | |
| function processZipFile(data) { | |
| if (isBase64PDF(base64Data)) { | |
| pdfIframe.src = 'data:application/pdf;base64,' + base64Data; | |
| console.log('PDF carregado diretamente.'); | |
| } else { | |
| var dataArray = new Uint8Array(data); | |
| attemptUnzip(dataArray, ['JSZip', 'fflate', 'pako'], function() { | |
| console.log("Processo de descompactação completo."); | |
| }); | |
| } | |
| } | |
| function attemptUnzip(data, libraries, callback) { | |
| if (libraries.length === 0) { | |
| console.error("Nenhuma biblioteca de descompressão disponível."); | |
| return; | |
| } | |
| var library = libraries.shift(); | |
| switch (library) { | |
| case 'JSZip': | |
| if (window.JSZip) { | |
| unzipWithJSZip(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com JSZip falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| case 'fflate': | |
| if (window.fflate) { | |
| unzipWithFflate(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com fflate falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| case 'pako': | |
| if (window.pako) { | |
| unzipWithPako(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com pako falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| } | |
| } | |
| function unzipWithJSZip(data, callback) { | |
| JSZip.loadAsync(data).then(function(zip) { | |
| var success = false; | |
| zip.forEach(function(relativePath, zipEntry) { | |
| zipEntry.async('blob').then(function(blob) { | |
| handleEntry(blob, relativePath); | |
| success = true; | |
| console.log('Arquivo ' + relativePath + ' processado com JSZip.'); | |
| }); | |
| }); | |
| callback(success); | |
| }, function() { | |
| console.error('Erro ao descompactar com JSZip.'); | |
| callback(false); | |
| }); | |
| } | |
| function unzipWithFflate(data, callback) { | |
| fflate.unzip(data, function(err, unzipped) { | |
| if (err) { | |
| console.error('Erro ao descompactar com fflate:', err); | |
| callback(false); | |
| } else { | |
| var success = false; | |
| for (var key in unzipped) { | |
| if (unzipped.hasOwnProperty(key)) { | |
| handleEntry(new Blob([unzipped[key]]), key); | |
| success = true; | |
| console.log('Arquivo ' + key + ' processado com fflate.'); | |
| } | |
| } | |
| callback(success); | |
| } | |
| }); | |
| } | |
| function unzipWithPako(data, callback) { | |
| try { | |
| var inflated = pako.inflate(data); | |
| handleEntry(new Blob([inflated]), 'output.pdf'); // Assume que é um PDF descompactado | |
| callback(true); | |
| console.log('Arquivo descompactado com Pako.'); | |
| } catch (err) { | |
| console.error('Erro ao descompactar com Pako:', err); | |
| callback(false); | |
| } | |
| } | |
| function handleEntry(blob, filename) { | |
| if (/Mobi|Android/i.test(navigator.userAgent)) { | |
| // Se for um dispositivo móvel, prioriza o download | |
| var downloadLink = document.createElement('a'); | |
| downloadLink.href = URL.createObjectURL(blob); | |
| downloadLink.download = filename; | |
| downloadLink.textContent = 'Baixar PDF'; | |
| downloadContainer.style.display = 'block'; | |
| downloadContainer.appendChild(downloadLink); | |
| // Adiciona aviso para dispositivos móveis | |
| var aviso = document.createElement('p'); | |
| aviso.textContent = 'Para visualizar o PDF, por favor, baixe o arquivo.'; | |
| downloadContainer.appendChild(aviso); | |
| // Ainda tenta exibir no iframe, mas com prioridade para o download | |
| pdfIframe.src = URL.createObjectURL(blob); | |
| console.log('PDF carregado no iframe com URL.createObjectURL para visualização em dispositivo móvel.'); | |
| } else { | |
| // Para desktops, tenta exibir diretamente no iframe | |
| if (blob.type === 'application/pdf' || filename.toLowerCase().endsWith('.pdf')) { | |
| pdfIframe.src = URL.createObjectURL(blob); | |
| console.log('PDF carregado no iframe com URL.createObjectURL.'); | |
| } else { | |
| console.log('Arquivo não é PDF, ignorando para visualização direta.'); | |
| } | |
| } | |
| } | |
| var data = base64ToArrayBuffer(base64Data); | |
| processZipFile(data); | |
| }); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="viewer-container" style="height: 100%; width: 100%; border: 0" class="w-100 h-100 border-0"> | |
| <div id="download-link-container" style="display: none; text-align: center; margin-bottom: 10px;"></div> | |
| <iframe id="pdf-iframe" type="application/pdf" class="w-100 h-100 border-0"></iframe> | |
| <object id="pdf-viewer" type="application/pdf" class="d-none"> | |
| <param id="base64-param" value="<%= @file_base64 %>"> | |
| </object> | |
| </div> | |
| <script src="https://cdn.jsdelivr.net/npm/jszip/dist/jszip.min.js" type="text/javascript"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js" type="text/javascript"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.4/pako.min.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| document.addEventListener('DOMContentLoaded', function () { | |
| var base64Data = document.getElementById('base64-param').getAttribute('value'); | |
| var pdfIframe = document.getElementById('pdf-iframe'); | |
| var laudoNumber = new URLSearchParams(window.location.search).get('laudo'); | |
| var downloadContainer = document.getElementById('download-link-container'); | |
| function isBase64PDF(base64) { | |
| return base64.startsWith('JVBERi0'); | |
| } | |
| function base64ToArrayBuffer(base64) { | |
| var binaryString = window.atob(base64); | |
| var bytes = new Uint8Array(binaryString.length); | |
| for (var i = 0; i < binaryString.length; i++) { | |
| bytes[i] = binaryString.charCodeAt(i); | |
| } | |
| return bytes.buffer; | |
| } | |
| function processZipFile(data) { | |
| if (isBase64PDF(base64Data)) { | |
| pdfIframe.src = 'data:application/pdf;base64,' + base64Data; | |
| console.log('PDF carregado diretamente.'); | |
| } else { | |
| var dataArray = new Uint8Array(data); | |
| attemptUnzip(dataArray, ['JSZip', 'fflate', 'pako'], function() { | |
| console.log("Processo de descompactação completo."); | |
| }); | |
| } | |
| } | |
| function attemptUnzip(data, libraries, callback) { | |
| if (libraries.length === 0) { | |
| console.error("Nenhuma biblioteca de descompressão disponível."); | |
| return; | |
| } | |
| var library = libraries.shift(); | |
| switch (library) { | |
| case 'JSZip': | |
| if (window.JSZip) { | |
| unzipWithJSZip(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com JSZip falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| case 'fflate': | |
| if (window.fflate) { | |
| unzipWithFflate(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com fflate falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| case 'pako': | |
| if (window.pako) { | |
| unzipWithPako(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com pako falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| } | |
| } | |
| function unzipWithJSZip(data, callback) { | |
| JSZip.loadAsync(data).then(function(zip) { | |
| var success = false; | |
| zip.forEach(function(relativePath, zipEntry) { | |
| zipEntry.async('blob').then(function(blob) { | |
| handleEntry(blob, relativePath); | |
| success = true; | |
| console.log('Arquivo ' + relativePath + ' processado com JSZip.'); | |
| }); | |
| }); | |
| callback(success); | |
| }, function() { | |
| console.error('Erro ao descompactar com JSZip.'); | |
| callback(false); | |
| }); | |
| } | |
| function unzipWithFflate(data, callback) { | |
| fflate.unzip(data, function(err, unzipped) { | |
| if (err) { | |
| console.error('Erro ao descompactar com fflate:', err); | |
| callback(false); | |
| } else { | |
| var success = false; | |
| for (var key in unzipped) { | |
| if (unzipped.hasOwnProperty(key)) { | |
| handleEntry(new Blob([unzipped[key]]), key); | |
| success = true; | |
| console.log('Arquivo ' + key + ' processado com fflate.'); | |
| } | |
| } | |
| callback(success); | |
| } | |
| }); | |
| } | |
| function unzipWithPako(data, callback) { | |
| try { | |
| var inflated = pako.inflate(data); | |
| handleEntry(new Blob([inflated]), 'output.pdf'); // Assume que é um PDF descompactado | |
| callback(true); | |
| console.log('Arquivo descompactado com Pako.'); | |
| } catch (err) { | |
| console.error('Erro ao descompactar com Pako:', err); | |
| callback(false); | |
| } | |
| } | |
| function handleEntry(blob, filename) { | |
| var fileUrl = URL.createObjectURL(blob); | |
| if (blob.type === 'application/pdf' || filename.toLowerCase().endsWith('.pdf')) { | |
| pdfIframe.src = fileUrl; | |
| console.log('PDF carregado no iframe com URL.createObjectURL.'); | |
| // Se for um dispositivo móvel, oferece download mesmo para PDFs | |
| if (/Mobi|Android/i.test(navigator.userAgent)) { | |
| var downloadLink = document.createElement('a'); | |
| downloadLink.href = fileUrl; | |
| downloadLink.download = filename; | |
| downloadLink.textContent = 'Baixar PDF'; | |
| downloadContainer.style.display = 'block'; | |
| downloadContainer.appendChild(downloadLink); | |
| var aviso = document.createElement('p'); | |
| aviso.textContent = 'Para visualizar o PDF, por favor, baixe o arquivo.'; | |
| downloadContainer.appendChild(aviso); | |
| } | |
| } else { | |
| console.log('Arquivo não é PDF, ignorando para visualização direta.'); | |
| } | |
| // Revoga a URL após o carregamento do PDF no iframe | |
| pdfIframe.onload = function() { | |
| URL.revokeObjectURL(fileUrl); | |
| }; | |
| } | |
| var data = base64ToArrayBuffer(base64Data); | |
| processZipFile(data); | |
| }); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="viewer-container" style="height: 100%; width: 100%; border: 0" class="w-100 h-100 border-0"> | |
| <div id="download-link-container" style="display: none; text-align: center; margin-bottom: 10px;"></div> | |
| <iframe id="pdf-iframe" type="application/pdf" class="w-100 h-100 border-0"></iframe> | |
| <object id="pdf-viewer" type="application/pdf" class="d-none"> | |
| <param id="base64-param" value="<%= @file_base64 %>"> | |
| </object> | |
| </div> | |
| <script src="https://cdn.jsdelivr.net/npm/jszip/dist/jszip.min.js" type="text/javascript"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js" type="text/javascript"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.4/pako.min.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| document.addEventListener('DOMContentLoaded', function () { | |
| var base64Data = document.getElementById('base64-param').getAttribute('value'); | |
| var pdfIframe = document.getElementById('pdf-iframe'); | |
| var laudoNumber = new URLSearchParams(window.location.search).get('laudo'); | |
| var downloadContainer = document.getElementById('download-link-container'); | |
| // Função para obter ou definir a última biblioteca bem-sucedida | |
| function getLastSuccessLibrary() { | |
| return localStorage.getItem('lastSuccessLibrary') || 'JSZip'; // Padrão para JSZip | |
| } | |
| function setLastSuccessLibrary(library) { | |
| localStorage.setItem('lastSuccessLibrary', library); | |
| } | |
| function isBase64PDF(base64) { | |
| return base64.startsWith('JVBERi0'); | |
| } | |
| function base64ToArrayBuffer(base64) { | |
| var binaryString = window.atob(base64); | |
| var bytes = new Uint8Array(binaryString.length); | |
| for (var i = 0; i < binaryString.length; i++) { | |
| bytes[i] = binaryString.charCodeAt(i); | |
| } | |
| return bytes.buffer; | |
| } | |
| function blobToBase64(blob, callback) { | |
| var reader = new FileReader(); | |
| reader.onload = function() { | |
| var base64Data = reader.result.split(',')[1]; | |
| callback(base64Data); | |
| }; | |
| reader.readAsDataURL(blob); | |
| } | |
| function processZipFile(data) { | |
| if (isBase64PDF(base64Data)) { | |
| var blob = new Blob([base64ToArrayBuffer(base64Data)], { type: 'application/pdf' }); | |
| var fileUrl = URL.createObjectURL(blob); | |
| pdfIframe.src = fileUrl; | |
| console.log('PDF carregado diretamente.'); | |
| } else { | |
| var dataArray = new Uint8Array(data); | |
| var libraries = ['JSZip', 'fflate', 'pako']; | |
| // Move a última biblioteca bem-sucedida para o início do array | |
| var lastSuccess = getLastSuccessLibrary(); | |
| libraries = libraries.filter(lib => lib !== lastSuccess); | |
| libraries.unshift(lastSuccess); | |
| attemptUnzip(dataArray, libraries, function() { | |
| console.log("Processo de descompactação completo."); | |
| }); | |
| } | |
| } | |
| function attemptUnzip(data, libraries, callback) { | |
| if (libraries.length === 0) { | |
| console.error("Nenhuma biblioteca de descompressão disponível."); | |
| return; | |
| } | |
| var library = libraries.shift(); | |
| switch (library) { | |
| case 'JSZip': | |
| if (window.JSZip) { | |
| unzipWithJSZip(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com JSZip falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else { | |
| if (success) setLastSuccessLibrary('JSZip'); | |
| if (callback) callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| case 'fflate': | |
| if (window.fflate) { | |
| unzipWithFflate(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com fflate falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else { | |
| if (success) setLastSuccessLibrary('fflate'); | |
| if (callback) callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| case 'pako': | |
| if (window.pako) { | |
| unzipWithPako(data, function(success) { | |
| if (!success && libraries.length > 0) { | |
| console.log('Tentativa com pako falhou, tentando outro método.'); | |
| attemptUnzip(data, libraries, callback); | |
| } else { | |
| if (success) setLastSuccessLibrary('pako'); | |
| if (callback) callback(); | |
| } | |
| }); | |
| } else { | |
| attemptUnzip(data, libraries, callback); | |
| } | |
| break; | |
| } | |
| } | |
| function unzipWithJSZip(data, callback) { | |
| JSZip.loadAsync(data).then(function(zip) { | |
| var success = false; | |
| zip.forEach(function(relativePath, zipEntry) { | |
| zipEntry.async('blob').then(function(blob) { | |
| handleEntry(blob, relativePath); | |
| success = true; | |
| console.log('Arquivo ' + relativePath + ' processado com JSZip.'); | |
| }); | |
| }); | |
| callback(success); | |
| }, function() { | |
| console.error('Erro ao descompactar com JSZip.'); | |
| callback(false); | |
| }); | |
| } | |
| function unzipWithFflate(data, callback) { | |
| fflate.unzip(data, function(err, unzipped) { | |
| if (err) { | |
| console.error('Erro ao descompactar com fflate:', err); | |
| callback(false); | |
| } else { | |
| var success = false; | |
| for (var key in unzipped) { | |
| if (unzipped.hasOwnProperty(key)) { | |
| handleEntry(new Blob([unzipped[key]], { type: 'application/pdf' }), key); | |
| success = true; | |
| console.log('Arquivo ' + key + ' processado com fflate.'); | |
| } | |
| } | |
| callback(success); | |
| } | |
| }); | |
| } | |
| function unzipWithPako(data, callback) { | |
| try { | |
| var inflated = pako.inflate(data); | |
| handleEntry(new Blob([inflated], { type: 'application/pdf' }), `Laudo_${laudoNumber}.pdf`); | |
| callback(true); | |
| console.log('Arquivo descompactado com Pako.'); | |
| } catch (err) { | |
| console.error('Erro ao descompactar com Pako:', err); | |
| callback(false); | |
| } | |
| } | |
| function handleEntry(blob, filename) { | |
| if (blob.type === 'application/pdf' || filename.toLowerCase().endsWith('.pdf')) { | |
| // Ajuste específico para dispositivos móveis | |
| if (/Mobi|Android/i.test(navigator.userAgent)) { | |
| // Especialmente para Chrome Mobile, usamos base64 | |
| if (navigator.userAgent.indexOf('Chrome') !== -1) { | |
| blobToBase64(blob, function(base64Pdf) { | |
| pdfIframe.src = 'data:application/pdf;base64,' + base64Pdf; | |
| // Nome do arquivo no iframe para dispositivos móveis | |
| pdfIframe.setAttribute('title', `Laudo_${laudoNumber}.pdf`); | |
| console.log('PDF carregado no iframe com base64 para Chrome Mobile.'); | |
| }); | |
| } else if (/iPhone|iPod|iPad/i.test(navigator.userAgent)) { | |
| var fileUrl = URL.createObjectURL(blob); | |
| pdfIframe.style.height = '100vh'; | |
| pdfIframe.style.minHeight = '600px'; | |
| pdfIframe.style.overflowY = 'auto'; | |
| pdfIframe.src = fileUrl; | |
| // Nome do arquivo no iframe para iOS | |
| pdfIframe.setAttribute('title', `Laudo_${laudoNumber}.pdf`); | |
| console.log('PDF carregado no iframe com Blob para iPhone.'); | |
| pdfIframe.onload = function() { | |
| URL.revokeObjectURL(fileUrl); | |
| }; | |
| } else { | |
| // Para outros dispositivos móveis, tentamos com base64 | |
| blobToBase64(blob, function(base64Pdf) { | |
| pdfIframe.src = 'data:application/pdf;base64,' + base64Pdf; | |
| // Nome do arquivo no iframe para outros dispositivos móveis | |
| pdfIframe.setAttribute('title', `Laudo_${laudoNumber}.pdf`); | |
| console.log('PDF carregado no iframe com base64 para dispositivos móveis.'); | |
| }); | |
| } | |
| } else { | |
| // Para dispositivos desktop, usamos Blob | |
| var fileUrl = URL.createObjectURL(blob); | |
| pdfIframe.src = fileUrl; | |
| // Nome do arquivo no iframe para desktop | |
| pdfIframe.setAttribute('title', `Laudo_${laudoNumber}.pdf`); | |
| console.log('PDF carregado no iframe com Blob para desktop.'); | |
| pdfIframe.onload = function() { | |
| URL.revokeObjectURL(fileUrl); | |
| }; | |
| } | |
| // Oferta de download para dispositivos móveis | |
| if (/Mobi|Android/i.test(navigator.userAgent)) { | |
| downloadContainer.innerHTML = ''; | |
| var downloadLink = document.createElement('a'); | |
| var currentUrl = new URL(window.location.href); | |
| currentUrl.searchParams.set('modo', 'baixar'); | |
| downloadLink.href = currentUrl.href; | |
| downloadLink.download = `laudo_${laudoNumber}.zip`; | |
| downloadLink.textContent = 'Baixar ZIP'; | |
| downloadContainer.style.display = 'block'; | |
| downloadContainer.appendChild(downloadLink); | |
| var aviso = document.createElement('p'); | |
| aviso.textContent = 'Para melhor visualização, baixe o ZIP contendo o PDF.'; | |
| downloadContainer.appendChild(aviso); | |
| } | |
| } else { | |
| console.log('Arquivo não é PDF, ignorando para visualização direta.'); | |
| } | |
| } | |
| var data = base64ToArrayBuffer(base64Data); | |
| processZipFile(data); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment