-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_pdf.php
214 lines (202 loc) · 8.32 KB
/
_pdf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
$rootDir = __DIR__;
$vendorDir = $rootDir . '/vendor/';
require_once $vendorDir . 'autoload.php';
require_once $rootDir . '/data/mock.php';
$json_file = $file = isset($_GET['file']) ? $_GET['file'] : $defaultFile;
$udata = isset($_POST['udata']) ? $_POST['udata'] : [];
$fd = $rootDir . '/data/perso/' . $json_file;
$inputData = json_decode(file_get_contents($fd), true);
$object = $inputData['objects'];
$format = $inputData['format'];
$format_name = $format['name'];
$dimension = $format['dimension'];
$pageWidth = $dimension['mm']['width'];
$pageHeight = $dimension['mm']['height'];
$rate = $dimension['px']['width'] / $dimension['mm']['width'];
$fontFamily = array();
$m = current($mock);
$attributes = [
'position' => 'absolute',
'top' => '',
'left' => '',
'width' => '',
'height' => '',
'color' => 'black',
'text-align' => 'right',
'fill-opacity' => '1',
'font-size' => '4mm',
'font-style' => 'normal',
'font-family' => '',
];
$inner = '';
foreach ($object as $o) {
$w = pixelToMm($o['width'], $rate) * $o['scaleX'];
$h = pixelToMm($o['height'], $rate) * $o['scaleY'];
$l = pixelToMm($o['bx'], $rate);
$t = pixelToMm($o['by'], $rate);
$elt_attributes = [
'top' => $t . 'mm',
'left' => $l . 'mm',
'width' => $w . 'mm',
'height' => $h . 'mm',
'rotate' => -$o['angle'],
];
switch ($o['type']) {
case 'textbox':
case 'i-text':
if (isset($o['fontFamily'])){
if ($o['fontFamily'] != 'helvetica' || $o['fontFamily'] != 'Helvetica'){
$font = $o['fontFamily'];
if ('bold' == $o['fontWeight']) {
$font .= 'b';
}
if ('italic' == $o['fontStyle']) {
$font .= 'i';
}
$fontFamily[] = $font;
} else {
$defaultFontBold = ('bold' == $o['fontWeight']) ? true : false;
$defaultFontItalic = ('italic' == $o['fontStyle']) ? true : false;
}
}
$elt_attributes = array_merge($elt_attributes, [
'color' => $o['fill'],
'text-align' => $o['textAlign'],
'font-size' => ($o['fontSize'] - 1) . 'px',
'font-family' => isset($font) ? $font : '',
'text-decoration' => $o['textDecoration'],
'line-height' => $o['lineHeight'],
'font-style' => isset($defaultFontItalic) ? ($defaultFontItalic) ? 'italic' : '' : '',
'font-weight' => isset($defaultFontBold) ? ($defaultFontBold) ? 'bold' : '' : '',
]);
$merge = array_merge($attributes, $elt_attributes);
$r = '';
foreach ($merge as $k => $v) {
$r .= $k . ':' . $v . ';';
}
if (isset($m[$o['tag']])) {
$o['text'] = $m[$o['tag']];
}
$text = nl2br($o['text']);
// $inner .= '<div style="border: solid 0.5mm red;' . $r . '">' . $o['text'] . '</div>' . PHP_EOL;
$inner .= '<div style="' . $r . '">' . $text . '</div>' . PHP_EOL;
break;
case 'image':
$merge = array_merge($attributes, $elt_attributes, [
'color' => $o['fill'],
'border-style' => 'solid', //A modifier
'border-color' => $o['stroke'],
'border-width' => $o['strokeWidth'],
]);
$r = '';
foreach ($merge as $k => $v) {
$r .= $k . ':' . $v . ';';
}
switch ($o['tag']) {
case "qrcode":
$inner .= '<qrcode style="' . $r . '" value="' . $m['barcode_id'] . '" ec="H"></qrcode>' . PHP_EOL;
break;
case "barcode":
// var_dump($r);
$inner .= '<div style="' . $r . '">' . PHP_EOL;
$inner .= '<barcode value="' . $m['barcode_id'] . '" type="EAN13"></barcode>' . PHP_EOL;
$inner .= '</div>' . PHP_EOL;
break;
default:
$inner .= '<div style="' . $r . '"><img style="width:100%;height:100%" src="' . str_replace("http://localhost:8080/", "", $o['src']) . '"/></div>' . PHP_EOL;
break;
}
break;
case 'rect':
$strokeW = pixelToMm($o['strokeWidth'], $rate) * $o['scaleX'];
$strokeH = pixelToMm($o['strokeWidth'], $rate) * $o['scaleY'];
$elt_attributes = array_merge($elt_attributes, [
'background-color' => $o['fill'],
'border-style' => 'solid',
'border-color' => $o['stroke'],
'border-width' => $strokeH .'mm '.$strokeW . 'mm',
'width' => $w - ($strokeW) .'mm',
'height' => $h - ($strokeH) .'mm',
]);
$merge = array_merge($attributes, $elt_attributes);
$r = '';
foreach ($merge as $k => $v) {
$r .= $k . ':' . $v . ';';
}
$inner .= '<div style="' . $r . '"></div>' . PHP_EOL;
break;
case 'circle':
$strokeW = pixelToMm($o['strokeWidth'], $rate) * $o['scaleX'];
$strokeH = pixelToMm($o['strokeWidth'], $rate) * $o['scaleY'];
$elt_attributes = array_merge($elt_attributes, [
'background-color' => $o['fill'],
'border-style' => 'solid',
'border-color' => $o['stroke'],
'border-width' => $strokeH .'mm '.$strokeW . 'mm',
'border-radius' => ($w+$strokeW) / 2 . 'mm / ' . ($h+$strokeH) / 2 . 'mm', //html2pdf n'accepte pas les pourcentages
'width' => $w - ($strokeW) .'mm',
'height' => $h - ($strokeH) .'mm',
]);
$merge = array_merge($attributes, $elt_attributes);
$r = '';
foreach ($merge as $k => $v) {
$r .= $k . ':' . $v . ';';
}
$inner .= '<div style="' . $r . '"></div>' . PHP_EOL;
break;
case 'line':
$strokeH = pixelToMm($o['strokeWidth'], $rate) * $o['scaleY'];
$elt_attributes = array_merge($elt_attributes, [
'background-color' => $o['stroke'],
'border-color' => $o['stroke'],
'height' => $strokeH .'mm',
]);
$merge = array_merge($attributes, $elt_attributes);
$r = '';
foreach ($merge as $k => $v) {
$r .= $k . ':' . $v . ';';
}
$inner .= '<div style="' . $r . '"></div>' . PHP_EOL;
break;
default:
break;
}
}
//echo '<pre>';
//var_dump(htmlentities($inner));
//echo '</pre>';
//exit;
$time_start = microtime(true);
if ($format_name == '8x3') {
$content = "<page orientation=paysage>";
} else {
$content = "<page>";
}
$content .= $inner;
$content .= "</page>";
$html2pdf = new HTML2PDF('P', array($pageWidth, $pageHeight), 'fr', true, 'UTF-8', [0, 0, 0, 0]);
foreach ($fontFamily as $fontName){
/*
* Si le fichier fontName.php n'est pas dans le dossier :
* - Télécharger la font nécessaire sur fonts.google.com
* - Déplacer le fichier font.ttf dans le dossier data/fonts du projet
* - Dans le terminal, depuis la racine du projet : vendor/tecnickcom/tcpdf/tools/tcpdf_addfont.php -i data/fonts/font.ttf
*/
$html2pdf->addFont($fontName,'','data/fonts/'.str_replace(" ", "", strtolower($fontName)).".php");
}
//$html2pdf->setModeDebug();
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
$time_end = microtime(true);
$execution_time = number_format(($time_end - $time_start), 2);
function pixelToMm($value, $rate = 1) {
return $value / $rate;
}
//function renderInner($s, $d = 1) {
// if ($d) {
// $s = '<div style="border: solid 1mm red">' . $s . '</div>';
// }
// return $s . PHP_EOL;
//}
echo 'In ' . $execution_time . ' s' . PHP_EOL;