Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 34 additions & 45 deletions app/CertificateParticipation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Log;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

Expand All @@ -25,7 +24,6 @@ class CertificateParticipation

private $personalized_template_name;

//private $event;
private $id;

private $event_name;
Expand All @@ -40,42 +38,36 @@ class CertificateParticipation

public function __construct($name_for_certificate, $event_name, $event_date)
{
ini_set('max_execution_time', 600); // Set max execution time to 600 seconds
ini_set('memory_limit', '512M'); // Set memory limit to 512 MB (adjust as needed)

$this->name_of_certificate_holder = $name_for_certificate;
$this->event_name = $event_name;
$this->event_date = $event_date;

//if ($this->is_greek_text($this->name_of_certificate_holder)){
$random = Str::random(10);
//} else {
// $random = Str::kebab($this->name_of_certificate_holder) . "-" . Str::random(10);
//}

$this->personalized_template_name = $random.'-'.auth()->id();
$this->resource_path = resource_path().'/latex';
$this->personalized_template_name = $random . '-' . auth()->id();
$this->resource_path = resource_path() . '/latex';
$this->pdflatex = config('codeweek.pdflatex_path');
$this->id = auth()->id().'-'.$random;
Log::info('User ID '.auth()->id().' generating participation certificate with name: '.$name_for_certificate);
$this->id = auth()->id() . '-' . $random;
}

public function generate()
{

$this->customize_and_save_latex();
$this->run_pdf_creation();
$this->clean_temp_files();

return $this->personalized_template_name;
//$s3path = $this->copy_to_s3();

//return $s3path;

}

private function clean_temp_files()
{
Storage::disk('latex')->delete($this->personalized_template_name.'.aux');
Storage::disk('latex')->delete($this->personalized_template_name.'.tex');
Storage::disk('latex')->delete($this->personalized_template_name.'.log');
$extensions = ['aux', 'tex', 'log'];
foreach ($extensions as $ext) {
Storage::disk('latex')->delete($this->personalized_template_name . '.' . $ext);
}
}

protected function update_event($s3path)
Expand All @@ -86,26 +78,17 @@ protected function update_event($s3path)
]);
}

/**
* @throws \League\Flysystem\FileNotFoundException
*/
protected function copy_to_s3(): string
{
$inputStream = Storage::disk('latex')->getDriver()->readStream($this->personalized_template_name.'.pdf');
$destination = Storage::disk('s3')->path('/certificates/'.$this->id.'.pdf');
$inputStream = Storage::disk('latex')->getDriver()->readStream($this->personalized_template_name . '.pdf');
$destination = Storage::disk('s3')->path('/certificates/' . $this->id . '.pdf');
Storage::disk('s3')->put($destination, $inputStream);

return Storage::disk('s3')->url('certificates/'.$this->id.'.pdf');
return Storage::disk('s3')->url('certificates/' . $this->id . '.pdf');
}

/**
* @return mixed
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function customize_and_save_latex()
{

if ($this->is_greek_text($this->event_name) || $this->is_greek_text($this->event_date) || $this->is_greek_text($this->name_of_certificate_holder)) {
$this->templateName = 'participation_greek.tex';
}
Expand All @@ -121,11 +104,9 @@ protected function customize_and_save_latex()
if ($this->is_greek_text($this->name_of_certificate_holder)) {
$this->certificate_holder_name_lang = 'greek';
}
// Log::info($this->templateName);
//open the latex template

$base_template = Storage::disk('latex')->get($this->templateName);

//replace the text in template
$template = str_replace('<CERTIFICATE_HOLDER_NAME>', $this->tex_escape($this->name_of_certificate_holder), $base_template);
$template = str_replace('<EVENT_NAME>', $this->tex_escape($this->event_name), $template);
$template = str_replace('<EVENT_DATE>', $this->tex_escape($this->event_date), $template);
Expand All @@ -134,26 +115,34 @@ protected function customize_and_save_latex()
$template = str_replace('<EVENT_NAME_LANG>', $this->tex_escape($this->event_name_lang), $template);
$template = str_replace('<EVENT_DATE_LANG>', $this->tex_escape($this->event_date_lang), $template);

// dd($template);

//save it locally
Storage::disk('latex')->put($this->personalized_template_name.'.tex', $template);
Storage::disk('latex')->put($this->personalized_template_name . '.tex', $template);
}

protected function run_pdf_creation(): void
{
if (!file_exists($this->pdflatex)) {
throw new \RuntimeException("pdflatex binary not found at path: {$this->pdflatex}");
}

//call the pdflatex command
$command = $this->pdflatex.' -interaction=nonstopmode -output-directory '.$this->resource_path.' '.$this->resource_path.'/'.$this->personalized_template_name.'.tex';
$command = escapeshellcmd($this->pdflatex) . ' -interaction=nonstopmode -output-directory '
. escapeshellarg($this->resource_path) . ' '
. escapeshellarg($this->resource_path . '/' . $this->personalized_template_name . '.tex');

$cwd = $this->resource_path;
$process = Process::fromShellCommandline($command, $cwd);
//$process = new Process($command, $cwd);
$process->run();
$process = Process::fromShellCommandline($command, $this->resource_path);
$process->setTimeout(600); // Allow up to 600 seconds for execution
$this->execute_process($process);

// executes after the command finishes
if (! $process->isSuccessful()) {
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}

private function execute_process(Process $process): void
{
$process->start();

while ($process->isRunning()) {
usleep(400000); // Sleep for 400ms
}
}
}
9 changes: 4 additions & 5 deletions app/Traits/LatexCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public function tex_escape($string)
$map = [
'ʼ' => "'", // Replace Unicode apostrophe with standard apostrophe
'ə' => '\\textschwa{}', // Handle ə
'"' => "''", // Replace double quotes with two single quotes
'#' => '\\#',
'$' => '\$',
'$' => '\\$',
'%' => '\\%',
'&' => '\\&',
'~' => '\\~{}',
Expand All @@ -21,14 +22,12 @@ public function tex_escape($string)
'}' => '\\}',
];

$string = preg_replace_callback(
"/([\^\%~\\\\#\$%&_\{\}ʼ])/",
return preg_replace_callback(
"/([\#\$%&~_\^\\\\{}ʼ\"])/",
function ($matches) use ($map) {
return $map[$matches[0]] ?? $matches[0];
},
$string
);

return $string;
}
}