1: <?php
2: namespace TIC\DpdfBundle\Traits;
3:
4: use Dompdf\Dompdf;
5: use Dompdf\Css\Stylesheet;
6: use Dompdf\Frame\FrameTree;
7: use Dompdf\DOMDocument;
8: use Dompdf\DOMNode;
9:
10: /**
11: * Wrapper sur l'interface publique de la classe utilitaire Dompdf.
12: * (permet d'exposer les méthodes dans un service Symfony)
13: */
14: trait DompdfWrapper
15: {
16: protected $dompdf;
17:
18: /**
19: * Class constructor
20: *
21: * @param Options|array|null $options
22: */
23: public function construct($options = null)
24: {
25: $this->dompdf = new Dompdf($options);
26: }
27:
28:
29: public function loadDOM($doc, $quirksmode = false)
30: {
31: $this->dompdf->loadDOM($doc, $quirksmode);
32: }
33:
34: /**
35: * Loads an HTML file
36: * Parse errors are stored in the global array _dompdf_warnings.
37: *
38: * @param string $file a filename or url to load
39: * @param string $encoding Encoding of $file
40: *
41: * @throws Exception
42: */
43: public function loadHtmlFile($file, $encoding = null)
44: {
45: $this->dompdf->loadHtmlFile($file, $encoding);
46: }
47:
48: /**
49: * Loads an HTML string
50: * Parse errors are stored in the global array _dompdf_warnings.
51: *
52: * @param string $str HTML text to load
53: * @param string $encoding Encoding of $str
54: */
55: public function loadHtml($str, $encoding = null)
56: {
57: $this->dompdf->loadHtml($str, $encoding);
58: }
59:
60: /**
61: * @param DOMNode $node
62: */
63: public static function removeTextNodes(DOMNode $node)
64: {
65: $this->dompdf->removeTextNodes($node);
66: }
67:
68: /**
69: * Enable experimental caching capability
70: *
71: * @param string $cacheId
72: */
73: public function enableCaching($cacheId)
74: {
75: $this->dompdf->enableCaching($cacheId);
76: }
77:
78: /**
79: * @param string $value
80: * @return bool
81: */
82: public function parseDefaultView($value)
83: {
84: return $this->dompdf->parseDefaultView($value);
85: }
86:
87: /**
88: * Renders the HTML to PDF
89: */
90: public function render()
91: {
92: $this->dompdf->render();
93: }
94:
95: /**
96: * Add meta information to the PDF after rendering.
97: *
98: * @param string $label Label of the value (Creator, Producer, etc.)
99: * @param string $value The text to set
100: */
101: public function addInfo(string $label, string $value): void
102: {
103: $this->dompdf->addInfo($label, $value);
104: }
105:
106: /**
107: * Streams the PDF to the client.
108: *
109: * The file will open a download dialog by default. The options
110: * parameter controls the output. Accepted options (array keys) are:
111: *
112: * 'compress' = > 1 (=default) or 0:
113: * Apply content stream compression
114: *
115: * 'Attachment' => 1 (=default) or 0:
116: * Set the 'Content-Disposition:' HTTP header to 'attachment'
117: * (thereby causing the browser to open a download dialog)
118: *
119: * @param string $filename the name of the streamed file
120: * @param array $options header options (see above)
121: */
122: public function stream($filename = "document.pdf", $options = [])
123: {
124: $this->dompdf->stream($filename, $options);
125: }
126:
127: /**
128: * Returns the PDF as a string.
129: *
130: * The options parameter controls the output. Accepted options are:
131: *
132: * 'compress' = > 1 or 0 - apply content stream compression, this is
133: * on (1) by default
134: *
135: * @param array $options options (see above)
136: *
137: * @return string|null
138: */
139: public function output($options = [])
140: {
141: return $this->dompdf->output($options);
142: }
143:
144: /**
145: * Returns the underlying HTML document as a string
146: *
147: * @return string
148: */
149: public function outputHtml()
150: {
151: return $this->dompdf->outputHtml();
152: }
153:
154: /**
155: * Sets the paper size & orientation
156: *
157: * @param string|float[] $size 'letter', 'legal', 'A4', etc. {@link Dompdf\Adapter\CPDF::$PAPER_SIZES}
158: * @param string $orientation 'portrait' or 'landscape'
159: * @return $this
160: */
161: public function setPaper($size, $orientation = "portrait")
162: {
163: return $this->dompdf->setPaper($size, $orientation);
164: }
165:
166: /**
167: * Gets the paper size
168: *
169: * @return float[] A four-element float array
170: */
171: public function getPaperSize()
172: {
173: return $this->dompdf->getPaperSize();
174: }
175:
176: /**
177: * Gets the paper orientation
178: *
179: * @return string Either "portrait" or "landscape"
180: */
181: public function getPaperOrientation()
182: {
183: return $this->dompdf->getPaperOrientation();
184: }
185:
186: /**
187: * @param FrameTree $tree
188: * @return $this
189: */
190: public function setTree(FrameTree $tree)
191: {
192: return $this->dompdf->setTree($tree);
193: }
194:
195: /**
196: * Returns the underlying {@link FrameTree} object
197: *
198: * @return FrameTree
199: */
200: public function getTree()
201: {
202: return $this->dompdf->getTree();
203: }
204:
205: /**
206: * Sets the protocol to use
207: * FIXME validate these
208: *
209: * @param string $protocol
210: * @return $this
211: */
212: public function setProtocol(string $protocol)
213: {
214: return $this->dompdf->setProtocol($protocol);
215: }
216:
217: /**
218: * Returns the protocol in use
219: *
220: * @return string
221: */
222: public function getProtocol()
223: {
224: return $this->dompdf->getProtocol();
225: }
226:
227: /**
228: * Sets the base hostname
229: *
230: * @param string $baseHost
231: * @return $this
232: */
233: public function setBaseHost(string $baseHost)
234: {
235: return $this->dompdf->setBaseHost($baseHost);
236: }
237:
238: /**
239: * Returns the base hostname
240: *
241: * @return string
242: */
243: public function getBaseHost()
244: {
245: return $this->dompdf->getBaseHost();
246: }
247:
248: /**
249: * Sets the base path
250: *
251: * @param string $basePath
252: * @return $this
253: */
254: public function setBasePath(string $basePath)
255: {
256: return $this->dompdf->setBasePath($basePath);
257: }
258:
259: /**
260: * Returns the base path
261: *
262: * @return string
263: */
264: public function getBasePath()
265: {
266: return $this->dompdf->getBasePath();
267: }
268:
269: /**
270: * Sets the default view
271: *
272: * @param string $defaultView The default document view
273: * @param array $options The view's options
274: * @return $this
275: */
276: public function setDefaultView($defaultView, $options)
277: {
278: return $this->dompdf->setDefaultView($defaultView, $options);
279: }
280:
281: /**
282: * Sets the HTTP context
283: *
284: * @param resource|array $httpContext
285: * @return $this
286: */
287: public function setHttpContext($httpContext)
288: {
289: return $this->dompdf->setHttpContext($httpContext);
290: }
291:
292: /**
293: * Returns the HTTP context
294: *
295: * @return resource
296: */
297: public function getHttpContext()
298: {
299: return $this->dompdf->getHttpContext();
300: }
301:
302: /**
303: * Set a custom `Canvas` instance to render the document to.
304: *
305: * Be aware that the instance will be replaced on render if the document
306: * defines a paper size different from the canvas.
307: *
308: * @param Canvas $canvas
309: * @return $this
310: */
311: public function setCanvas(Canvas $canvas)
312: {
313: return $this->dompdf->setCanvas($canvas);
314: }
315:
316: /**
317: * Return the underlying Canvas instance (e.g. Dompdf\Adapter\CPDF, Dompdf\Adapter\GD)
318: *
319: * @return Canvas
320: */
321: public function getCanvas()
322: {
323: return $this->dompdf->getCanvas();
324: }
325:
326: /**
327: * @param Stylesheet $css
328: * @return $this
329: */
330: public function setCss(Stylesheet $css)
331: {
332: return $this->dompdf->setCss($css);
333: }
334:
335: /**
336: * Returns the stylesheet
337: *
338: * @return Stylesheet
339: */
340: public function getCss()
341: {
342: return $this->dompdf->getCss();
343: }
344:
345: /**
346: * @param DOMDocument $dom
347: * @return $this
348: */
349: public function setDom(DOMDocument $dom)
350: {
351: return $this->dompdf->setDom($dom);
352: }
353:
354: /**
355: * @return DOMDocument
356: */
357: public function getDom()
358: {
359: return $this->dompdf->getDom();
360: }
361:
362: /**
363: * @param Options $options
364: * @return $this
365: */
366: public function setOptions(Options $options)
367: {
368: return $this->dompdf->setOptions($options);
369: }
370:
371: /**
372: * @return Options
373: */
374: public function getOptions()
375: {
376: return $this->dompdf->getOptions();
377: }
378:
379: /**
380: * Returns the callbacks array
381: *
382: * @return array
383: */
384: public function getCallbacks()
385: {
386: return $this->dompdf->getCallbacks();
387: }
388:
389: /**
390: * Define callbacks that allow modifying the document during render.
391: *
392: * The callbacks array should contain arrays with `event` set to a callback
393: * event name and `f` set to a function or any other callable.
394: *
395: * The available callback events are:
396: * * `begin_page_reflow`: called before page reflow
397: * * `begin_frame`: called before a frame is rendered
398: * * `end_frame`: called after frame rendering is complete
399: * * `begin_page_render`: called before a page is rendered
400: * * `end_page_render`: called after page rendering is complete
401: * * `end_document`: called for every page after rendering is complete
402: *
403: * The function `f` receives three arguments `Frame $frame`, `Canvas $canvas`,
404: * and `FontMetrics $fontMetrics` for all events but `end_document`. For
405: * `end_document`, the function receives four arguments `int $pageNumber`,
406: * `int $pageCount`, `Canvas $canvas`, and `FontMetrics $fontMetrics` instead.
407: *
408: * @param array $callbacks The set of callbacks to set.
409: * @return $this
410: */
411: public function setCallbacks(array $callbacks): self
412: {
413: return $this->dompdf->setCallbacks($callbacks);
414: }
415:
416: /**
417: * Get the quirks mode
418: *
419: * @return boolean true if quirks mode is active
420: */
421: public function getQuirksmode()
422: {
423: return $this->dompdf->getQuirksmode();
424: }
425:
426: /**
427: * @param FontMetrics $fontMetrics
428: * @return $this
429: */
430: public function setFontMetrics(FontMetrics $fontMetrics)
431: {
432: return $this->dompdf->setFontMetrics($fontMetrics);
433: }
434:
435: /**
436: * @return FontMetrics
437: */
438: public function getFontMetrics()
439: {
440: return $this->dompdf->getFontMetrics();
441: }
442:
443: }
444: