const { PDFDocument } = PDFLib; const basePDFUrls = { nacimiento: 'https://raw.githubusercontent.com/Maskorss/FOLIO.github.io/09af5208bb9674c26dd040b4293c9ab57a89a60c/REVERSO/NACIMIENTO.pdf', defuncion: 'https://raw.githubusercontent.com/Maskorss/FOLIO.github.io/09af5208bb9674c26dd040b4293c9ab57a89a60c/REVERSO/DEFUNCION.pdf', matrimonio: 'https://raw.githubusercontent.com/Maskorss/FOLIO.github.io/09af5208bb9674c26dd040b4293c9ab57a89a60c/REVERSO/MATRIMONIO.pdf', divorcio: 'https://raw.githubusercontent.com/Maskorss/FOLIO.github.io/09af5208bb9674c26dd040b4293c9ab57a89a60c/REVERSO/DIVORCIO.pdf', }; document.getElementById('constitutionFile').addEventListener('change', (event) => { const files = event.target.files; if (files.length > 0 && files.length <= 1000) { identifyDocumentType(files[0]); } else { alert('Por favor, selecciona hasta 1000 archivos PDF.'); } }); document.getElementById('nacimientoBtn').addEventListener('click', () => executeScript(processFiles, { x: 0, y: 5 }, 'nacimiento')); document.getElementById('defuncionBtn').addEventListener('click', () => executeScript(processFiles, { x: 7, y: 10 }, 'defuncion')); document.getElementById('matrimonioBtn').addEventListener('click', () => executeScript(processFiles, { x: 1, y: 10 }, 'matrimonio')); document.getElementById('divorcioBtn').addEventListener('click', () => executeScript(processFiles, { x: 1, y: 10 }, 'divorcio')); function executeScript(scriptFunction, coordinates, docType) { const files = document.getElementById('constitutionFile').files; if (files.length > 0) { scriptFunction(files, coordinates, docType); } else { alert('Por favor, selecciona al menos un archivo PDF.'); } } async function identifyDocumentType(file) { if (file.type === 'application/pdf') { try { const fileBytes = await file.arrayBuffer(); const pdf = await pdfjsLib.getDocument({ data: new Uint8Array(fileBytes) }).promise; const page1 = await pdf.getPage(1); const textContent = await page1.getTextContent(); const texto = textContent.items.map(item => item.str).join(' '); // 🔍 Mostrar el texto extraído en la consola console.log("Texto extraído del PDF:", texto); if (texto.includes("Número de Certificado de Nacimiento")) { executeScript(processFiles, { x: 0, y: 5 }, 'nacimiento'); } else if (texto.includes("Destino del Cadáver:")) { executeScript(processFiles, { x: 0, y: 5 }, 'defuncion'); } else if (texto.includes("Fecha de Inscripción del Matrimonio")) { executeScript(processFiles, { x: 1, y: 3 }, 'matrimonio'); } else if (texto.includes("Via de la disolución")) { executeScript(processFiles, { x: 1, y: 10 }, 'divorcio'); } } catch (error) { console.error('Error al identificar el tipo de documento:', error); } } } async function processFiles(files, coordinates, docType) { for (const file of files) { if (file.type === 'application/pdf') { try { const fileBytes = await file.arrayBuffer(); const pdfData = new Uint8Array(fileBytes); const flagUrl = basePDFUrls[docType]; const flagPdfBytes = await fetch(flagUrl).then(res => res.arrayBuffer()); const flagPdfDoc = await PDFDocument.load(flagPdfBytes); const flagPage = flagPdfDoc.getPages()[0]; const flagPageWidth = flagPage.getWidth(); const flagPageHeight = flagPage.getHeight(); const pdfDoc = await PDFDocument.create(); const page = pdfDoc.addPage([flagPageWidth, flagPageHeight]); const [background] = await pdfDoc.embedPdf(flagPdfBytes); page.drawPage(background, { x: 0, y: 0, width: flagPageWidth, height: flagPageHeight, }); const loadedPdf = await PDFDocument.load(pdfData); const embeddedPage = await pdfDoc.embedPage(loadedPdf.getPages()[0], { left: 0, bottom: 0, right: 600, top: 900, }); const embeddedDims = embeddedPage.scale(1); page.drawPage(embeddedPage, { ...embeddedDims, x: coordinates.x, y: coordinates.y, }); const pdfBytes = await pdfDoc.save(); const blob = new Blob([new Uint8Array(pdfBytes)], { type: "application/pdf" }); const pdf = await pdfjsLib.getDocument({ data: new Uint8Array(pdfBytes) }).promise; const page1 = await pdf.getPage(1); const textContent = await page1.getTextContent(); const texto = textContent.items.map(item => item.str).join(' '); const filename = file.name; saveAs(blob, filename); } catch (error) { console.error('Error al procesar el archivo PDF:', error); } } } }