102 lines
3.6 KiB
JavaScript
102 lines
3.6 KiB
JavaScript
import PDFDocument from 'pdfkit';
|
||
import QRCode from 'qrcode';
|
||
import { fileURLToPath } from 'node:url';
|
||
|
||
const font = fileURLToPath(new URL('../assets/fonts/DejaVuSans.ttf', import.meta.url));
|
||
export function loginLink(baseUrl, user) {
|
||
const url = new URL(baseUrl);
|
||
url.hash = '/login?' + new URLSearchParams({ l: user.login, p: user.passwordForAdmin });
|
||
return url.href;
|
||
}
|
||
|
||
// A4, 3 × 4 cards per page. QR modules are vectors with a four-module quiet zone.
|
||
export function createExpertQrPdf(users, baseUrl) {
|
||
return new Promise((resolve, reject) => {
|
||
const doc = new PDFDocument({
|
||
size: 'A4',
|
||
margin: 24,
|
||
autoFirstPage: false,
|
||
info: { Title: 'Эксперты — QR-коды входа', Author: 'CyberGarden' },
|
||
});
|
||
const chunks = [];
|
||
doc.on('data', chunk => chunks.push(chunk));
|
||
doc.on('end', () => resolve(Buffer.concat(chunks)));
|
||
doc.on('error', reject);
|
||
try {
|
||
doc.registerFont('Names', font);
|
||
const technical = users.filter(user => user.role === 'technical');
|
||
const regular = users.filter(user => user.role !== 'technical');
|
||
const headingHeight = 22;
|
||
let column = 0;
|
||
let rowY = 24;
|
||
let groupTitle = '';
|
||
function cardHeight() {
|
||
return (doc.page.height - 48 - headingHeight) / 4;
|
||
}
|
||
function drawHeading() {
|
||
doc
|
||
.font('Names')
|
||
.fontSize(12)
|
||
.fillColor('#2c634c')
|
||
.text(groupTitle, 24, rowY + 4, { width: doc.page.width - 48, height: 18 });
|
||
rowY += headingHeight;
|
||
}
|
||
function newPage() {
|
||
doc.addPage();
|
||
rowY = 24;
|
||
column = 0;
|
||
drawHeading();
|
||
}
|
||
function startGroup(title) {
|
||
groupTitle = title;
|
||
newPage();
|
||
}
|
||
function drawCard(user) {
|
||
if (!column && rowY + cardHeight() > doc.page.height - 24 + 0.01) newPage();
|
||
const width = (doc.page.width - 48) / 3;
|
||
const height = cardHeight();
|
||
const x = 24 + column * width;
|
||
const y = rowY;
|
||
doc.lineWidth(0.4).strokeColor('#d4d4d4').rect(x, y, width, height).stroke();
|
||
const link = loginLink(baseUrl, user);
|
||
const matrix = QRCode.create(link, { errorCorrectionLevel: 'M' }).modules;
|
||
const size = 124;
|
||
const module = size / (matrix.size + 8);
|
||
const qrX = x + (width - size) / 2;
|
||
const qrY = y + 12;
|
||
doc.fillColor('#ffffff').rect(qrX, qrY, size, size).fill();
|
||
doc.fillColor('#000000');
|
||
for (let row = 0; row < matrix.size; row++)
|
||
for (let col = 0; col < matrix.size; col++) {
|
||
if (matrix.get(row, col)) doc.rect(qrX + (col + 4) * module, qrY + (row + 4) * module, module, module);
|
||
}
|
||
doc.fill();
|
||
doc.link(qrX, qrY, size, size, link);
|
||
const fullName = [user.lastName, user.firstName, user.middleName].filter(Boolean).join(' ');
|
||
const cases = (user.cases || [])
|
||
.map(task => task.name)
|
||
.sort((a, b) => a.localeCompare(b, 'ru'))
|
||
.join(', ');
|
||
const label = user.role === 'technical' ? fullName + '\n' + (cases || 'Кейс не назначен') : fullName;
|
||
const textOptions = { width: width - 18, align: 'center', lineGap: 2 };
|
||
let fontSize = 11;
|
||
doc.font('Names').fontSize(fontSize);
|
||
while (doc.heightOfString(label, textOptions) > height - 147 && fontSize > 7) doc.fontSize(--fontSize);
|
||
doc.fillColor('#000000').text(label, x + 9, y + 142, { ...textOptions, height: height - 147, ellipsis: true });
|
||
if (++column === 3) {
|
||
column = 0;
|
||
rowY += height;
|
||
}
|
||
}
|
||
if (technical.length) startGroup('Тех. эксперты');
|
||
for (const user of technical) drawCard(user);
|
||
if (regular.length) startGroup('Эксперты');
|
||
for (const user of regular) drawCard(user);
|
||
doc.end();
|
||
} catch (error) {
|
||
doc.destroy();
|
||
reject(error);
|
||
}
|
||
});
|
||
}
|