+ /**
+ * @throws PdfExportException
+ */
+ protected function renderUsingCommand(string $html): string
+ {
+ $command = config('exports.pdf_command');
+ $inputHtml = tempnam(sys_get_temp_dir(), 'bs-pdfgen-html-');
+ $outputPdf = tempnam(sys_get_temp_dir(), 'bs-pdfgen-output-');
+
+ $replacementsByPlaceholder = [
+ '{input_html_path}' => $inputHtml,
+ '{output_html_path}' => $outputPdf,
+ ];
+
+ foreach ($replacementsByPlaceholder as $placeholder => $replacement) {
+ $command = str_replace($placeholder, escapeshellarg($replacement), $command);
+ }
+
+ file_put_contents($inputHtml, $html);
+
+ $process = Process::fromShellCommandline($command);
+ $process->setTimeout(15);
+ $process->run();
+
+ if (!$process->isSuccessful()) {
+ throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
+ }
+
+ $pdfContents = file_get_contents($outputPdf);
+ unlink($outputPdf);
+
+ if ($pdfContents === false) {
+ throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");
+ } else if (empty($pdfContents)) {
+ throw new PdfExportException("PDF Export via command failed, PDF output file is empty");
+ }
+
+ return $pdfContents;
+ }
+