config = $config; } public function generate(string $html, ?string $css = null, array $options = []): string { // Créer un fichier HTML temporaire $htmlFile = $this->createTempFile($html, $css); $pdfFile = tempnam($this->config['tmp_dir'], 'pdf_') . '.pdf'; try { // Construire la commande Paged.js CLI $command = $this->buildCommand($htmlFile, $pdfFile, $options); // Exécuter Paged.js CLI $output = []; $returnCode = 0; exec($command . ' 2>&1', $output, $returnCode); if ($returnCode !== 0) { $this->log('Paged.js CLI error: ' . implode("\n", $output)); throw new \Exception('PDF generation failed: ' . implode("\n", $output)); } // Lire le PDF généré if (!file_exists($pdfFile)) { throw new \Exception('PDF file not created'); } $pdfContent = file_get_contents($pdfFile); return $pdfContent; } finally { // Nettoyer les fichiers temporaires @unlink($htmlFile); @unlink($pdfFile); } } public function generateFromUrl(string $url, array $options = []): string { $pdfFile = tempnam($this->config['tmp_dir'], 'pdf_') . '.pdf'; try { $cmd = escapeshellcmd($this->config['pagedjs_bin']); $cmd .= ' ' . escapeshellarg($url); $cmd .= ' -o ' . escapeshellarg($pdfFile); if (!empty($options['timeout'])) { $cmd .= ' --timeout ' . (int)$options['timeout']; } else { $cmd .= ' --timeout ' . ($this->config['pagedjs_timeout'] * 1000); } $output = []; $returnCode = 0; exec($cmd . ' 2>&1', $output, $returnCode); if ($returnCode !== 0) { $this->log('Paged.js CLI error (URL): ' . implode("\n", $output)); throw new \Exception('PDF generation failed: ' . implode("\n", $output)); } if (!file_exists($pdfFile)) { throw new \Exception('PDF file not created'); } return file_get_contents($pdfFile); } finally { @unlink($pdfFile); } } private function createTempFile(string $html, ?string $css): string { $fullHtml = $html; // Injecter le CSS dans le HTML si fourni if ($css) { $styleTag = ""; if (stripos($html, '') !== false) { $fullHtml = str_ireplace('', $styleTag . '', $html); } else { // Si pas de
, créer une structure HTML complète $fullHtml = "{$styleTag}{$html}"; } } // S'assurer que le HTML a une structure complète if (stripos($fullHtml, '{$fullHtml}"; } $tempFile = tempnam($this->config['tmp_dir'], 'html_') . '.html'; file_put_contents($tempFile, $fullHtml); return $tempFile; } private function buildCommand(string $htmlFile, string $pdfFile, array $options): string { $cmd = escapeshellcmd($this->config['pagedjs_bin']); $cmd .= ' ' . escapeshellarg($htmlFile); $cmd .= ' -o ' . escapeshellarg($pdfFile); // Options supplémentaires pour Paged.js CLI // Note: Paged.js utilise les règles CSS @page pour le format // Les options sont limitées dans la CLI // Timeout (millisecondes) if (!empty($options['timeout'])) { $cmd .= ' --timeout ' . (int)$options['timeout']; } else { $cmd .= ' --timeout ' . ($this->config['pagedjs_timeout'] * 1000); } return $cmd; } private function log(string $message): void { $timestamp = date('Y-m-d H:i:s'); $logMessage = "[{$timestamp}] {$message}\n"; file_put_contents($this->config['log_file'], $logMessage, FILE_APPEND); } }