first commit

This commit is contained in:
brusnitsyn
2025-10-31 16:48:05 +09:00
commit 8b650558e2
143 changed files with 24664 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Http\Controllers;
use App\Models\DocumentTemplate;
use App\Services\DocxParser;
use App\Services\PageBreaker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Inertia\Inertia;
use PhpOffice\PhpWord\IOFactory;
class DocumentController extends Controller
{
public function show($id, Request $request)
{
$template = DocumentTemplate::find($id);
return Inertia::render('ContractGenerator', [
'template' => $template
]);
}
/**
* Предпросмотр документа с подставленными значениями
*/
public function preview(Request $request, $id)
{
$request->validate([
'variables' => 'nullable|array'
]);
try {
$template = DocumentTemplate::findOrFail($id);
// Генерируем DOCX с подставленными значениями
$docxPath = $template->generateDocument($request->variables);
// Конвертируем в PDF
$pdfPath = $template->convertToPdf($docxPath);
// Регистрируем функцию для удаления после завершения
register_shutdown_function(function () use ($docxPath, $pdfPath) {
File::delete($docxPath);
File::delete($pdfPath);
});
// Отдаем PDF
return response()->file($pdfPath, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="preview.pdf"'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 500);
}
}
/**
* Скачивание готового документа
*/
public function download(Request $request, $id)
{
$request->validate([
'variables' => 'nullable|array'
]);
try {
$template = DocumentTemplate::findOrFail($id);
$docxPath = $template->generateDocument($request->variables);
return response()->download($docxPath,
$template->name . '.docx',
[
'Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition' => 'attachment; filename="' . $template->name . '.docx"'
]
);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 500);
}
}
}