I want to bulk generate certificates for my students, to do so I've created an external script (not in Laravel) that gets a JSON file with student data as input and outputs a PDF file containing all certificates, my goal is to integrate this script into my Laravel application
- Retrieve students as an array and convert it to JSON
- Create a timestamp-ed json file containing that array
- Run the script and pass it the JSON file as well as a filename for the PDF that it's going to output
- Download the PDF file for the user
- Delete the JSON & PDF files
Here's what I came up with
$students = [
'students' => [
[
'name' => 'John Doe',
'course' => 'Horse Nail Clipping',
'date' => 'January 12th, 2025',
'hours' => 13,
],
[
'name' => 'Jane Doe',
'course' => 'Video Editing',
'date' => 'January 17th, 2025',
'hours' => 40,
],
],
];
// Save JSON file
$filename = 'cert-data-'.now()->timestamp.'.json';
Storage::disk('local')->put($filename, json_encode($students, JSON_PRETTY_PRINT));
$jsonPath = 'app/'.$filename;
$pdfName = pathinfo($filename, PATHINFO_FILENAME).'.pdf';
$pdfPath = storage_path('app/'.$pdfName);
// Run Typst
$command = 'typst compile '.storage_path('cert.typ')." $pdfPath "."--input data=$jsonPath";
$proc = Process::run($command);
Now this works when put in a handle method of a command but when I move it to a HTTP route and try to download the resulting file it fails to generate the PDF and thus download
I also feel like the way I've done this whole thing is wrong and not how Laravel is designed to handle external scripts (especially the constant creation and deletion of files feels flaky) so if you know a better way to do this let me know