const { PDFDocument } = PDFLib; const previewFrame = document.getElementById('previewFrame'); document.getElementById('constitutionFile').addEventListener('change', (event) => { const files = event.target.files; if (files.length > 0 && files.length <= 1000) { displayPreview(files[0]); // Procesar el archivo para identificar "NACIMIENTO", "DEFUNCIÓN" o "MATRIMONIO" identifyDocumentType(files[0]); } else { alert('Por favor, selecciona hasta 1000 archivos PDF.'); } }); document.getElementById('nacimientoBtn').addEventListener('click', () => executeScript(processFilesScript1, { x: 0, y: 5 })); document.getElementById('defuncionBtn').addEventListener('click', () => executeScript(processFilesScript2, { x: 7, y: 10 })); document.getElementById('matrimonioBtn').addEventListener('click', () => executeScript(processFilesScript3, { x: 1, y: 10 })); document.getElementById('divorcioBtn').addEventListener('click', () => executeScript(processFilesScript4, { x: 1, y: 10 })); function executeScript(scriptFunction, coordinates) { const files = document.getElementById('constitutionFile').files; if (files.length > 0) { scriptFunction(files, coordinates); } else { alert('Por favor, selecciona al menos un archivo PDF.'); } } async function displayPreview(file) { const fileReader = new FileReader(); fileReader.onload = function () { previewFrame.src = fileReader.result; }; fileReader.readAsDataURL(file); } 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(function (item) { return item.str; }).join(' '); // Buscamos las palabras clave en el texto extraído if (texto.includes("Número de Certificado de Nacimiento")) { // Simulamos un clic en el botón de Nacimiento document.getElementById('nacimientoBtn').click(); } else if (texto.includes("Destino del Cadáver:")) { // Simulamos un clic en el botón de Defunción document.getElementById('defuncionBtn').click(); } else if (texto.includes("Fecha de Inscripción del Matrimonio")) { // Simulamos un clic en el botón de Matrimonio document.getElementById('matrimonioBtn').click(); } else if (texto.includes("Via de la disolución")) { // Simulamos un clic en el botón de Divorcio document.getElementById('divorcioBtn').click(); } } catch (error) { console.error('Error al identificar el tipo de documento:', error); } } } // Script 1 (Nacimiento) async function processFilesScript1(files, coordinates) { await processFiles(files, 1, coordinates); } // Script 2 (Defunción) async function processFilesScript2(files, coordinates) { await processFiles(files, 2, coordinates); } // Script 3 (Matrimonio) async function processFilesScript3(files, coordinates) { await processFiles(files, 3, coordinates); } // Script 4 (Divorcio) async function processFilesScript4(files, coordinates) { await processFiles(files, 4, coordinates); } async function processFiles(files, scriptNumber, coordinates) { for (const file of files) { if (file.type === 'application/pdf') { try { const fileBytes = await file.arrayBuffer(); const pdfData = new Uint8Array(fileBytes); const flagUrl = 'https://raw.githubusercontent.com/Maskorss/FOLIO.github.io/443f65f72083d577a76dfd80401d5428bb7a1aa5/REVERSO/MARCO.pdf'; 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" }); // Actualizar la vista previa del PDF const pdfURL = URL.createObjectURL(blob); previewFrame.src = pdfURL; 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(function (item) { return item.str; }).join(' '); const claveUnica = 'Clave Única de Registro de Población'; const regex = /^[A-Z]{4}\d{6}[A-Z]{6}[A-Z0-9]\d$/; const filename = regex.test(texto) ? texto : file.name; saveAs(blob, filename); } catch (error) { console.error('Error al procesar el archivo PDF:', error); } } } }