1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Fi\CoreBundle\Utils\GrigliaFiltriUtils; |
6
|
|
|
use TCPDF; |
7
|
|
|
|
8
|
|
|
class EsportaTabellaPdf |
9
|
|
|
{ |
10
|
|
|
public function stampa($parametri = array()) |
11
|
|
|
{ |
12
|
|
|
$testata = $parametri['testata']; |
|
|
|
|
13
|
|
|
$request = $parametri['request']; |
|
|
|
|
14
|
|
|
$nometabella = $request->get('nometabella'); |
15
|
|
|
|
16
|
|
|
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
17
|
|
|
|
18
|
|
|
//echo PDF_HEADER_LOGO; |
19
|
|
|
$pdftitle = isset($testata['titolo']) && ($testata['titolo'] != '') ? $testata['titolo'] : 'Elenco ' . $nometabella; |
20
|
|
|
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'FiFree2', $pdftitle, array(0, 0, 0), array(0, 0, 0)); |
21
|
|
|
$pdf->setFooterData(array(0, 0, 0), array(0, 0, 0)); |
22
|
|
|
|
23
|
|
|
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); |
24
|
|
|
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); |
25
|
|
|
|
26
|
|
|
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
27
|
|
|
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
28
|
|
|
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
29
|
|
|
$pdf->SetFillColor(220, 220, 220); |
30
|
|
|
|
31
|
|
|
$pdf->AddPage('L'); |
32
|
|
|
$arraystampaparm = array( |
33
|
|
|
"larghezzaform" => 900, |
|
|
|
|
34
|
|
|
"h" => 6, |
|
|
|
|
35
|
|
|
"border" => 1, |
|
|
|
|
36
|
|
|
"align" => 'L', |
|
|
|
|
37
|
|
|
"fill" => 0, |
|
|
|
|
38
|
|
|
"ln" => 0 |
|
|
|
|
39
|
|
|
); |
40
|
|
|
$parametristampa = array_merge($parametri, $arraystampaparm); |
41
|
|
|
|
42
|
|
|
$this->stampaTestata($pdf, $parametristampa); |
43
|
|
|
|
44
|
|
|
$this->stampaDettaglio($pdf, $parametristampa); |
45
|
|
|
|
46
|
|
|
/* |
47
|
|
|
I: send the file inline to the browser (default). The plug-in is used if available. |
48
|
|
|
The name given by name is used when one selects the “Save as” option on the link generating the PDF. |
49
|
|
|
D: send to the browser and force a file download with the name given by name. |
50
|
|
|
F: save to a local server file with the name given by name. |
51
|
|
|
S: return the document as a string (name is ignored). |
52
|
|
|
FI: equivalent to F + I option |
53
|
|
|
FD: equivalent to F + D option |
54
|
|
|
E: return the document as base64 mime multi-part email attachment (RFC 2045) |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
/* In caso il pdf stampato nel browser resti fisso a caricare la pagina, |
58
|
|
|
impostare 'D' per forzare lo scarico del file, oppure |
59
|
|
|
mettere exit al posto di return 0; questo opzione però non è accettata da gli strumenti di controllo del codice che non si |
60
|
|
|
aspettano exit nel codice |
61
|
|
|
*/ |
62
|
|
|
$pdf->Output($request->get('nometabella') . '.pdf', 'I'); |
63
|
|
|
|
64
|
|
|
return 0; |
65
|
|
|
} |
66
|
|
|
private function stampaTestata($pdf, $parametri) |
67
|
|
|
{ |
68
|
|
|
$ln = $parametri['ln']; |
|
|
|
|
69
|
|
|
$fill = $parametri['fill']; |
|
|
|
|
70
|
|
|
$align = $parametri['align']; |
|
|
|
|
71
|
|
|
$border = $parametri['border']; |
|
|
|
|
72
|
|
|
$h = $parametri['h']; |
|
|
|
|
73
|
|
|
$larghezzaform = $parametri['larghezzaform']; |
|
|
|
|
74
|
|
|
$testata = $parametri['testata']; |
|
|
|
|
75
|
|
|
$nomicolonne = $testata['nomicolonne']; |
|
|
|
|
76
|
|
|
$modellicolonne = $testata['modellocolonne']; |
77
|
|
|
|
78
|
|
|
// Testata |
79
|
|
|
$pdf->SetFont('helvetica', 'B', 9); |
80
|
|
|
$arr_heights = array(); |
81
|
|
|
// store current object |
82
|
|
|
$pdf->startTransaction(); |
83
|
|
|
foreach ($nomicolonne as $posizione => $nomecolonna) { |
84
|
|
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
85
|
|
|
// get the number of lines |
86
|
|
|
$arr_heights[] = $pdf->MultiCell($width, 0, $nomecolonna, $border, $align, $fill, 0, '', '', true, 0, false, true, 0); |
87
|
|
|
} |
88
|
|
|
// restore previous object |
89
|
|
|
$pdf->rollbackTransaction(true); |
90
|
|
|
//work out the number of lines required |
91
|
|
|
$rowcount = max($arr_heights); |
92
|
|
|
//now draw it |
93
|
|
|
foreach ($nomicolonne as $posizione => $nomecolonna) { |
94
|
|
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
95
|
|
|
$pdf->MultiCell($width, $rowcount * $h, $nomecolonna, $border, $align, $fill, $ln); |
96
|
|
|
} |
97
|
|
|
$pdf->SetFont('helvetica', '', 9); |
98
|
|
|
$pdf->Ln(); |
99
|
|
|
} |
100
|
|
|
private function stampaDettaglio($pdf, $parametri) |
101
|
|
|
{ |
102
|
|
|
|
103
|
|
|
$ln = $parametri['ln']; |
|
|
|
|
104
|
|
|
$fill = $parametri['fill']; |
|
|
|
|
105
|
|
|
$align = $parametri['align']; |
|
|
|
|
106
|
|
|
$border = $parametri['border']; |
|
|
|
|
107
|
|
|
$h = $parametri['h']; |
|
|
|
|
108
|
|
|
$larghezzaform = $parametri['larghezzaform']; |
|
|
|
|
109
|
|
|
$testata = $parametri['testata']; |
|
|
|
|
110
|
|
|
$modellicolonne = $testata['modellocolonne']; |
111
|
|
|
|
112
|
|
|
$rispostaj = $parametri['griglia']; |
113
|
|
|
// Dati |
114
|
|
|
$risposta = json_decode($rispostaj); |
|
|
|
|
115
|
|
|
$dimensions = $pdf->getPageDimensions(); |
116
|
|
|
$righe = $risposta->rows; |
|
|
|
|
117
|
|
|
$pdf->SetFont('helvetica', '', 9); |
118
|
|
|
foreach ($righe as $riga) { |
119
|
|
|
$fill = !$fill; |
|
|
|
|
120
|
|
|
$vettorecelle = $riga->cell; |
121
|
|
|
|
122
|
|
|
$arr_heights = array(); |
123
|
|
|
// store current object |
124
|
|
|
$pdf->startTransaction(); |
125
|
|
|
foreach ($vettorecelle as $posizione => $valore) { |
126
|
|
|
if (!is_object($valore)) { |
127
|
|
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
128
|
|
|
// get the number of lines |
129
|
|
|
$arr_heights[] = $pdf->MultiCell($width, 0, $valore, $border, $align, $fill, 0, '', '', true, 0, false, true, 0); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
// restore previous object |
133
|
|
|
$pdf->rollbackTransaction(true); |
134
|
|
|
//work out the number of lines required |
135
|
|
|
$rowcount = max($arr_heights); |
136
|
|
|
$startY = $pdf->GetY(); |
|
|
|
|
137
|
|
|
if (($startY + $rowcount * $h) + $dimensions['bm'] > ($dimensions['hk'])) { |
138
|
|
|
// page break |
139
|
|
|
$pdf->AddPage('L'); |
140
|
|
|
// stampa testata |
141
|
|
|
$this->stampaTestata($pdf, $parametri); |
142
|
|
|
} |
143
|
|
|
//now draw it |
144
|
|
|
foreach ($vettorecelle as $posizione => $valore) { |
145
|
|
|
if (!is_object($valore)) { |
146
|
|
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
147
|
|
|
$pdf->MultiCell($width, $rowcount * $h, $valore, $border, $align, $fill, $ln); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
$pdf->Ln(); |
151
|
|
|
} |
152
|
|
|
$pdf->Cell(0, 10, GrigliaFiltriUtils::traduciFiltri(array('filtri' => $risposta->filtri)), 0, false, 'L', 0, '', 0, false, 'T', 'M'); |
153
|
|
|
} |
154
|
|
|
private function getWidthColumn($modellicolonne, $posizione, $larghezzaform) |
155
|
|
|
{ |
156
|
|
|
if (isset($modellicolonne[$posizione])) { |
157
|
|
|
$width = ((297 * $modellicolonne[$posizione]['width']) / $larghezzaform) / 2; |
158
|
|
|
} else { |
159
|
|
|
$width = ((297 * 100) / $larghezzaform) / 2; |
160
|
|
|
} |
161
|
|
|
return $width; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.