Created
February 16, 2026 20:15
-
-
Save renanalencar/483607bbf316f6f952f729b283f2ab6b to your computer and use it in GitHub Desktop.
Script para injetar jsPDF via console em sites com política de segurança 'TrustedScriptURL'. Útil para converter sequências de imagens blob em um arquivo PDF único.
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
| (function() { | |
| // 1. Tratamento de Trusted Types para carregar o jsPDF | |
| let trustedUrl; | |
| const rawUrl = "https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js"; | |
| if (window.trustedTypes && window.trustedTypes.createPolicy) { | |
| if (!window.trustedTypes.defaultPolicy) { | |
| const policy = window.trustedTypes.createPolicy('forceLoad', { | |
| createScriptURL: (url) => url | |
| }); | |
| trustedUrl = policy.createScriptURL(rawUrl); | |
| } else { | |
| trustedUrl = rawUrl; | |
| } | |
| } else { | |
| trustedUrl = rawUrl; | |
| } | |
| const script = document.createElement("script"); | |
| script.src = trustedUrl; | |
| script.onload = function() { | |
| console.log("PDF Lib carregada. Iniciando captura..."); | |
| // Criar o PDF (formato A4) | |
| const pdf = new jsPDF('p', 'mm', 'a4'); | |
| const pdfWidth = pdf.internal.pageSize.getWidth(); | |
| const pdfHeight = pdf.internal.pageSize.getHeight(); | |
| // No Google Drive, as páginas geralmente ficam em elementos com a classe 'a-b-c-d' ou tags 'img' | |
| // Vamos buscar todas as imagens que pareçam ser as páginas do documento | |
| const imagens = Array.from(document.querySelectorAll('img')).filter(img => { | |
| return img.src.includes('blob:') || img.src.includes('googleusercontent'); | |
| }); | |
| if (imagens.length === 0) { | |
| console.error("Nenhuma imagem de página encontrada. Role o documento até o fim para carregar as páginas."); | |
| return; | |
| } | |
| imagens.forEach((img, index) => { | |
| try { | |
| const canvas = document.createElement('canvas'); | |
| const ctx = canvas.getContext("2d"); | |
| // Usar naturalWidth para melhor qualidade | |
| canvas.width = img.naturalWidth; | |
| canvas.height = img.naturalHeight; | |
| ctx.drawImage(img, 0, 0, canvas.width, canvas.height); | |
| const imgData = canvas.toDataURL("image/jpeg", 0.95); | |
| if (index > 0) pdf.addPage(); | |
| // Ajustar a imagem para caber na página A4 mantendo a proporção | |
| const ratio = Math.min(pdfWidth / canvas.width, pdfHeight / canvas.height); | |
| const width = canvas.width * ratio; | |
| const height = canvas.height * ratio; | |
| pdf.addImage(imgData, "JPEG", 0, 0, width, height); | |
| console.log(`Página ${index + 1} processada.`); | |
| } catch (e) { | |
| console.warn(`Erro na página ${index}:`, e); | |
| } | |
| }); | |
| pdf.save("Documento_GoogleDrive.pdf"); | |
| console.log("Download concluído!"); | |
| }; | |
| document.body.appendChild(script); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment