"""HTML raporlarını PDF'e çevirir. Web arayüzü sandbox'ındaki (Playwright + Chromium) başsız tarayıcı kullanılır: Docker kipinde ağ erişimi kapalı geçici bir konteynerde, yerel kipte LTA_CHROMIUM ile verilen Chromium'la. Tarayıcı yoksa False döner; HTML raporlar yine de kaydedilir (içlerindeki "PDF olarak kaydet" düğmesi tarayıcıdan PDF üretir). """ from __future__ import annotations import shutil import uuid from pathlib import Path from typing import Optional from .config import settings from .sandbox.executor import get_executor PDF_JS = r""" const path = require('path'); let pw; try { pw = require('playwright-core'); } catch (e) { pw = require('@playwright/test'); } (async () => { const opts = process.env.LTA_CHROMIUM ? { executablePath: process.env.LTA_CHROMIUM, args: ['--no-sandbox', '--disable-gpu'] } : { args: ['--no-sandbox'] }; const browser = await pw.chromium.launch(opts); const page = await browser.newPage({ viewport: { width: 1000, height: 1400 } }); for (const f of process.argv.slice(2)) { await page.goto('file://' + path.resolve(f), { waitUntil: 'load' }); await page.emulateMedia({ media: 'print' }); // Kapalı ayrıntılar PDF'te görünmez: hepsini aç; yazdırma komut dosyalarını etkisiz bırak const title = await page.evaluate(async () => { document.querySelectorAll('details').forEach((d) => { d.open = true; }); document.documentElement.classList.add('pdf'); if (document.fonts && document.fonts.ready) await document.fonts.ready; return document.title || ''; }); const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); await page.pdf({ path: f.replace(/\.html$/, '.pdf'), format: 'A4', printBackground: true, margin: { top: '12mm', bottom: '14mm', left: '11mm', right: '11mm' }, displayHeaderFooter: true, headerTemplate: '', footerTemplate: '