QR_code_generator/app/Services/Documents/GenerateWordDocument.php

52 lines
1.5 KiB
PHP

<?php
namespace App\Services\Documents;
use Exception;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
class GenerateWordDocument extends DocumentGeneratorService {
public array $options = [
'marginTop' => 300,
'marginLeft' => 600,
'marginRight' => 600,
'colsNum' => 1,
'pageNumberingStart' => 1,
'orientation' => 'portrait'
];
public string $fontName = 'Times New Roman';
public int $fontSize = 10;
public function __construct(string $template, array $data, array $options = []) {
$template = "documents.word.{$template}";
parent::__construct($template, $data, $options);
}
public function file(): string {
$template = view($this->template, $this->data);
$phpWord = new PhpWord();
$phpWord->setDefaultFontName($this->fontName);
$phpWord->setDefaultFontSize($this->fontSize);
$section = $phpWord->addSection($this->options);
Html::addHtml($section, $template->render(), false, false);
try {
$filePath = $this->makeFilePath('docx');
$objWriter = IOFactory::createWriter($phpWord);
$objWriter->save(Storage::path($filePath));
return $filePath;
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
public function asset(string $name = null) {
return $this->makeAsset($this->file(), $name);
}
}