From 2aeb5a0aaf2e6190660018dc3de6d84510c0b6ce Mon Sep 17 00:00:00 2001 From: root Date: Mon, 2 Feb 2026 23:05:44 -0300 Subject: [PATCH] Feat(Legislativo):Baixar Materias Legislativas [AB#849] --- .../js/materia_pesquisa_download_pdfs.js | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 sapl/static/js/materia_pesquisa_download_pdfs.js diff --git a/sapl/static/js/materia_pesquisa_download_pdfs.js b/sapl/static/js/materia_pesquisa_download_pdfs.js new file mode 100644 index 000000000..33e28fab3 --- /dev/null +++ b/sapl/static/js/materia_pesquisa_download_pdfs.js @@ -0,0 +1,85 @@ +/* + * Adiciona botões de download ao lado do link "Texto Original" + * na tela de resultados de pesquisa de matérias. + */ + +(function () { + function downloadFile(url, filename) { + // Usa fetch para baixar o arquivo como blob e forçar download + fetch(url) + .then(response => { + if (!response.ok) { + throw new Error('Erro ao baixar arquivo'); + } + return response.blob(); + }) + .then(blob => { + // Cria URL temporária do blob + const blobUrl = window.URL.createObjectURL(blob); + + // Cria link temporário e força download + const link = document.createElement('a'); + link.href = blobUrl; + link.download = filename || 'documento.pdf'; + link.style.display = 'none'; + + document.body.appendChild(link); + link.click(); + + // Limpa recursos + document.body.removeChild(link); + window.URL.revokeObjectURL(blobUrl); + }) + .catch(error => { + console.error('Erro ao baixar PDF:', error); + // Fallback: abre em nova aba se o download falhar + window.open(url, '_blank'); + }); + } + + function injectButtons() { + // Busca todos os elementos que contêm link "Texto Original" + const strongElements = Array.from(document.querySelectorAll('strong')); + + strongElements.forEach((strong) => { + const link = strong.querySelector('a'); + if (!link || link.textContent.trim() !== 'Texto Original') return; + + // evita duplicação + if (strong.nextSibling && strong.nextSibling.nodeType === Node.ELEMENT_NODE + && strong.nextSibling.hasAttribute('data-sapl-download-pdf-btn')) return; + + const pdfUrl = link.getAttribute('href'); + if (!pdfUrl) return; + + // extrai nome do arquivo da URL + const filename = pdfUrl.split('/').pop() || 'texto_original.pdf'; + + // cria botão com ícone FontAwesome + const btn = document.createElement('a'); + btn.setAttribute('data-sapl-download-pdf-btn', '1'); + btn.className = 'btn btn-sm btn-outline-primary ml-1'; + btn.title = 'Baixar PDF'; + btn.href = 'javascript:void(0);'; + btn.innerHTML = ''; + + // evento de click para forçar download + btn.addEventListener('click', function(e) { + e.preventDefault(); + e.stopPropagation(); + downloadFile(pdfUrl, filename); + return false; + }); + + // insere logo após o + strong.parentNode.insertBefore(document.createTextNode(' '), strong.nextSibling); + strong.parentNode.insertBefore(btn, strong.nextSibling); + }); + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', injectButtons); + } else { + injectButtons(); + } +})();