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
|
|
@ -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