Fixed an accounting sync in GL when adding bank account transactions (no customer...
[fa-stable.git] / reporting / includes / class.pdf.inc
1 <?php
2 /*
3         This class is an extension to the tcpdf class using a syntax that the original reports were written in
4         (the R &OS pdf.php class) - due to easily portation this wrapper class
5         was written to allow the same code base to use the more functional tcpdf.class by Nicola Asuni
6
7 *       Wrapper for use R&OSpdf API with tcpdf.org class
8 *       Joe Hunt <joe.hunt.consulting@gmail.com> and Janusz Dobrowolski <janusz@iron.from.pl>
9 */
10
11 define("K_RE_PATTERN_RTL", "/(
12           \xD6\xBE                                                                                                                                                                                      # R
13         | \xD7[\x80\x83\x86\x90-\xAA\xB0-\xB4]                                                                  # R
14         | \xDF[\x80-\xAA\xB4\xB5\xBA]                                                                                                           # R
15         | \xE2\x80\x8F                                                                                                                                                                  # R
16         | \xEF\xAC[\x9D\x9F\xA0-\xA8\xAA-\xB6\xB8-\xBC\xBE]                     # R
17         | \xEF\xAD[\x80\x81\x83\x84\x86-\x8F]                                                                           # R
18         | \xF0\x90\xA0[\x80-\x85\x88\x8A-\xB5\xB7\xB8\xBC\xBF]  # R
19         | \xF0\x90\xA4[\x80-\x99]                                                                                                                               # R
20         | \xF0\x90\xA8[\x80\x90-\x93\x95-\x97\x99-\xB3]                                 # R
21         | \xF0\x90\xA9[\x80-\x87\x90-\x98]                                                                                      # R
22         | \xE2\x80[\xAB\xAE]                                                                                                                                            # RLE & RLO
23         )/x");
24
25 /*
26  * Pattern to test Arabic strings using regular expressions.
27  * source: http://www.w3.org/International/questions/qa-forms-utf-8
28  */
29 define("K_RE_PATTERN_ARABIC", "/(
30                           \xD8[\x80-\x83\x8B\x8D\x9B\x9E\x9F\xA1-\xBA]  # AL
31                         | \xD9[\x80-\x8A\xAD-\xAF\xB1-\xBF]                                                     # AL
32                         | \xDA[\x80-\xBF]                                                                                                                               # AL
33                         | \xDB[\x80-\x95\x9D\xA5\xA6\xAE\xAF\xBA-\xBF]  # AL
34                         | \xDC[\x80-\x8D\x90\x92-\xAF]                                                                  # AL
35                         | \xDD[\x8D-\xAD]                                                                                                                               # AL
36                         | \xDE[\x80-\xA5\xB1]                                                                                                           # AL
37                         | \xEF\xAD[\x90-\xBF]                                                                                                           # AL
38                         | \xEF\xAE[\x80-\xB1]                                                                                                           # AL
39                         | \xEF\xAF[\x93-\xBF]                                                                                                           # AL
40                         | \xEF[\xB0-\xB3][\x80-\xBF]                                                                            # AL
41                         | \xEF\xB4[\x80-\xBD]                                                                                                           # AL
42                         | \xEF\xB5[\x90-\xBF]                                                                                                           # AL
43                         | \xEF\xB6[\x80-\x8F\x92-\xBF]                                                                  # AL
44                         | \xEF\xB7[\x80-\x87\xB0-\xBC]                                                                  # AL
45                         | \xEF\xB9[\xB0-\xB4\xB6-\xBF]                                                                  # AL
46                         | \xEF\xBA[\x80-\xBF]                                                                                                           # AL
47                         | \xEF\xBB[\x80-\xBC]                                                                                                           # AL
48                         | \xD9[\xA0-\xA9\xAB\xAC]                                                                                               # AN
49                         )/x");
50
51 include_once (dirname(__FILE__).'/tcpdf.php');
52
53 class Cpdf extends TCPDF {
54
55         function Cpdf($pageSize='A4', $l=array())
56         {
57                 if (!isset($l['a_meta_charset']))
58                         $l = array('a_meta_charset' => 'ISO-8859-1', 'a_meta_dir' => 'ltr', 'a_meta_language' => 'en_GB', 'w_page' => 'page');
59                 $enc = $l['a_meta_charset'];
60                 $uni = ($enc == 'UTF-8' || $enc == 'GB2312' ? true : false);
61                 if ($uni)
62                         ini_set("memory_limit", "48M");
63                 $this->TCPDF('P', 'pt', $pageSize, $uni, $enc);
64                 $this->setLanguageArray($l);
65                 $this->setPrintHeader(false);
66                 $this->setPrintFooter(false);
67                 $this->setPDFVersion("1.3");
68                 $this->setAutoPageBreak(0);
69                 $this->AddPage();
70
71                 $this->SetLineWidth(1);
72                 $this->cMargin = 0;
73         }
74
75         function selectFont($fontname, $style='')
76         {
77                 if ($fontname != '')
78                         $fontname = basename($fontname);
79                 if ($style == 'italic')
80                         $type = 'i';
81                 elseif ($style == 'bold')
82                         $type = 'b';
83                 else
84                         $type = '';
85                 if ($fontname == '')
86                 {
87                         if ($this->isunicode)
88                         {
89                                 switch ($this->l['a_meta_language'])
90                                 {
91                                         case "ar_EG" :  $fontname = "ae_tholoth";       break;
92                                         case "zh_CN" :  $fontname = "gbsn00lp";         break;
93                                         case "zh_TW" :  $fontname = "chinese_traditional_cid0";         break;
94                                         default :       $fontname = "dejavu";           break;
95                                 }
96                         }
97                         elseif ($this->encoding === "ISO-8859-2")
98                         {
99                                 switch ($this->l['a_meta_language'])
100                                 {
101                                         default :               $fontname = "freesans";         break;
102                                 }
103                         }
104                         elseif ($this->encoding === "ISO-8859-5")
105                         {
106                                 switch ($this->l['a_meta_language'])
107                                 {
108                                         default :               $fontname = "freesans5";        break;
109                                 }
110                         }
111                         // else use built-in adobe fonts helvetica.
112                 }
113                 $this->SetFont($fontname, $type);
114         }
115
116         function Header1()
117         {
118         }
119
120         function Footer()
121         {
122         }
123
124         function newPage()
125         {
126                 TCPDF::AddPage();
127         }
128
129         function line($x1,$y1,$x2,$y2)
130         {
131                 TCPDF::line($x1, $this->h-$y1, $x2, $this->h-$y2);
132         }
133
134         function rectangle($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array())
135         {
136                 TCPDF::Rect($x, $this->h-$y, $w, $h, $style, $border_style, $fill_color);
137         }
138
139
140         function addText($xb,$yb,$size,$txt)//,$angle=0,$wordSpaceAdjust=0)
141         {
142                 if ($this->isunicode && $this->encoding != "UTF-8")
143                         $txt = iconv($this->encoding, "UTF-8", $txt);
144                 $this->SetFontSize($size);
145                 $this->Text($xb, $this->h-$yb, $txt);
146         }
147
148         function addInfo($label,$value)
149         {
150                 if($label=='Title')
151                 {
152                         $this->SetTitle($value);
153                 }
154                 if ($label=='Subject')
155                 {
156                         $this->SetSubject($value);
157                 }
158                 if($label=='Creator')
159                 {
160                         // The Creator info in source is not exactly it should be ;)
161                         $value = str_replace( "ros.co.nz", "tcpdf.org", $value );
162                         $value = str_replace( "R&OS", "", $value );
163                         $this->SetCreator( $value );
164                 }
165                 if($label=='Author')
166                 {
167                         $this->SetAuthor($value);
168                 }
169         }
170
171         function addJpegFromFile($img,$x,$y,$w=0,$h=0)
172         {
173                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
174         }
175
176         function addPngFromFile($img,$x,$y,$w=0,$h=0)
177         {
178                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
179         }
180         /*
181         * Next Two functions are adopted from R&OS pdf class
182         */
183
184         /**
185         * draw a part of an ellipse
186         */
187         function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8)
188         {
189                 $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
190         }
191
192         /**
193         * draw an ellipse
194         * note that the part and filled ellipse are just special cases of this function
195         *
196         * draws an ellipse in the current line style
197         * centered at $x0,$y0, radii $r1,$r2
198         * if $r2 is not set, then a circle is drawn
199         * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
200         * pretty crappy shape at 2, as we are approximating with bezier curves.
201         */
202         function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0)
203         {
204                 TCPDF::Ellipse($x0, $y0, $r1, $r2, $angle, $astart. $afinish, ($close?'C':''), "", "", $nSeg);
205         }
206         function Stream()
207         {
208                 TCPDF::Output('', 'I');
209         }
210
211         function Output()
212         {
213                 return TCPDF::Output('','S');
214         }
215
216         function calcTextWrap($txt, $width, $spacebreak=false)
217         {
218                 $ret = "";
219                 $txt2 = $txt;
220                 $w = $this->GetStringWidth($txt);
221                 if ($w > $width && $w > 0 && $width != 0)
222                 {
223                         $n = strlen($txt);
224                         $k = intval($n * $width / $w);
225                         if ($k > 0 && $k < $n)
226                         {
227                                 $txt2 = substr($txt, 0, $k);
228                                 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
229                                 {
230                                         $txt2 = substr($txt2, 0, $pos);
231                                         $ret = substr($txt, $pos+1);
232                                 }
233                                 else
234                                         $ret = substr($txt, $k);
235                         }
236                 }
237                 return array($txt2, $ret);
238         }
239
240         function addTextWrap($xb, $yb, $w, $h, $txt, $align='left', $border=0, $fill=0, $spacebreak=false)
241         {
242                 $ret = "";
243                 if (!$this->rtl)
244                 {
245                         if ($align == 'right')
246                                 $align = 'R';
247                         elseif ($align == 'left')
248                                 $align = 'L';
249                 }
250                 else
251                         $align = 'R';
252                 $txt = $this->calcTextWrap($txt, $w, $spacebreak);
253                 $ret = $txt[1];
254                 $txt = $txt[0];
255                 $this->SetXY($xb, $this->h - $yb - $h);
256                 $txt = TCPDF::unhtmlentities($txt);
257                 if ($this->isunicode && $this->encoding != "UTF-8")
258                         $txt = iconv($this->encoding, "UTF-8", $txt);
259                 $this->Cell($w, $h, $txt, $border, 0, $align, $fill);
260                 return $ret;
261         }
262 /**
263 * sets the colour for stroke operations
264 */
265     function setStrokeColor($r,$g,$b,$force=0)
266     {
267         TCPDF::SetDrawColor($r,$g,$b);
268     }
269 /**
270 * this sets the line drawing style.
271 * width, is the thickness of the line in user units
272 * cap is the type of cap to put on the line, values can be 'butt','round','square'
273 *    where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
274 *    end of the line.
275 * join can be 'miter', 'round', 'bevel'
276 * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
277 *   on and off dashes.
278 *   (2) represents 2 on, 2 off, 2 on , 2 off ...
279 *   (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
280 * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
281 */
282         function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0)
283         {
284                 $this->SetLineWidth($width);
285         }
286
287         function Text($x, $y, $txt, $stroke=0, $clip=false)
288         {
289                 TCPDF::Text($x,$y, TCPDF::unhtmlentities($txt), $stroke, $clip);
290         }
291
292 } // end of class
293
294 ?>