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
|
// Valider les données
|
||||||
if (empty($data['html'])) {
|
if (empty($data['url']) && empty($data['html'])) {
|
||||||
$this->sendError(400, 'HTML content required');
|
$this->sendError(400, 'url or html required');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Générer le PDF
|
// Générer le PDF
|
||||||
$pdf = $this->generator->generate(
|
if (!empty($data['url'])) {
|
||||||
$data['html'],
|
$pdf = $this->generator->generateFromUrl(
|
||||||
$data['css'] ?? null,
|
$data['url'],
|
||||||
$data['options'] ?? []
|
$data['options'] ?? []
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
$pdf = $this->generator->generate(
|
||||||
|
$data['html'],
|
||||||
|
$data['css'] ?? null,
|
||||||
|
$data['options'] ?? []
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Retourner le PDF
|
// Retourner le PDF
|
||||||
header('Content-Type: application/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
|
private function createTempFile(string $html, ?string $css): string
|
||||||
{
|
{
|
||||||
$fullHtml = $html;
|
$fullHtml = $html;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue