		
		function a4pdf_DB($bon_id){
					require_once("config/connect.php");
					require_once("global_functions.php");
					require_once('include/extended_fpdf.php');
						
						$fontConfig=SCHRIFT_AUSDRUCK_PDF;
						

						if ( strpos(strtolower($fontConfig), 'arial') !== false  || strpos(strtolower($fontConfig), 'helvetica') !== false || 
									strpos(strtolower($fontConfig), 'sans serif') !== false ){ $pdfFont="Arial"; }
									
						else if ( strpos(strtolower($fontConfig), 'times') !== false  || strpos(strtolower($fontConfig), 'roman') !== false ||
									(strpos(strtolower($fontConfig), 'serif') !== false &&  strpos(strtolower($fontConfig), 'sans') == false)) { $pdfFont="Times"; }
						else if (strpos(strtolower($fontConfig), 'courier') !== false ) { $pdfFont="Courier";   }
						else {$pdfFont="Arial";  }
						
						preg_match('/\d+px | \d+pt/', $fontConfig, $m);
						preg_match('/\d+/', $m[0], $match);
						$pdfFontSize=$match[0];	

						$pdfFontSize*=2;

						
						$lineHeight=$pdfFontSize/2.5;
						

						$bonWidth=preg_replace('/[^\d]/', '', AUSDRUCK_GESAMTE_BREITE);
						$printer=mysql_fetch_array(mysql_query("SELECT win_drucker_name, drucker_margin_pdf, zoom_factor  FROM drucker WHERE LOWER(ist_a4) = 'ja' "));
						$drucker=$printer[0];
						
						$margin=$printer[1];
						$margins=explode(",", $margin);
						
						if(isset($margins[2]))$marginLR=preg_replace('/\D/', '', $margins[2]); else $marginLR=0; 
						if(isset($margins[1]))$marginBottom=preg_replace('/\D/', '', $margins[1]); else $marginBottom=0; 			
						if(isset($margins[0]))$marginTop=preg_replace('/\D/', '', $margins[0]); else $marginTop=0; 	
						
						if($marginLR < 15) $marginLR = 15;
						
						if(is_int($printer[2]) || is_float($printer[2])) $zoomF=$printer[2]; 
						else $zoomF=1;
						
						$pageWidth=210-($marginLR*2);
						
						$pdfPrinterData=$printer;
						$bonData = mysql_fetch_array(mysql_query("SELECT kunden_id, verkaufer_id, gesamt_summe, retourgeld, bon_erstellt_datum, storno_datum, gastro_tisch_id 
													FROM abgeschlossener_bon WHERE bon_id=".$bon_id." "));
						if(!$bonData) $xx=mysql_error();
						
								
					class PDF extends PDF_MemImage{

						function Header() {
							$marginTop=$this->marginTop;
							$marginLR=$this->marginLR;
							$pdfFontSize=$this->pdfFontSize;
							$pdfFont=$this->pdfFont;
							$lineHeight=$this->lineHeight;
							$bonId=$this->bonId;
							$bonData=$this->bonData;
							
							$this->SetFont($pdfFont, '', $pdfFontSize);

							$calcH=0;
							if(defined('AUSDRUCK_A4_LOGO_POSITION') && AUSDRUCK_A4_LOGO_POSITION != "")$logoPos=AUSDRUCK_A4_LOGO_POSITION;
							else $logoPos='R, 50';	
							
							$logoPos=explode(',', $logoPos);
							if(isset($logoPos[0]) && $logoPos[0] != '')$logoPos[0]=$logoPos[0];
							else $logoPos[0] = 'R';
							if(isset($logoPos[1]) && $logoPos[1] > 20)$imgWidth=$logoPos[1];
							else $imgWidth = 50;
							
							if(strtolower($logoPos[0]) == 'r') $leftPos=210-($imgWidth+$marginLR);
							else if(strtolower($logoPos[0]) == 'l') $leftPos=$marginLR;
							else if(strtolower($logoPos[0]) == 'c') $leftPos=(210-$imgWidth)/2;	
							
							if(defined('AUSDRUCK_A4_ADRESS_POSITION') && AUSDRUCK_A4_ADRESS_POSITION != "")$adressPos=AUSDRUCK_A4_ADRESS_POSITION;
							else $adressPos='L, 30, R';
							
							$adressPos=explode(',', $adressPos);
							if(isset($adressPos[0]) && $adressPos[0] != '')$adressPos[0]=$adressPos[0];
							else $adressPos[0]= 'L';
							if(isset($adressPos[1]) && $adressPos[1] > 20)$adressCellWidth=$adressPos[1];
							else $adressCellWidth=100;
							if(isset($adressPos[2]) && $adressPos[2] != '')$adressPos[2]=$adressPos[2];
							else $adressPos[2]= 'L';
		
							if ($this->PageNo() == 1){
															
								#	$this->Cell(0, $lineHeight, $adressPos[0]."-".$adressPos[1]."-".$adressPos[2], "BT", 1, 'C');
									if(defined('AUSDRUCK_LOGO_RECHNUNGSBON_PDF') && strtolower(AUSDRUCK_LOGO_RECHNUNGSBON_PDF) == 'ja'){
										$getImg=mysql_fetch_assoc(mysql_query("SELECT image, name, imgx, imgy FROM images WHERE name='bonlogo'"));
										$zuzX=$this->GetY();
										if($getImg == true){
											
											$fac=$getImg['imgx']/$imgWidth;
											$calcH=$getImg['imgy']/$fac;
											
											$this->MemImage($getImg['image'], $leftPos, $marginTop, $imgWidth, 0);   // breite ist definiert höhe auf null heißt wird calkuliert
										}
	
										if( strtolower($logoPos[0]) == 'r' && strtolower($adressPos[0]) == 'r'){
											$this->SetXY($leftPos, $marginTop+$calcH);
											$curYpos=$this->GetY()+$lineHeight;
										
										}
										else $curYpos=$marginTop;
									}
									
									else {
										$curYpos=$marginTop;
										
									}
									
									$ibanTx="";
									$kopfQl=mysql_query("SELECT name, wert FROM konfiguration WHERE name LIKE 'AUSRUCK_KOPFZEILE_%' AND wert <> '' ORDER BY name");
										$this->footerText1="";
										$this->footerText2="";
										while($kopfR=mysql_fetch_array($kopfQl)){
												if($kopfR["name"] == "AUSRUCK_KOPFZEILE_IBAN"){
												$ibanTx=strip_tags($kopfR['wert']);
												continue;
											}
											$wert=$kopfR["wert"];
												$zz=strtolower(strip_tags($kopfR['wert']));
												if(strpos($kopfR["name"], 'KOPFZEILE_1') !== false || strpos($kopfR["name"], 'KOPFZEILE_2') !== false || strpos($kopfR["name"], 'KOPFZEILE_3') !== false)
															$this->footerText1.=strip_tags($kopfR['wert']);
												else $this->footerText2.=strip_tags($kopfR['wert']);
											
												
												if(strtolower(trim($adressPos[0])) == 'r' && strtolower(trim($adressPos[2])) == 'r'){
													$this->SetXY(210-($adressPos[1]+$marginLR), $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'R');
												}
												else if(strtolower(trim($adressPos[0])) == 'r' && strtolower(trim($adressPos[2])) == 'c'){
													$this->SetXY(210-($adressPos[1]+$marginLR), $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'C');
												}
												else if(strtolower(trim($adressPos[0])) == 'r' && strtolower(trim($adressPos[2])) == 'l'){
													$this->SetXY(210-($adressPos[1]+$marginLR), $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'L');
												}
												else if(strtolower(trim($adressPos[0])) == 'c' && strtolower(trim($adressPos[2])) == 'r'){
													$this->SetXY((210-$adressPos[1])/2, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'R');
												}
												else if(strtolower(trim($adressPos[0])) == 'c' && strtolower(trim($adressPos[2])) == 'c'){
													$this->SetXY((210-$adressPos[1])/2, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'C');
												}
												else if(strtolower(trim($adressPos[0])) == 'c' && strtolower(trim($adressPos[2])) == 'l'){
													$this->SetXY((210-$adressPos[1])/2, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'L');
												}
												else if(strtolower(trim($adressPos[0])) == 'l' && strtolower(trim($adressPos[2])) == 'r'){
													$this->SetXY($marginLR, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'R');
												}
												else if(strtolower(trim($adressPos[0])) == 'l' && strtolower(trim($adressPos[2])) == 'c'){
													$this->SetXY($marginLR, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'C');
												}
												else if(strtolower(trim($adressPos[0])) == 'l' && strtolower(trim($adressPos[2])) == 'l'){
													$this->SetXY($marginLR, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 0, 1, 'L');
												}
												
												else{ 
													$this->SetXY($marginLR, $curYpos);
													$this->Cell($adressCellWidth, $lineHeight, strip_tags($kopfR['wert']), 1, 1, 'L');
												}	

												$curYpos+=$lineHeight; #  die Zellen sind absolut positioniert, deswegen muss hier die Y position immer um eine lineHeight erhöht werden
											
											
										}
										
										$this->footerText.=$ibanTx;
	
								$kundenId=$bonData["kunden_id"];	
	
								$this->SetY(65);
								
								if($kundenId > 0 ){
									$custData="";
									#	$this->Cell(0, 1, "", 0, 1, 'C');
										$kundenDetails=mysql_fetch_assoc(mysql_query("SELECT * FROM kunden WHERE id=".$kundenId." "));
										if($kundenDetails['Firma'] != '')$this->Cell(0, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kundenDetails['Firma'])), 0, 1, 'L');
										if($kundenDetails['Name'] != '')$this->Cell(0, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kundenDetails['Name'])), 0, 1, 'L');
										if($kundenDetails['Adresse'] != '')$this->Cell(0, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kundenDetails['Adresse'])), 0, 1, 'L'); 
										if($kundenDetails['plz'] != '' || $kundenDetails['Ort'] != '')
											$this->Cell(0, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kundenDetails['plz']))." ".iconv('UTF-8', 'windows-1252', strip_tags($kundenDetails['Ort'])), 0, 1, 'L'); 
																			
										$this->Cell(0, $lineHeight, "", 'B', 1, 'C'); 	#  trennlinie unterhalb von Kunden Druck, falls Kunde ausgegeben wird										
								}
								
								else {
									$zahlung=mysql_query("SELECT Name FROM abgeschlossener_bon_zahlung WHERE bon_id=".$bonId." ");
									while($zahlR=mysql_fetch_array($zahlung)){
										$zahlForm=$zahlR['Name'];
									}
									
									if(mysql_num_rows ($zahlung) > 1 ) $zahlForm="Gemischte Zahlungsform";
										
									$this->Cell(0, $lineHeight, $zahlForm." Verkauf", 0, 1, 'L');
									$this->Cell(0, $lineHeight, "", 0, 1, 'L');
									$this->Cell(0, $lineHeight, "", 0, 1, 'L');
									$this->Cell(0, $lineHeight, "", 0, 1, 'L');
									
									$this->Cell(0, $lineHeight, "", 'B', 1, 'C');
								
								}
							
							}
							
							else {					# bei mehrseitigen ausdruck wird ab der zweiten seite ein reduzierter header gedruckt
								if(defined('AUSDRUCK_LOGO_RECHNUNGSBON_PDF') && strtolower(AUSDRUCK_LOGO_RECHNUNGSBON_PDF) == 'ja'){
										$getImg=mysql_fetch_assoc(mysql_query("SELECT image, name, imgx, imgy FROM images WHERE name='bonlogo'"));

										if($getImg == true){
											
											$fac=$getImg['imgx']/$imgWidth;
											$calcH=$getImg['imgy']/$fac;
											$this->MemImage($getImg['image'], $leftPos, $marginTop, $imgWidth, 0);   // breite ist definiert höe auf null heißt wird calkuliert
										}
								}
								
								$this->Ln($calcH+$marginTop+10);
								
								if($this->checkPageBreak == true){
									$pageWidth=210-($this->marginLR*2);
									$colA_width=($pageWidth*.12)*$this->zoomF;
									$colB_width=($pageWidth*.38)*$this->zoomF;
									$colC_width=($pageWidth*.25)*$this->zoomF;
									$colD_width=($pageWidth*.25)*$this->zoomF;
									
									$this->SetFillColor(90);
									$this->SetTextColor(255); 
									
									$this->Cell($colA_width, $lineHeight*1.3, "Stk.", 0, 0, 'R', true);
									$this->Cell($colB_width, $lineHeight*1.3, "Bezeichnung", 0, 0, 'L', true);
									$this->Cell($colC_width, $lineHeight*1.3, "Einzel", 0, 0, 'R', true);
									$this->Cell($colD_width, $lineHeight*1.3, "Ges. Brutto", 0, 1, 'R', true);
									
									$this->SetFillColor(255);
									$this->SetTextColor(0);
									
								}
							
							}
							
							
						}
						
						function docData(){
							$marginLR=$this->marginLR;
							$pdfFontSize=$this->pdfFontSize;
							$pdfFont=$this->pdfFont;
							$lineHeight=$this->lineHeight;
							$bonId=$this->bonId;
							$bonData=$this->bonData;
							
							$invoiceSumRaw=$bonData["gesamt_summe"];
							$gesamtsumme_brutto=number_format($bonData["gesamt_summe"],2,",",".");
							$datetime=$bonData["bon_erstellt_datum"];
							$stornoDatetime=$bonData["storno_datum"];
							$retourgeld=$bonData["retourgeld"];
						
							$verkauferId=$bonData["verkaufer_id"];
							$gastroTischId=$bonData["gastro_tisch_id"];
						
							$this->SetFont($pdfFont, '', $pdfFontSize);
							$pageWidth=210-($marginLR*2);
							
							$this->Cell($pageWidth/2, $lineHeight*1.2, BELEG_BENENNUNG." ".$bonId, 0, 0, 'L');				
							$this->Cell($pageWidth/2, $lineHeight*1.2, $datetime, 0, 1, 'R');	
			
							if(defined("VERKAEUFER_AUF_BON_DRUCKEN") && strtolower(VERKAEUFER_AUF_BON_DRUCKEN)=='ja'){
								$userQl=mysql_fetch_array(mysql_query("SELECT name FROM benutzer WHERE id = ".$verkauferId." "));
								if($userQl==true) $this->Cell($pageWidth/2, $lineHeight*1.2, AUSDRUCK_VERKAUFER_AUF_BON_TEXT." ".iconv('UTF-8', 'windows-1252', $userQl[0]), 0, 0, 'L');
							}
							else if($userQl==true) $this->Cell($pageWidth/2, $lineHeight*1.2, "", 0, 0, 'L');
							$this->Cell($pageWidth/2, $lineHeight*1.2, iconv('UTF-8', 'windows-1252', "Beträge in EUR"), 0, 1, 'R');
							
							$this->Cell(0, $lineHeight*1.2, "", "T", 1, 'C');	
						
							$colA_width=($pageWidth*.12)*$this->zoomF;
							$colB_width=($pageWidth*.38)*$this->zoomF;
							$colC_width=($pageWidth*.25)*$this->zoomF;
							$colD_width=($pageWidth*.25)*$this->zoomF;
							
							$this->SetFillColor(90);
							$this->SetTextColor(255); 
							
							$this->Cell($colA_width, $lineHeight*1.3, "Stk.", 0, 0, 'R', true);
							$this->Cell($colB_width, $lineHeight*1.3, "Bezeichnung", 0, 0, 'L', true);
							$this->Cell($colC_width, $lineHeight*1.3, "Einzel", 0, 0, 'R', true);
							$this->Cell($colD_width, $lineHeight*1.3, "Ges. Brutto", 0, 1, 'R', true);
							
							$this->SetFillColor(255);
							$this->SetTextColor(0);
							
							$this->Cell(0, $lineHeight/2, "", 0, 1, 'C'); 
							$sql_produkte = mysql_query("SELECT produkt_id, tax_rate, REFERENCE, CODE, NAME, PRICEBUY, PRICESELL, CATEGORY, TAXCAT, tax_rate, DISPLAY, 
										VARPRICE, PRINTNAME, NEGATIVEPRICE, PRODUKTORDER, SICHTBAR, stueck 
										FROM abgeschlossener_bon_produkte WHERE bon_id='".$bonId."'");

							if (!$sql_produkte)	{
								echo "<script>console.log('Datenbank Fehler: '".mysql_error().")</script>";		
								exit(1);
							}
							
							$countRows=mysql_num_rows($sql_produkte)*1;
							$checkRows=0;
							$this->checkPageBreak=0;
							
							while ($row_produkt=mysql_fetch_assoc($sql_produkte)) {
								$einzel_preis=$row_produkt["PRICESELL"];
								$einzel_preis=$einzel_preis*(1+$row_produkt["tax_rate"]);
								$summe=$einzel_preis*$row_produkt["stueck"];
								$einzel_preis=number_format($einzel_preis,2,",",".");
								$stueck=$row_produkt["stueck"];
								// Checke ob Stueck nachkomma hat oder nicht wenn ja, dann 2 Nachkommastellen ansonsten keine
								if ( (int) $stueck == $stueck) {
									$stueck_anzeige=number_format($stueck,0,"",".");
								}	
								else $stueck_anzeige=number_format($stueck,2,",",".");
									
								$summe=number_format($summe,2,",",".");
								$printname=$row_produkt["PRINTNAME"];

								$this->Cell($colA_width, $lineHeight, $stueck_anzeige, 0, 0, 'R');
								
				
								if($this->getstringwidth(iconv('UTF-8', 'windows-1252', $printname)) > $colB_width){
									$posY=$this->GetY();
									$posX=($colA_width + $colB_width + $marginLR );
									$this->MultiCell( $colB_width, $lineHeight, iconv('UTF-8', 'windows-1252', $printname) , 0, 'L', 0);  # border-0; align L, fill-0,
									$posY_2=$this->GetY();
									$this->SetXY($posX, $posY);
									
									$this->Cell($colC_width, $lineHeight, $einzel_preis, 0, 0, 'R');
									$this->Cell($colD_width, $lineHeight, $summe, 0, 1, 'R');
									$this->SetY($posY_2);
								}
								else{	
									$this->Cell($colB_width, $lineHeight, iconv('UTF-8', 'windows-1252', $printname), 0, 0, 'L');
									$this->Cell($colC_width, $lineHeight, $einzel_preis, 0, 0, 'R');
									$this->Cell($colD_width, $lineHeight, $summe, 0, 1, 'R');
								}
								
								$checkRows++;
								if($checkRows < $countRows) $this->checkPageBreak = true;
								else $this->checkPageBreak = false;
								
							}	
						$this->Cell(0, $lineHeight/2, "", 0, 1, 'C', 0);
						$this->SetLineWidth(.5);	
						$this->SetDrawColor(90);
						$this->SetFont($pdfFont,'B', $pdfFontSize); 
						$this->Cell($colA_width + $colB_width, $lineHeight*1.2, "Rechnungssumme: ", "T", 0, 'L',0);
						
						$this->Cell($colC_width + $colD_width, $lineHeight*1.2, $gesamtsumme_brutto, "T", 1, 'R',0);
						$this->SetLineWidth(.2);	
						$this->SetDrawColor(250, 250, 250);
						
						$this->SetFont($pdfFont,'', $pdfFontSize); 
						$this->Cell(0, $lineHeight*1.5, "", 0, 1, 'C');
						
						$StColBlind_width=($pageWidth*.15)*$this->zoomF;
						$StColA_width=($pageWidth*.20)*$this->zoomF;
						$StColB_width=($pageWidth*.20)*$this->zoomF;
						$StColC_width=($pageWidth*.20)*$this->zoomF;
						$StColD_width=($pageWidth*.25)*$this->zoomF;
						
						$this->Cell($StColBlind_width, $lineHeight, "", 0, 0, 'R');
						$this->Cell($StColA_width, $lineHeight, "Steuersatz", 0, 0, 'R');
						$this->Cell($StColB_width, $lineHeight, "Netto", 0, 0, 'R');
						$this->Cell($StColC_width, $lineHeight, "Steuer", 0, 0, 'R');
						$this->Cell($StColD_width, $lineHeight, "Brutto", 0, 1, 'R');
						
						
					$sql_tax = mysql_query("SELECT tax_rate, gesamt_betrag from abgeschlossener_bon_steuern WHERE bon_id='".$bonId."'");
					if (! $sql_produkte){
						echo "<script>console.log('Datenbank Fehler: '".mysql_error().")</script>";		
						exit(1);
					}
		
					while ($rowTax=mysql_fetch_assoc($sql_tax)) 	{
						$steuersatz=$rowTax["tax_rate"]*100;
						$netto=$rowTax["gesamt_betrag"];
						$steuer=$netto*$rowTax["tax_rate"];
						$brutto=$netto+$steuer;
						$brutto=number_format($brutto,2,",",".");
						$netto=number_format($netto,2,",",".");
						$steuer=number_format($steuer,2,",",".");
						
						$this->Cell($StColBlind_width, $lineHeight, "", 0, 0, 'R');
						$this->Cell( $StColA_width, $lineHeight, $steuersatz, 0, 0, 'R');
						$this->Cell( $StColB_width, $lineHeight, $netto , 0, 0, 'R');
						$this->Cell( $StColC_width, $lineHeight, $steuer, 0, 0, 'R');
						$this->Cell( $StColD_width, $lineHeight, $brutto, 0, 1, 'R');
					
					
					}
		 													
			}  # ende function docdata 
						
						function Footer(){
								$this->SetY(-AUSDRUCK_A4_FOOTER_HOEHE);
								$this->SetDrawColor(0);
								$this->Cell(0, 2, "", "T", 1, 'C');
								
								$this->SetFont($this->pdfFont, '', $this->pdfFontSize*.75);
								$this->Cell(0, $this->lineHeight, $this->footerText1, 0, 1, 'C');
								$this->Cell(0, $this->lineHeight, $this->footerText2, 0, 1, 'C');
								$this->Cell(0, $this->lineHeight, 'Seite '.$this->PageNo(). '/{nb}', 0, 1, 'R');								 
						}
					}
					
					
					$pdf=new PDF('P','mm', 'A4');
					$pdf->SetMargins($marginLR, $marginTop);

					$pdf->header = 0;
					$pdf->footer = 0;
				#	$pdf->addPage('','',false); 
					
					$pdf->pdfPrinterData=$pdfPrinterData;
					$pdf->marginTop=$marginTop;
					$pdf->marginLR=$marginLR;
					$pdf->pdfFont=$pdfFont;
					$pdf->pdfFontSize=$pdfFontSize;
					$pdf->lineHeight=$lineHeight;
					$pdf->bonId=$bon_id;
					$pdf->bonData=$bonData;
					$pdf->zoomF=$zoomF;
					$pdf->footerText1="";
					$pdf->footerText2="";
					
					$pdf->checkPageBreak=false;
					$pdf->SetAutoPageBreak(true, AUSDRUCK_A4_FOOTER_HOEHE+15);
					
					$pdf->AddPage('','',false); 
					$pdf->docData();
					$pdf->AliasNbPages('{nb}');

					
					
						
					$outputTarget=__DIR__ ."\docs\kpA4Test_".$bon_id."_".date('d_m_H_i').".pdf";
					$command="C:\sitpos\pdfxchange\App\PDFV_Portable\PDFXCview.exe /print:printer=\"$drucker\" \"$outputTarget\" ";	

							
					$writeFile=$pdf->Output($outputTarget, "F");
					
					exec($command, $output, $error_code);
				
		}