first commit
This commit is contained in:
68
app/Models/DocumentTemplate.php
Normal file
68
app/Models/DocumentTemplate.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\DocxTemplateProcessor;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class DocumentTemplate extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'content',
|
||||
'variables',
|
||||
'source_path'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'variables' => 'array'
|
||||
];
|
||||
|
||||
/**
|
||||
* Подстановка значений в DOCX шаблон
|
||||
*/
|
||||
public function generateDocument($data)
|
||||
{
|
||||
$tempDir = storage_path('app/temp/' . uniqid());
|
||||
mkdir($tempDir, 0755, true);
|
||||
|
||||
// Копируем исходный шаблон
|
||||
$templatePath = $tempDir . '/template.docx';
|
||||
copy($this->source_path, $templatePath);
|
||||
|
||||
$docx = new DocxTemplateProcessor();
|
||||
|
||||
// Подставляем значения
|
||||
$changedDocxPath = $docx->processWithPhpWord($templatePath, $data);
|
||||
|
||||
// Возвращаем путь к сгенерированному файлу
|
||||
return $changedDocxPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Конвертация в PDF для предпросмотра
|
||||
*/
|
||||
public function convertToPdf($docxPath)
|
||||
{
|
||||
$pdfPath = str_replace('.docx', '.pdf', $docxPath);
|
||||
|
||||
// Надо добавить....
|
||||
$home = config('libreoffice.home');
|
||||
$user = config('libreoffice.user');
|
||||
putenv("HOME=$home");
|
||||
putenv("USER=$user");
|
||||
$command = "libreoffice --headless --convert-to pdf --outdir " .
|
||||
escapeshellarg(dirname($pdfPath)) . " " .
|
||||
escapeshellarg($docxPath);
|
||||
|
||||
$result = shell_exec($command . " 2>&1");
|
||||
|
||||
if (!file_exists($pdfPath)) {
|
||||
throw new \Exception("PDF conversion failed: " . $result);
|
||||
}
|
||||
|
||||
return $pdfPath;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user