3057ec13 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | |
5 | | | |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | |
8 | | and copyright information, see https://civicrm.org/licensing | |
9 | +--------------------------------------------------------------------+ |
10 | */ |
11 | |
12 | use Civi\Report\OutputHandlerInterface; |
13 | use Civi\Report\OutputHandlerBase; |
14 | |
15 | /** |
16 | * PDF Report Output Handler |
17 | */ |
18 | class CRM_Report_OutputHandler_Pdf extends OutputHandlerBase implements OutputHandlerInterface { |
19 | |
20 | /** |
21 | * Are we a suitable output handler based on the given form? |
22 | * |
23 | * The class member $form isn't set yet at this point since we don't |
24 | * even know if we're in play yet, so the form is a parameter. |
25 | * |
26 | * @param CRM_Report_Form $form |
27 | * |
28 | * @return bool |
29 | */ |
30 | public function isOutputHandlerFor(CRM_Report_Form $form):bool { |
31 | return ($form->getOutputMode() === 'pdf'); |
32 | } |
33 | |
34 | /** |
35 | * Return the download filename. This should be the "clean" name, not |
36 | * a munged temporary filename. |
37 | * |
38 | * @return string |
39 | */ |
40 | public function getFileName():string { |
41 | return 'CiviReport.pdf'; |
42 | } |
43 | |
44 | /** |
45 | * Return the html body of the email. |
46 | * |
47 | * @return string |
48 | */ |
49 | public function getMailBody():string { |
50 | // @todo It would be nice if this was more end-user configurable, but |
51 | // keeping it the same as it was before for now. |
52 | $url = CRM_Utils_System::url('civicrm/report/instance/' . $this->getForm()->getID(), 'reset=1', TRUE); |
53 | return $this->getForm()->getReportHeader() . '<p>' . ts('Report URL') . |
54 | ": {$url}</p>" . '<p>' . |
55 | ts('The report is attached as a PDF file.') . '</p>' . |
56 | $this->getForm()->getReportFooter(); |
57 | } |
58 | |
59 | /** |
60 | * Return the report contents as a string, in this case the pdf file. |
61 | * |
62 | * @return string |
63 | */ |
64 | public function getOutputString():string { |
65 | return CRM_Utils_PDF_Utils::html2pdf( |
66 | $this->getForm()->compileContent(), |
67 | $this->getFileName(), |
68 | TRUE, |
69 | ['orientation' => 'landscape'] |
70 | ); |
71 | } |
72 | |
73 | /** |
74 | * Set headers as appropriate and send the output to the browser. |
75 | */ |
76 | public function download() { |
77 | // Nb. Once upon a time we used a package called Open Flash Charts to |
78 | // draw charts, and we had a feature whereby a browser could send the |
79 | // server a PNG version of the chart, which could then be included in a |
80 | // PDF by including <img> tags in the HTML for the conversion below. |
81 | // |
82 | // This feature stopped working when browsers stopped supporting Flash, |
83 | // and although we have a different client-side charting library in |
84 | // place, we decided not to reimplement the (rather convoluted) |
85 | // browser-sending-rendered-chart-to-server process. |
86 | // |
87 | // If this feature is required in future we should find a better way to |
88 | // render charts on the server side, e.g. server-created SVG. |
89 | |
90 | CRM_Utils_PDF_Utils::html2pdf( |
91 | $this->getForm()->compileContent(), |
92 | $this->getFileName(), |
93 | FALSE, |
94 | ['orientation' => 'landscape'] |
95 | ); |
96 | } |
97 | |
98 | /** |
99 | * Mime type of the attachment. |
100 | * |
101 | * @return string |
102 | */ |
103 | public function getMimeType():string { |
104 | return 'application/pdf'; |
105 | } |
106 | |
107 | } |