Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
18 / 18 |
| PdfGenerator | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
18 / 18 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| createFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| makeDirectory | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
|||
| setOptions | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\FileGenerator\Services\FileGenerator\Generators; |
| 4 | |
| 5 | use Dompdf\Dompdf; |
| 6 | use Qmp\Laravel\FileGenerator\Config; |
| 7 | |
| 8 | class PdfGenerator extends AbstractGenerator |
| 9 | { |
| 10 | /** |
| 11 | * @var array |
| 12 | */ |
| 13 | protected array $options = [ |
| 14 | 'defaultPaperSize' => 'A4', |
| 15 | 'fontDir' => '/var/common/app/public/fonts/dompdf', |
| 16 | 'fontCache' => '/var/common/app/private/temp/dompdf', |
| 17 | 'isRemoteEnabled' => true, |
| 18 | 'isFontSubsettingEnabled' => true |
| 19 | ]; |
| 20 | |
| 21 | /** |
| 22 | * @var \Dompdf\Dompdf |
| 23 | */ |
| 24 | protected Dompdf $dompdf; |
| 25 | |
| 26 | /** |
| 27 | * @var array|string[] |
| 28 | */ |
| 29 | protected array $rules = [ |
| 30 | 'type' => 'required|in:pdf', |
| 31 | 'content' => 'required|string', |
| 32 | 'options' => 'present|array' |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * @param \Qmp\Laravel\FileGenerator\Config $config |
| 37 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
| 38 | */ |
| 39 | public function __construct(Config $config) |
| 40 | { |
| 41 | parent::__construct($config); |
| 42 | |
| 43 | $this->dompdf = new Dompdf(); |
| 44 | $this->setOptions(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | */ |
| 50 | public function createFile(): void |
| 51 | { |
| 52 | $this->dompdf->loadHTML($this->config->content); |
| 53 | $this->dompdf->render(); |
| 54 | file_put_contents($this->config->output_path, $this->dompdf->output()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * |
| 59 | */ |
| 60 | protected function makeDirectory() |
| 61 | { |
| 62 | if (!is_dir($this->dompdf->getOptions()->get('fontDir'))) { |
| 63 | mkdir($this->dompdf->getOptions()->get('fontDir'), 0755, true); |
| 64 | } |
| 65 | |
| 66 | if (!is_dir($this->dompdf->getOptions()->get('fontCache'))) { |
| 67 | mkdir($this->dompdf->getOptions()->get('fontCache'), 0755, true); |
| 68 | } |
| 69 | |
| 70 | parent::makeDirectory(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * |
| 75 | */ |
| 76 | protected function setOptions() |
| 77 | { |
| 78 | $options = $this->dompdf->getOptions(); |
| 79 | $options->set($this->getOptions()); |
| 80 | $this->dompdf->setOptions($options); |
| 81 | } |
| 82 | } |