vendor/sasedev/mpdf-bundle/src/Sasedev/MpdfBundle/Factory/MpdfFactory.php line 93

Open in your IDE?
  1. <?php
  2. namespace Sasedev\MpdfBundle\Factory;
  3. use Mpdf\Config\ConfigVariables;
  4. use Mpdf\Config\FontVariables;
  5. use Mpdf\Mpdf;
  6. use Mpdf\Output\Destination;
  7. use Symfony\Component\HttpFoundation\Response;
  8. /**
  9. *
  10. * Sasedev\MpdfBundle\Factory\MpdfFactory
  11. *
  12. *
  13. * @author sasedev <sinus@sasedev.net>
  14. * Created on: 1 juin 2020 @ 22:56:28
  15. */
  16. class MpdfFactory
  17. {
  18. /**
  19. *
  20. * @var string
  21. */
  22. private $cacheDir = '/tmp';
  23. /**
  24. * MpdfFactory constructor.
  25. *
  26. * @param string $cacheDir
  27. */
  28. public function __construct(string $cacheDir)
  29. {
  30. $this->cacheDir = $cacheDir;
  31. }
  32. /**
  33. *
  34. * @return array
  35. */
  36. public function getDefaultConstructorParams(): array
  37. {
  38. return [
  39. 'mode' => '',
  40. 'format' => 'A4',
  41. 'default_font_size' => 0,
  42. 'default_font' => '',
  43. 'margin_left' => 15,
  44. 'margin_right' => 15,
  45. 'margin_top' => 16,
  46. 'margin_bottom' => 16,
  47. 'margin_header' => 9,
  48. 'margin_footer' => 9,
  49. 'orientation' => 'P'
  50. ];
  51. }
  52. /**
  53. *
  54. * @return array
  55. */
  56. public function getDefaultConfigVariables(): array
  57. {
  58. $configObject = new ConfigVariables();
  59. return $configObject->getDefaults();
  60. }
  61. /**
  62. *
  63. * @return array
  64. */
  65. public function getDefaultFontVariables(): array
  66. {
  67. $fontObject = new FontVariables();
  68. return $fontObject->getDefaults();
  69. }
  70. /**
  71. * Get an instance of mPDF class
  72. *
  73. * @param array $mpdfArgs
  74. * arguments for mPDF constror
  75. * @return Mpdf
  76. */
  77. public function createMpdfObject($mpdfArgs = [])
  78. {
  79. $defaultOptions = \array_merge($this->getDefaultConstructorParams(), $this->getDefaultConfigVariables(), $this->getDefaultFontVariables(),
  80. [
  81. 'mode' => 'utf-8',
  82. 'format' => 'A4',
  83. 'tempDir' => $this->cacheDir
  84. ]);
  85. $argOptions = \array_merge($defaultOptions, $mpdfArgs);
  86. $mPdf = new Mpdf($argOptions);
  87. return $mPdf;
  88. }
  89. /**
  90. *
  91. * @param Mpdf $mPdf
  92. * @param string $filename
  93. * @param int $status
  94. * @param array $headers
  95. *
  96. * @return Response
  97. */
  98. public function createInlineResponse(Mpdf $mPdf, ?string $filename = null, ?int $status = 200, ?array $headers = [])
  99. {
  100. $content = $mPdf->Output('', Destination::STRING_RETURN);
  101. $defaultHeaders = [
  102. 'Content-Type' => 'application/pdf',
  103. 'Cache-Control' => 'public, must-revalidate, max-age=0',
  104. 'Pragma' => 'public',
  105. 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
  106. 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT'
  107. ];
  108. if (false == \is_null($filename))
  109. {
  110. $defaultHeaders['Content-disposition'] = sprintf('inline; filename="%s"', $filename);
  111. }
  112. else
  113. {
  114. $defaultHeaders['Content-disposition'] = sprintf('inline; filename="%s"', 'file.pdf');
  115. }
  116. $headers = \array_merge($defaultHeaders, $headers);
  117. return new Response($content, $status, $headers);
  118. }
  119. /**
  120. *
  121. * @param Mpdf $mPdf
  122. * @param string $filename
  123. * @param int $status
  124. * @param array $headers
  125. *
  126. * @return Response
  127. */
  128. public function createDownloadResponse(Mpdf $mPdf, string $filename, ?int $status = 200, ?array $headers = [])
  129. {
  130. $content = $mPdf->Output('', Destination::STRING_RETURN);
  131. $defaultHeaders = [
  132. 'Content-Type' => 'application/pdf',
  133. 'Content-Description' => 'File Transfer',
  134. 'Content-Transfer-Encoding' => 'binary',
  135. 'Cache-Control' => 'public, must-revalidate, max-age=0',
  136. 'Pragma' => 'public',
  137. 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
  138. 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
  139. 'Content-disposition' => sprintf('attachment; filename="%s"', $filename)
  140. ];
  141. $headers = \array_merge($headers, $defaultHeaders);
  142. $headers = \array_merge($defaultHeaders, $headers);
  143. return new Response($content, $status, $headers);
  144. }
  145. }