(function () { "use strict"; function plain(value) { return String(value ?? "") .replaceAll("₺", "TL").replaceAll("€", "EUR") .replace(/[çÇ]/g, match => match === "ç" ? "c" : "C") .replace(/[ğĞ]/g, match => match === "ğ" ? "g" : "G") .replace(/[ıİ]/g, match => match === "ı" ? "i" : "I") .replace(/[öÖ]/g, match => match === "ö" ? "o" : "O") .replace(/[şŞ]/g, match => match === "ş" ? "s" : "S") .replace(/[üÜ]/g, match => match === "ü" ? "u" : "U") .normalize("NFD").replace(/[\u0300-\u036f]/g, "") .replace(/\s*·\s*/g, " | ") .replace(/[–—−]/g, "-").replace(/[^\x20-\x7e]/g, "?"); } function pdfText(value) { return plain(value).replace(/\\/g, "\\\\").replace(/\(/g, "\\(").replace(/\)/g, "\\)"); } function fit(value, width, fontSize = 8) { const limit = Math.max(4, Math.floor(width / (fontSize * .55))); const text = plain(value); return text.length > limit ? `${text.slice(0, Math.max(1, limit - 3))}...` : text; } function makePdf(title, subtitle, columns, rows, options = {}) { const pageWidth = 842, pageHeight = 595, margin = 36, tableWidth = pageWidth - margin * 2; const requested = Array.isArray(options.widths) && options.widths.length === columns.length ? options.widths : columns.map(() => 1); const widthTotal = requested.reduce((sum, value) => sum + Number(value || 1), 0); const widths = requested.map(value => tableWidth * Number(value || 1) / widthTotal); const rowHeight = 18, firstY = 487, rowsPerPage = 25; const pages = []; const chunks = rows.length ? Array.from({length: Math.ceil(rows.length / rowsPerPage)}, (_, index) => rows.slice(index * rowsPerPage, (index + 1) * rowsPerPage)) : [[]]; chunks.forEach((pageRows, pageIndex) => { const commands = []; commands.push("0.07 0.31 0.23 rg 36 535 770 32 re f"); commands.push(`BT /F1 16 Tf 1 1 1 rg 48 547 Td (${pdfText(title)}) Tj ET`); commands.push(`BT /F1 9 Tf 0.15 0.24 0.2 rg 40 515 Td (${pdfText(fit(subtitle, 650, 9))}) Tj ET`); commands.push(`BT /F1 8 Tf 0.35 0.42 0.39 rg 720 519 Td (Sayfa ${pageIndex + 1} / ${chunks.length}) Tj ET`); let x = margin; columns.forEach((column, index) => { commands.push(`0.88 0.94 0.91 rg ${x.toFixed(2)} ${firstY} ${widths[index].toFixed(2)} ${rowHeight} re f`); commands.push(`0.55 0.65 0.6 RG ${x.toFixed(2)} ${firstY} ${widths[index].toFixed(2)} ${rowHeight} re S`); commands.push(`BT /F1 8 Tf 0.05 0.22 0.16 rg ${(x + 4).toFixed(2)} ${(firstY + 6).toFixed(2)} Td (${pdfText(fit(column, widths[index] - 8, 8))}) Tj ET`); x += widths[index]; }); pageRows.forEach((row, rowIndex) => { const y = firstY - (rowIndex + 1) * rowHeight; let cellX = margin; row.forEach((cell, columnIndex) => { if (rowIndex % 2) commands.push(`0.97 0.98 0.97 rg ${cellX.toFixed(2)} ${y} ${widths[columnIndex].toFixed(2)} ${rowHeight} re f`); commands.push(`0.75 0.8 0.77 RG ${cellX.toFixed(2)} ${y} ${widths[columnIndex].toFixed(2)} ${rowHeight} re S`); commands.push(`BT /F1 8 Tf 0.08 0.13 0.11 rg ${(cellX + 4).toFixed(2)} ${(y + 6).toFixed(2)} Td (${pdfText(fit(cell, widths[columnIndex] - 8, 8))}) Tj ET`); cellX += widths[columnIndex]; }); }); if (!pageRows.length) commands.push("BT /F1 11 Tf 0.35 0.42 0.39 rg 40 475 Td (Bu donemde hareket bulunmuyor.) Tj ET"); commands.push(`BT /F1 8 Tf 0.35 0.42 0.39 rg 36 22 Td (Sut ve Yem Takip - ${pdfText(new Date().toLocaleDateString("tr-TR"))}) Tj ET`); pages.push(commands.join("\n")); }); const objects = [null, "<< /Type /Catalog /Pages 2 0 R >>", "", "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"]; const pageIds = []; pages.forEach(content => { const pageId = objects.length, contentId = pageId + 1; pageIds.push(pageId); objects.push(`<< /Type /Page /Parent 2 0 R /MediaBox [0 0 ${pageWidth} ${pageHeight}] /Resources << /Font << /F1 3 0 R >> >> /Contents ${contentId} 0 R >>`); objects.push(`<< /Length ${content.length} >>\nstream\n${content}\nendstream`); }); objects[2] = `<< /Type /Pages /Kids [${pageIds.map(id => `${id} 0 R`).join(" ")}] /Count ${pageIds.length} >>`; let pdf = "%PDF-1.4\n", offsets = [0]; for (let id = 1; id < objects.length; id++) { offsets[id] = pdf.length; pdf += `${id} 0 obj\n${objects[id]}\nendobj\n`; } const xref = pdf.length; pdf += `xref\n0 ${objects.length}\n0000000000 65535 f \n`; for (let id = 1; id < objects.length; id++) pdf += `${String(offsets[id]).padStart(10, "0")} 00000 n \n`; pdf += `trailer\n<< /Size ${objects.length} /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF`; return new Blob([pdf], {type: "application/pdf"}); } function downloadTablePdf(filename, title, subtitle, columns, rows, options) { const url = URL.createObjectURL(makePdf(title, subtitle, columns, rows, options)); const anchor = document.createElement("a"); anchor.href = url; anchor.download = filename; document.body.append(anchor); anchor.click(); anchor.remove(); setTimeout(() => URL.revokeObjectURL(url), 1500); } window.SutYemPdf = {downloadTablePdf}; })();