src/JuridicusBundle/Entity/Pdf.php line 15

Open in your IDE?
  1. <?php
  2. namespace JF\JuridicusBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  9. /**
  10. * Pdf
  11. */
  12. class Pdf extends InitModeEntity
  13. {
  14. const CLASSNAME = __CLASS__;
  15. /**
  16. * @var integer
  17. */
  18. protected $id;
  19. /**
  20. * @var UploadedFile
  21. * @Assert\File(maxSize="6000000")
  22. */
  23. protected $file;
  24. /**
  25. * @var string
  26. */
  27. protected $filename;
  28. /**
  29. * @var integer
  30. */
  31. protected $anzahl_seiten;
  32. /**
  33. * @var string
  34. */
  35. protected $path;
  36. /**
  37. * @var string
  38. */
  39. protected $mimeType = 'application/pdf'; // Standardwert
  40. /**
  41. * @var float
  42. */
  43. protected $size;
  44. /**
  45. * Get id
  46. *
  47. * @return integer
  48. */
  49. public function getId()
  50. {
  51. return $this->id;
  52. }
  53. /**
  54. * Get file
  55. *
  56. * @return UploadedFile
  57. */
  58. public function getFile()
  59. {
  60. return $this->file;
  61. }
  62. /**
  63. * Set file
  64. *
  65. * @return Pdf
  66. */
  67. public function setFile($file)
  68. {
  69. $this->file = $file;
  70. return $this;
  71. }
  72. /**
  73. *
  74. * @return string
  75. */
  76. public function getFilename()
  77. {
  78. if (!$this->filename) {
  79. $file = new File($this->getPath());
  80. $this->filename = $file->getFileName();
  81. }
  82. return $this->filename;
  83. }
  84. /**
  85. * Set anzahl_seiten
  86. *
  87. * @param integer $anzahlSeiten
  88. * @return Pdf
  89. */
  90. public function setAnzahlSeiten($anzahlSeiten)
  91. {
  92. $this->anzahl_seiten = $anzahlSeiten;
  93. return $this;
  94. }
  95. /**
  96. * Get anzahl_seiten
  97. *
  98. * @return integer
  99. */
  100. public function getAnzahlSeiten()
  101. {
  102. return $this->anzahl_seiten;
  103. }
  104. /**
  105. * Set path
  106. *
  107. * @param string $path
  108. * @return Pdf
  109. */
  110. public function setPath($path)
  111. {
  112. $this->path = $path;
  113. return $this;
  114. }
  115. /**
  116. * Get path
  117. *
  118. * @return string
  119. */
  120. public function getPath()
  121. {
  122. $this->path = str_replace('/data/webserv/sandbox_2/', __DIR__."/../../../../", $this->path);
  123. $this->path = str_replace('/home/juridicus/', __DIR__."/../../../../".'app/files/', $this->path);
  124. return $this->path; //__DIR__."/../../../../".
  125. }
  126. /**
  127. * Set mimeType
  128. *
  129. * @param string $mimeType
  130. * @return Pdf
  131. */
  132. public function setMimeType($mimeType)
  133. {
  134. $this->mimeType = $mimeType;
  135. return $this;
  136. }
  137. /**
  138. * Get mimeType
  139. *
  140. * @return string
  141. */
  142. public function getMimeType()
  143. {
  144. return $this->mimeType;
  145. }
  146. /**
  147. * Set size
  148. *
  149. * @param float $size
  150. * @return Pdf
  151. */
  152. public function setSize($size)
  153. {
  154. $this->size = $size;
  155. return $this;
  156. }
  157. /**
  158. * Get size
  159. *
  160. * @return float
  161. */
  162. public function getSize()
  163. {
  164. return $this->size;
  165. }
  166. /**
  167. * Get dir
  168. *
  169. * @return string
  170. */
  171. public function getDir()
  172. {
  173. return realpath(__DIR__ . '/../../../../app/files/pdf');
  174. }
  175. /**
  176. *
  177. * @return string
  178. */
  179. public function getContent()
  180. {
  181. return file_get_contents($this->getPath());
  182. }
  183. /**
  184. * createResponse - creates Response with Headers set for Download
  185. *
  186. * @param bool $inline
  187. * @return Symfony\Component\HttpFoundation\Response
  188. */
  189. public function createResponse($inline = false)
  190. {
  191. $response = new Response();
  192. $disposition = $response->headers->makeDisposition(
  193. $inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  194. $this->getFilename().".pdf",
  195. iconv('utf-8', 'us-ascii//TRANSLIT', $this->getFilename())
  196. );
  197. $response->headers->set('Content-Disposition', $disposition);
  198. $response->headers->set('Content-Type', $this->getMimeType());
  199. $response->headers->set('Content-Transfer-Encoding', 'binary');
  200. $response->headers->set('Content-Length', filesize($this->getPath()));
  201. $response->setContent($this->getContent());
  202. return $response;
  203. }
  204. /**
  205. * createAttachment - creates Swift_Attachment for SwiftMailer
  206. *
  207. * @return Swift_Attachment
  208. */
  209. public function createAttachment()
  210. {
  211. return \Swift_Attachment::newInstance()
  212. ->setFilename($this->getFilename())
  213. ->setContentType($this->getMimeType())
  214. ->setBody(file_get_contents($this->getPath()))
  215. ;
  216. }
  217. }