1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
pdf.php
См. документацию.
1<?
2
3define('FPDF_FONTPATH', $_SERVER["DOCUMENT_ROOT"]."/bitrix/fonts/");
4
5require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/sale/general/tfpdf/tfpdf.php");
6
7class CSaleTfpdf extends tFPDF
8{
9
10 private $background;
11
12 public function SetBackground($image, $bgHeight = 0, $bgWidth = 0, $style = 'none')
13 {
14 if (!in_array($style, array('none', 'tile', 'stretch')))
15 $style = 'none';
16
17 if ($image && $bgHeight && $bgWidth)
18 {
19 $this->background = array(
20 'image' => $image,
21 'height' => $bgHeight,
22 'width' => $bgWidth,
23 'style' => $style
24 );
25 }
26 }
27
28 public function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '')
29 {
30 try
31 {
32 parent::Image($file, $x, $y, $w, $h, $type, $link);
33 }
34 catch (Exception $e)
35 {
36 }
37 }
38
39 public function Header()
40 {
41 if (!empty($this->background))
42 {
43 switch ($this->background['style'])
44 {
45 case 'none':
46 $this->Image($this->background['image'], 0, 0);
47 break;
48 case 'tile':
49 $y = 0;
50 while ($y <= $this->GetPageHeight())
51 {
52 $x = 0;
53 while ($x <= $this->GetPageWidth())
54 {
55 $this->Image($this->background['image'], $x, $y);
56 $x += $this->background['width'];
57 }
58
59 $y += $this->background['height'];
60 }
61 break;
62 case 'stretch':
63 $this->Image(
64 $this->background['image'],
65 0, 0,
66 $this->GetPageWidth(), $this->GetPageHeight()
67 );
68 break;
69 }
70 }
71 }
72
73 public function GetPageWidth()
74 {
75 return $this->w;
76 }
77
78 public function GetPageHeight()
79 {
80 return $this->h;
81 }
82
83 public function Output($name = '', $dest = '', $utfName = '')
84 {
85 // invalid symbols: "%*:<>?| and \x00-\x1F\x7F and \x80-\xFF
86 $name = preg_replace('/[\x00-\x1F\x22\x25\x2A\x3A\x3C\x3E\x3F\x7C\x7F-\xFF]+/', '', $name);
87 $name = str_replace('\\', '/', $name);
88
89 if (in_array($dest, array('I', 'D')))
90 $name = basename($name);
91
92 return parent::Output($name, $dest, $utfName);
93 }
94
95 function _parsebmp($file)
96 {
97 // Extract info from a BMP file (via PNG conversion)
98 if(!function_exists('imagepng'))
99 $this->Error('GD extension is required for BMP support');
100 $im = CFile::ImageCreateFromBMP($file);
101 if(!$im)
102 $this->Error('Missing or incorrect image file: '.$file);
103 imageinterlace($im,0);
104 $f = @fopen('php://temp','rb+');
105 if($f)
106 {
107 // Perform conversion in memory
108 ob_start();
109 imagepng($im);
110 $data = ob_get_clean();
111 imagedestroy($im);
112 fwrite($f,$data);
113 rewind($f);
114 $info = $this->_parsepngstream($f,$file);
115 fclose($f);
116 }
117 else
118 {
119 // Use temporary file
120 $tmp = tempnam('.','gif');
121 if(!$tmp)
122 $this->Error('Unable to create a temporary file');
123 if(!imagepng($im,$tmp))
124 $this->Error('Error while saving to temporary file');
125 imagedestroy($im);
126 $info = $this->_parsepng($tmp);
127 unlink($tmp);
128 }
129 return $info;
130 }
131
132 public function calculateRowsWidth($cols, $cells, $countItems, $docWidth, $margin = null)
133 {
134 $eps = 1E-05;
137
138 if ($margin === null || $margin < 0)
139 {
140 $margin = 5;
141 }
142 else
143 {
144 $margin = (int)$margin;
145 }
146
147 // last columns always digital
148 end($cols);
149 $lastColumn = key($cols);
150 $cols[$lastColumn]['IS_DIGIT'] = true;
151
152 $digitColumns = [];
153 $digitWidth = 0;
154 $digitColumnFullWidth = [];
155 foreach ($cols as $columnId => $column)
156 {
157 $max = $this->GetStringWidth($this->getMaximumWord($column['NAME']) . " ");
158 foreach ($cells as $i => $cell)
159 {
160 $maxLengthWord = $cell[$columnId];
161 if ($cols[$columnId]['IS_DIGIT'] !== true)
162 {
163 $maxLengthWord = $this->getMaximumWord($cell[$columnId]) . " ";
164 }
165
166 if ($i <= $countItems || $lastColumn === $columnId)
167 {
168 $max = max($max, $this->GetStringWidth($maxLengthWord));
169 }
170 }
171
172 $arRowsWidth[$columnId] = $max + $margin * 2;
173 $arRowsContentWidth[$columnId] = $max;
174
175 if ($cols[$columnId]['IS_DIGIT'] === true)
176 {
177 $digitWidth += $arRowsWidth[$columnId];
178 $digitColumns[] = $columnId;
179 $columnWith = $this->GetStringWidth($column['NAME']);
180 $digitColumnFullWidth[$columnId] = ($columnWith > $arRowsWidth[$columnId]) ? $columnWith : $arRowsWidth[$columnId];
181 }
182 }
183
184 $noDigitWidth = array_sum($arRowsWidth) - $digitWidth;
185 $requiredWidth = $docWidth - $digitWidth;
186 if ($noDigitWidth - $requiredWidth > $eps)
187 {
188 $colNameTitle = $this->GetStringWidth($cols['NAME']['NAME']);
189 if ($colNameTitle < $requiredWidth)
190 {
191 $arRowsWidth['NAME'] = $requiredWidth;
192 $arRowsContentWidth['NAME'] = $requiredWidth - $margin * 2;
193 }
194
195 $noDigitWidth = array_sum($arRowsWidth) - $digitWidth;
196 if ($noDigitWidth - $requiredWidth > $eps)
197 {
198 if (!in_array($lastColumn, $digitColumns))
199 {
200 $digitColumns[] = $lastColumn;
201 }
202
203 $digitWidth = 0;
204 foreach ($digitColumns as $columnId)
205 {
206 if (!isset($cols[$columnId]))
207 continue;
208
209 $max = $this->GetStringWidth($this->getMaximumWord($cols[$columnId]['NAME']) . " ");
210 foreach ($cells as $i => $cell)
211 {
212 if ($i <= $countItems || $lastColumn === $columnId)
213 {
214 $max = max($max, $this->GetStringWidth($cell[$columnId]));
215 }
216 }
217
218 $arRowsWidth[$columnId] = $max + $margin * 2;
219 $arRowsContentWidth[$columnId] = $max;
220 $digitWidth += $arRowsWidth[$columnId];
221 }
222
223 $requiredWidth = $docWidth - $digitWidth;
224 }
225 }
226
227 $additionalWidth = $requiredWidth / count($digitColumns);
228 reset($cols);
229 $firstColumnKey = key($cols);
230 $digitWidth = 0;
231 $onlyDigit = true;
232 foreach ($arRowsWidth as $columnId => $rowWidth)
233 {
234 if ($columnId === $firstColumnKey
235 && $cols[$columnId]['IS_DIGIT']
236 )
237 {
238 $digitWidth += $arRowsWidth[$columnId];
239 continue;
240 }
241
242 if (isset($digitColumnFullWidth[$columnId]))
243 {
244 $width = $arRowsWidth[$columnId] + $additionalWidth;
245 if ($width > ($digitColumnFullWidth[$columnId] + $margin * 2))
246 {
247 $arRowsWidth[$columnId] = $digitColumnFullWidth[$columnId] + $margin * 2;
248 }
249 else
250 {
251 $arRowsWidth[$columnId] = $width + $margin * 2;
252 }
253 }
254
255 if ($cols[$columnId]['IS_DIGIT'] === true)
256 {
257 $digitWidth += $arRowsWidth[$columnId];
258 }
259 else
260 {
261 $onlyDigit = false;
262 }
263 }
264
265 $requiredWidth = $docWidth - $digitWidth;
266 if ($requiredWidth > 0)
267 {
268 foreach ($arRowsWidth as $columnId => $rowWidth)
269 {
270 if ($onlyDigit)
271 {
272 $arRowsWidth[$columnId] += $requiredWidth / count($digitColumns);
273 $arRowsContentWidth[$columnId] += $requiredWidth / count($digitColumns);
274 }
275 elseif ($cols[$columnId]['IS_DIGIT'] !== true)
276 {
277 $ratio = $requiredWidth / $noDigitWidth;
278 $arRowsWidth[$columnId] *= $ratio;
279 $arRowsContentWidth[$columnId] *= $ratio;
280 }
281 }
282 }
283
284 return array(
285 'ROWS_WIDTH' => $arRowsWidth,
286 'ROWS_CONTENT_WIDTH' => $arRowsContentWidth
287 );
288 }
289
294 protected function getMaximumWord($str)
295 {
296 $wordList = explode(" ", $str);
297 $maxWord = "";
298
299 foreach ($wordList as $word)
300 {
301 if (mb_strlen($word, 'UTF-8') > mb_strlen($maxWord, 'UTF-8'))
302 {
303 $maxWord = $word;
304 }
305 }
306
307 return $maxWord;
308 }
309}
310
312{
313
314 protected $generator;
315
316 public static function isPdfAvailable()
317 {
318 if (!file_exists(FPDF_FONTPATH.'/pt_serif-regular.ttf') || !file_exists(FPDF_FONTPATH.'/pt_serif-bold.ttf'))
319 return false;
320
321 return true;
322 }
323
324 public static function prepareToPdf($string)
325 {
326 $string = htmlspecialcharsback($string);
327 $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
328
329 return $string;
330 }
331
332 public function splitString($text, $width)
333 {
334 if ($width <= 0 || $this->generator->GetStringWidth($text) <= $width)
335 {
336 return array($text, '');
337 }
338 else
339 {
340 $string = $text;
341 while ($this->generator->GetStringWidth($string) > $width)
342 {
343 $l = floor(mb_strlen($string, 'UTF-8') * $width/$this->generator->GetStringWidth($string));
344 $p = mb_strrpos($string, ' ', $l-mb_strlen($string, 'UTF-8'), 'UTF-8') ?: $l;
345
346 $string = mb_substr($string, 0, $p, 'UTF-8');
347 }
348
349 if ($p == 0)
350 $p++;
351
352 return array(
353 $string,
354 trim(mb_substr($text, $p, mb_strlen($text, 'UTF-8'), 'UTF-8'))
355 );
356 }
357 }
358
359 public function __construct($orientation = 'P', $unit = 'mm', $size = 'A4')
360 {
361 $this->generator = new CSaleTfpdf($orientation, $unit, $size);
362 }
363
364 public function __call($name, $arguments)
365 {
366 return call_user_func_array(array($this->generator, $name), $arguments);
367 }
368
369 public function SetBackground($image, $style)
370 {
371 list($bgHeight, $bgWidth) = $this->GetImageSize($image);
372
373 $this->generator->SetBackground($this->GetImagePath($image), $bgHeight, $bgWidth, $style);
374 }
375
376 public function GetImageSize($file)
377 {
378 $height = 0;
379 $width = 0;
380
381 if (intval($file) > 0)
382 {
383 $arFile = CFile::GetFileArray($file);
384
385 if ($arFile)
386 {
387 $height = $arFile['HEIGHT'] * 0.75;
388 $width = $arFile['WIDTH'] * 0.75;
389 }
390 }
391 else
392 {
393 $arFile = CFile::GetImageSize($this->GetImagePath($file), true);
394
395 if ($arFile)
396 {
397 $height = $arFile[1] * 0.75;
398 $width = $arFile[0] * 0.75;
399 }
400 }
401
402 return array(0 => $height, 1 => $width);
403 }
404
405 public function GetImagePath($file)
406 {
407 $path = false;
408
409 if (intval($file) > 0)
410 {
411 $arFile = CFile::MakeFileArray($file);
412
413 if ($arFile)
414 $path = $arFile['tmp_name'];
415 }
416 elseif ($file)
417 {
418 $path = mb_strpos($file, $_SERVER['DOCUMENT_ROOT']) === 0
419 ? $file
420 : $_SERVER['DOCUMENT_ROOT'] . $file;
421 }
422
423 return $path;
424 }
425
426 public function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '')
427 {
428 $this->generator->Image($this->GetImagePath($file), $x, $y, $w, $h, $type, $link);
429 }
430
431 public static function CheckImage(array $file)
432 {
433 $pdf = new \tFPDF();
434
435 $pos = strrpos($file['name'], '.');
436 $type = substr($file['name'], $pos+1, strlen($file['name']));
437
438 try
439 {
440 $pdf->Image($file['tmp_name'], null, null, 0, 0, $type);
441 }
442 catch (Exception $e)
443 {
444 return $e->getMessage();
445 }
446
447 return null;
448 }
449}
$path
Определения access_edit.php:21
$type
Определения options.php:106
Определения pdf.php:312
static CheckImage(array $file)
Определения pdf.php:431
__call($name, $arguments)
Определения pdf.php:364
GetImagePath($file)
Определения pdf.php:405
SetBackground($image, $style)
Определения pdf.php:369
__construct($orientation='P', $unit='mm', $size='A4')
Определения pdf.php:359
splitString($text, $width)
Определения pdf.php:332
$generator
Определения pdf.php:314
static isPdfAvailable()
Определения pdf.php:316
static prepareToPdf($string)
Определения pdf.php:324
Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
Определения pdf.php:426
GetImageSize($file)
Определения pdf.php:376
Определения pdf.php:8
Output($name='', $dest='', $utfName='')
Определения pdf.php:83
calculateRowsWidth($cols, $cells, $countItems, $docWidth, $margin=null)
Определения pdf.php:132
GetPageHeight()
Определения pdf.php:78
getMaximumWord($str)
Определения pdf.php:294
SetBackground($image, $bgHeight=0, $bgWidth=0, $style='none')
Определения pdf.php:12
Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
Определения pdf.php:28
_parsebmp($file)
Определения pdf.php:95
Header()
Определения pdf.php:39
GetPageWidth()
Определения pdf.php:73
$str
Определения commerceml2.php:63
$f
Определения component_props.php:52
if(!is_array($prop["VALUES"])) $tmp
Определения component_props.php:203
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$p
Определения group_list_element_edit.php:23
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$l
Определения options.php:783
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения backup.php:924
htmlspecialcharsback($str)
Определения tools.php:2693
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$text
Определения template_pdf.php:79
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$width
Определения html.php:68
if(CSalePaySystemAction::GetParamValue('BACKGROUND', false)) $margin
Определения html.php:61
if(!empty($sellerData)) $dest
Определения pdf.php:818
$pdf
Определения pdf.php:13
const FPDF_FONTPATH
Определения pdf.php:3
$cells
Определения template.php:268
$max
Определения template_copy.php:262
$arRowsContentWidth
Определения template_pdf.php:447
$arRowsWidth
Определения template_pdf.php:446