Add URL-based PDF generation support
All checks were successful
Deploy / deploy (push) Successful in 8s
All checks were successful
Deploy / deploy (push) Successful in 8s
This commit is contained in:
parent
e3534dcefb
commit
09afbff2fd
5 changed files with 49 additions and 7 deletions
|
|
@ -39,18 +39,25 @@ class GenerateController
|
|||
}
|
||||
|
||||
// Valider les données
|
||||
if (empty($data['html'])) {
|
||||
$this->sendError(400, 'HTML content required');
|
||||
if (empty($data['url']) && empty($data['html'])) {
|
||||
$this->sendError(400, 'url or html required');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Générer le PDF
|
||||
$pdf = $this->generator->generate(
|
||||
$data['html'],
|
||||
$data['css'] ?? null,
|
||||
$data['options'] ?? []
|
||||
);
|
||||
if (!empty($data['url'])) {
|
||||
$pdf = $this->generator->generateFromUrl(
|
||||
$data['url'],
|
||||
$data['options'] ?? []
|
||||
);
|
||||
} else {
|
||||
$pdf = $this->generator->generate(
|
||||
$data['html'],
|
||||
$data['css'] ?? null,
|
||||
$data['options'] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
// Retourner le PDF
|
||||
header('Content-Type: application/pdf');
|
||||
|
|
|
|||
|
|
@ -47,6 +47,41 @@ class PdfGenerator
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue