<?php 
if (!ini_get('display_errors')) {
    ini_set('display_errors', '1');
}
require_once("./vendor/autoload.php");
require_once("invoice_helpers.php");

if($_SERVER['REQUEST_METHOD'] === "OPTIONS"){ echo json_encode(array("1")); exit(1); }

/* priprema podataka */
$data = $_POST;

/* ===== PODACI OPĆENITO ====== */
$jezik = $data['jezik'];
$generate_2d_barcode = (isset($_POST["generate_2d_barcode"]) && $_POST["generate_2d_barcode"] == "1")? $_POST["generate_2d_barcode"] : false;
$generate_signing_spot = (isset($_POST["generate_signing_spot"]) && $_POST["generate_signing_spot"] == "1")? $_POST["generate_signing_spot"] : false;
$generate_payment_data = (isset($_POST["generate_payment_data"]) && $_POST["generate_payment_data"] == "1")? $_POST["generate_payment_data"] : false;


/*===== ZAGLAVLJE, PODACI O IZDAVACU RACUNA I KUPCU ======*/
$logo = (isset($data['logo']) && $data['logo']!="")? $data['logo'] : false;
$vrsta = $data['vrsta'];
$broj_dokumenta = $data['broj_dokumenta'];
$datum_kreiranja = $data['datum_kreiranja'];
$datum_isporuke = $data['datum_isporuke'];
$rok_placanja = $data['rok_placanja'];
$datum_racuna = (isset($data["datum_racuna"]) && $data["datum_racuna"] != "")? $data["datum_racuna"] : false;

$kupac_naziv = (isset($data['kupac']['naziv']) && $data['kupac']['naziv']!="")? $data['kupac']['naziv'] : false ;
$kupac_adresa = (isset($data['kupac']['adresa']) && $data['kupac']['adresa']!="")? $data['kupac']['adresa'] : false ;
$kupac_postanski_broj_i_mjesto = (isset($data['kupac']['postanski_broj_i_mjesto']) && $data['kupac']['postanski_broj_i_mjesto']!="")? $data['kupac']['postanski_broj_i_mjesto'] : false ;
$kupac_oib = (isset($data['kupac']['oib']) && $data['kupac']['oib']!="")? $data['kupac']['oib'] : false ;

$izdavac_racuna_naziv = (isset($data['izdavac_racuna']['naziv']))? $data['izdavac_racuna']['naziv'] : "" ;
$izdavac_racuna_adresa = (isset($data['izdavac_racuna']['adresa']))? $data['izdavac_racuna']['adresa'] : "" ;
$izdavac_racuna_postanski_broj_i_mjesto = (isset($data['izdavac_racuna']['postanski_broj_i_mjesto']))? $data['izdavac_racuna']['postanski_broj_i_mjesto'] : "" ;
$izdavac_racuna_oib = (isset($data['izdavac_racuna']['oib']))? $data['izdavac_racuna']['oib'] : "" ;
$izdavac_racuna_id_agencije = (isset($data['izdavac_racuna']['id_agencije']))? $data['izdavac_racuna']['id_agencije'] : false ;


/* ===== PODACI O PLAĆANJU ====== */ 

$nacin_placanja = (isset($data['nacin_placanja']) && $data['nacin_placanja'] )? $data['nacin_placanja'] : false ;
$kreditna_kartica = (isset($data['kreditna_kartica']) && $data['kreditna_kartica']!="")? $data['kreditna_kartica'] : false ;
$r1 = (isset($data['r1']))? true : false;
$racun_izdao = $data['racun_izdao'];
$iban = $data['iban'];
$swift = $data['swift'];
$poziv_na_broj = (isset($data['poziv_na_broj']) && $data['poziv_na_broj']!="")? $data['poziv_na_broj'] : false ;
$model_racuna = $data['model_racuna'];
$opis_placanja = $data['opis_placanja'];
$napomena_pacanja_arr  = (isset($_POST['napomene']))? $_POST['napomene'] : [];

$zki = (isset($data['zki']) && $data['zki']!="")? $data['zki'] : false ;
$jir = (isset($data['jir']) && $data['jir']!="")? $data['jir'] : false ;

/* ===== VALUTE ====== */

$valute = $data['valute'];
/*
'valute' : {
	'primarna' : {
			oznaka : EUR || HRK || ...
			tecaj : null || float 
		}
	}, 
	'sekundarna' : {
			oznaka : EUR || HRK || ...
			tecaj : null || float 
		}
	}
}
 */

$vpo = $data['valute']['primarna']['oznaka'];  // oznaka primarna
$vso = (isset($data['valute']['sekundarna']['oznaka'] ))? $data['valute']['sekundarna']['oznaka'] : false; // oznaka sekundarna
$valute_billing = [];
foreach ($valute as $key=>$v) {
	$valute_billing[$v['oznaka']] = $v['tecaj'];
}

/* ===== ARTIKLI ====== */
$products = $_POST['stavke'];

$datum_racuna_o = new DateTime($datum_racuna);
$datum_racuna_hnb = date_format($datum_racuna_o, "Y-m-d");

$billing = billing($products, $datum_racuna_hnb, true, $vpo, $valute_billing );
// zakoruzi na dvije decimale
	function round_two_decimals(&$item, $key) {
		if(is_float($item) || is_integer($item)) $item = number_format($item, 2, ".", ",");
	}
	array_walk_recursive($billing, 'round_two_decimals');

$stavke = $billing['stavke'];
$pdv_stope = $billing['pdv_stope'];
$total = $billing['ukupno'];

$predujam_stavke = $billing['predujam']['stavke'];
$ostatak_za_platiti = $billing['ostatak_za_uplatu'];
$has_advance = (!empty($predujam_stavke))? true : false;

/* popuni product bank sa stavkama iz računa i predujmovima */
$product_bank = [];
foreach($stavke as $item) array_push($product_bank, $item);
foreach($predujam_stavke as $item) array_push($product_bank, $item);

/* ===== NAPOMENE ====== */
$napomena_placanja = "";
foreach ($napomena_pacanja_arr as $napomena) {
	$napomena_placanja.= $napomena."<br>";
}

/* ===== OPĆI PODACI ====== */
$opci_podaci_web_stranica = $data['opci_podaci']['web_stranica'];
$opci_podaci_telefon = $data['opci_podaci']['telefon'];
$opci_podaci_mobilni = $data['opci_podaci']['mobilni'];
$opci_podaci_email = $data['opci_podaci']['email'];
$opci_podaci_disclaimer = $data['opci_podaci']['disclaimer'];
$opci_podaci_sjediste = $data['opci_podaci']['sjediste'];

/* ===== POSLOVNICA ====== */
$poslovnica_naziv = $poslovnica_adresa  = $poslovnica_postanski_broj_i_mjesto = $poslovnica_broj_tel = false;
if(isset($data['poslovnica']) && $data['poslovnica']){
	$poslovnica_naziv = $data['poslovnica']['naziv'];
	$poslovnica_adresa = $data['poslovnica']['adresa'];
	$poslovnica_postanski_broj_i_mjesto = $data['poslovnica']['postanski_broj_i_mjesto'];
	$poslovnica_broj_tel = $data['poslovnica']['broj_tel'];
}

/* ===== IZNAJMLJIVAC ====== */
$iznajmljivac_naziv = $iznajmljivac_adresa = $iznajmljivac_postanski_broj_i_mjesto = $iznajmljivac_oib = false;
if(isset($data['iznajmljivac']) && !empty($data['iznajmljivac'])){
	$iznajmljivac_naziv = $data['iznajmljivac']['naziv'];
	$iznajmljivac_adresa = $data['iznajmljivac']['adresa'];
	$iznajmljivac_postanski_broj_i_mjesto = $data['iznajmljivac']['postanski_broj_i_mjesto'];
	$iznajmljivac_oib = $data['iznajmljivac']['oib'];
}

/* 2d bar code HUB3 */
if($generate_2d_barcode){
	$sifra_namjene = "COST";
	$podaci_2d_barcode['ime_i_prezime_platitelja'] = $kupac_naziv;
	$podaci_2d_barcode['adresa_platitelja_ulica_i_broj'] = $kupac_adresa;
	$podaci_2d_barcode['adresa_platitelja_postanski_broj_i_mjesto'] = $kupac_postanski_broj_i_mjesto;
	$podaci_2d_barcode['naziv_primatelja'] = $izdavac_racuna_naziv;
	$podaci_2d_barcode['adresa_primatelja_ulica_i_broj'] = $izdavac_racuna_adresa;
	$podaci_2d_barcode['adresa_primatelja_postanski_broj_i_mjesto'] = $izdavac_racuna_postanski_broj_i_mjesto;
	$podaci_2d_barcode['iban_ili_racun_primatelja'] = $iban;
	$podaci_2d_barcode['model_racuna_primatelja'] = $model_racuna;
	$podaci_2d_barcode['poziv_na_broj_primatelja'] = $poziv_na_broj;
	$podaci_2d_barcode['sifra_namjene'] = $sifra_namjene;
	$podaci_2d_barcode['opis_placanja'] = $opis_placanja;
	$podaci_2d_barcode['iznos'] = $total['HRK']['total'];
	$podaci_2d_barcode['tip_izlaza'] = "data-url";
	$bs64data = base64_encode(json_encode($podaci_2d_barcode));
	$barcode_base64 = file_get_contents(BASE_URL."/2D-bar-code.php?d=".$bs64data);
	if(is_json($barcode_base64)){ // ako vrati json znači da se desila greska i greske su u JSON, normalno vrati base64, jpg ili png
		$generate_2d_barcode = false;
	}
}

/* logo */
$logo_path = $logo;
$logo_type = pathinfo($logo_path, PATHINFO_EXTENSION);
$logo_data = file_get_contents($logo_path);
$logo_base64 = 'data:image/' . $logo_type . ';base64,' . base64_encode($logo_data);

ob_start();
?><!DOCTYPE html>
<html>
	<head>
		<title>Racun</title>
		<meta http-equiv="Content-Type" content="text/html; charset=Windows-1250"/>
		<link rel="stylesheet" type="text/css" href="style/style.css" />
	</head>
	
<body>
	<script type='text/php'>
		if ( isset($pdf) ) { 
			$size = 9;
			$y = $pdf->get_height() - 30;
			$x = $pdf->get_width() - 60 - 9;
			$pdf->page_text($x, $y,'{PAGE_NUM}/{PAGE_COUNT}', 'helvetica', $size);
		} 
    </script>
    <table class="first-section" border="0">
		<tr>
			<td style="vertical-align: top;" >
				<p class="invoice-number"><?=$vrsta?>: <?=$broj_dokumenta?></p>
				<div class="dates">
					<div class="datum font-med">
						<span class="datum-1 font-normal">Datum kreiranja</span>
						<span class="date-value font-normal"><?=$datum_kreiranja?></span>
					</div>
					<?php if($datum_isporuke && $datum_isporuke!=""){ ?> 
					<div class="datum font-med">
						<span class="datum-1 font-normal ">Datum isporuke</span>
						<span class="date-value font-normal"><?=$datum_isporuke?></span>
					</div>
					<?php } ?>
					<?php if($rok_placanja && $rok_placanja!=""){ ?> 
					<div class="datum font-med">
						<span class="datum-1 font-normal">Rok plaćanja</span>
						<span class="date-value font-normal"><?=$rok_placanja?></span>
					</div>
					<?php } ?>
					<?php if($datum_racuna && $datum_racuna!=""){ ?> 
					<div class="datum font-med">
						<span class="datum-1 font-normal">Datum računa</span>
						<span class="date-value font-normal"><?=$datum_racuna?></span>
					</div>
					<?php } ?>
				</div>
			</td>
			<td style="vertical-align: top; text-align: right; " >
				<img src="<?=$logo_base64?>" class="logo" alt="<?=$izdavac_racuna_naziv?>" />
			</td> 
		</tr>
		<tr>
			<td style="vertical-align: top;">
				<div class="seller-info">
					<p class="font-small weight-small">Račun izdao :</p>
					<p class="font-normal">
						<b><?=$izdavac_racuna_naziv?></b>
					</p>
					<p class="font-small weight-small"><?=$izdavac_racuna_adresa.", ".$izdavac_racuna_postanski_broj_i_mjesto?></p>
					<p class="font-normal">OIB:<span class="weight-high"><b><?=$izdavac_racuna_oib?></b></span></p>
					<?php if($izdavac_racuna_id_agencije){ ?>
					<p class="font-normal">ID:<span class="weight-small"><?=$izdavac_racuna_id_agencije?></span></p>
					<?php } ?>					
				</div>
			</td>
			<td style="vertical-align: top; ">
				<div class="buyer-info">
					<p class="font-small weight-small">Kupac :</p>
					<p class="weight-high font-normal"><b><?=$kupac_naziv?></b></p>
					<p class="font-normal weight-small">
						<?=($kupac_adresa)? $kupac_adresa : ""?>
						<?=($kupac_adresa&&$kupac_postanski_broj_i_mjesto)? ", " : "" ?>
						<?=($kupac_postanski_broj_i_mjesto)? $kupac_postanski_broj_i_mjesto : "" ?></p>
					<p class="font-normal">
						<?php if($kupac_oib){  ?>
						OIB:<span class="weight-high"><b><?=$kupac_oib?></b></span>
						<?php } ?>
					</p>
				</div>
			</td>
		</tr>
		<tr>
			<td style="vertical-align: top; ">
    				<?php if($poslovnica_naziv){ ?>
    				<div class="office-and-owner">
    					<p>Poslovnica :</p>
    					<p><b><?=$poslovnica_naziv?></b></p>
    					<p ><?=$poslovnica_adresa.", ".$poslovnica_postanski_broj_i_mjesto?></p>
    					<p >Tel: <?=$poslovnica_broj_tel?></p>
    				</div>
    				<?php
    				}
    				if($iznajmljivac_naziv){ ?>
    				<div class="office-and-owner">
    					<p>Iznajmljivač :</p>
    					<p><b><?=$iznajmljivac_naziv?></b></p>
    					<p ><?=$iznajmljivac_adresa.", ".$iznajmljivac_postanski_broj_i_mjesto?></p>
    					<p >OIB: <?=$iznajmljivac_oib?></p>
    				</div>
    				<?php } ?>
			</td>
		</tr>
	</table>
	<!-- second section -->
	<table 	class="second-section products-table">
		<tr style=" padding: 4px 0; " >
			<td style="width: 40px; "> R.BR</td>
			<td> Opis proizvoda/usluge</td>
			<td class="align-center" style="width: 50px;"> Jed.</td>
			<td class="align-center" style="width: 50px;"> Kol.</td>
			<td class="align-right"  style="width: 70px;"> Jed. cijena</td>
			<td class="align-right"  style="width: 70px;"> Osnovica</td>
			<td class="align-center" style="width: 60px;"> Stopa <br/>PDV-a</td>
			<td class="align-center" style="width: 50px;"> Popust </td>
			<td class="align-right"  style="width: 92px;"> Iznos stavke</td>
		</tr>
		<?php
		
		foreach ($product_bank as $n => $p){
			$p_cijene = $p['product_prices'];
			?>
		<tr>
			<td> <?=$n+1?>. </td>
			<td> 
				<p class="item-name">
					<?=$p['name']?>
				</p>
				<p class="item-description light-color">
					<?=$p['description']?>
				</p>
			</td>
			<td class="align-center" style="width: 50px;" > 
				<?=$p['mesure']?>
			</td>
			<td class="align-center" style="width: 50px;"> 
				<?=$p['amount']?>
			</td>
			<td class="align-right"  style="width: 80px;"> 
				<?=$p_cijene[$vpo]['price_per_unit']?> <?=$vpo?>
				<?php if($vso){ ?>
				<br>
				<span class="price-eur"><?=$p_cijene[$vso]['price_per_unit']?> <?=$vso?></span>
				<?php 	
				} ?>
			</td>
			<td class="align-right" style="width: 80px;"> 
				<?=$p_cijene[$vpo]['osnovica']?> <?=$vpo?>
				<?php if($vso){ ?>
				<br>
				<span class="price-eur"><?=$p_cijene[$vso]['osnovica']?> <?=$vso?></span>
				<?php 	
				} ?>
			</td>
			<td class="align-center" style="width: 60px;">
				<?=$p['stopa_pdv']?>%
			</td>
			<td class="align-center" style="width: 50px;">
				<?=$p['popust']?>%
			</td>
			<td class="align-right"  style="width: 92px;"> 
				<?=$p_cijene[$vpo]['item_total']?> <?=$vpo?>
				<?php if($vso){ ?>
				<br>
				<span class="price-eur"><?=$p_cijene[$vso]['item_total']?> <?=$vso?></span>
				<?php 	
				} ?>
			</td>
		</tr>
		<?php  
		}
		?>
		<!-- ukupno -->
	</table>
	<hr class="total-line">
	<div class="total-container" style="page-break-inside: avoid; ">
		<div class="total-item-container"> 
			<span class="weight-high font-small total-item" >Osnovica: <?=($vso)? "<br>&nbsp;" : ""?></span>
			<span class="font-small total-price">
				<?=$billing['ukupno'][$vpo]['total_bez_pdv']?> <?=$vpo?>
				<?php if($vso){ ?> 
				<br /> <?=$billing['ukupno'][$vso]['total_bez_pdv']?> <?=$vso?>
				<?php } ?>
			</span>
		</div>
		<div class="clear-float"> </div>
	
		<?php  
		// sort pdv stope
		function assocArraySortByStopa($a, $b){
			if(!isset($a['stopa']) || !isset($b['stopa'])) return 0;
		    if ($a['stopa'] == $b['stopa']) return 0;
		    return ($a['stopa'] > $b['stopa']) ? -1 : 1;
		}
		usort($pdv_stope, "assocArraySortByStopa");

		for($i=0; $i<sizeof($pdv_stope); $i++){
            $display_stopa = ( $pdv_stope[$i]["stopa"] == 0 )? "Neoporezivo" : "Porez ". $pdv_stope[$i]["stopa"] . "%";
		?>
	 
		<div class="total-item-container"> 
			<span class="weight-high font-small total-item" >
				<?=$display_stopa?>, osnovica <?=$pdv_stope[$i]["iznos_osnovice"][$vpo]?> <?=$vpo?> : <?=($vso)? "<br>&nbsp;" : ""?> 
			</span>
			<span class="font-small total-price"> 
				<b><?=$pdv_stope[$i]["total"][$vpo]?> <?=$vpo?></b>
				<?php if($vso){ ?> 
				<br /> <?=$pdv_stope[$i]["total"][$vso]?> <?=$vso?>
				<?php } ?>
			</span>
		</div> 

		<div class="clear-float"> </div>
		<?php  
 		}
		?>
	

        <div class="total-item-container"> 
			<span class="weight-high font-small total-item" style="display:inline;" >
				Ukupno <?php if(!$has_advance) { ?> za platiti <?php } ?> : <?php if($vso){ ?><br />&nbsp; <?php } ?>
			</span>
			<span class="font-small total-price" style="display:inline-block; width:<?=(!$has_advance)? "225": "280"?>px;  " > 
			    <b>
				    <?=$billing['ukupno'][$vpo]['total']?> <?=$vpo?>
				</b>
				<?php if($vso) { ?> 
    				<br />
    				<a><?=$billing['ukupno'][$vso]['total']?> <?=$vso?></a>
				<?php } ?>
			</span>
		</div> 


		<div class="clear-float"> </div>
		<?php if($has_advance) { ?>

		<div class="total-item-container" > 
			<span class="weight-high font-small total-item" style="display:inline;" >Predujam: <?=($vso) ? "<br />&nbsp;" : "" ?></span>
			<span class="font-small total-price" style="display:inline-block; width:260px; text-align: right; " >
				<b><?php echo $billing['predujam']['total'][$vpo]['ukupno_predujam']; ?> <?=$vpo?></b>
				<?php if($vso){ ?> 
				<br>
				<?php echo $billing['predujam']['total'][$vso]['ukupno_predujam']; ?> <?=$vso?>
				<?php } ?>
			</span>
		</div>
		

		<div class="clear-float"> </div>
		<div class="total-item-container" > 
			<span class="weight-high font-small total-item" style="display:inline;" >Ukupno za platiti: <?=($vso)? "<br />&nbsp;" : "" ?></span>
			<span class="font-small total-price" style="display:inline-block; width:230px; text-align: right;" >
				<b><?=$billing['ostatak_za_uplatu'][$vpo]?> <?=$vpo?></b>
				<?php if($vso){ ?> 
				<br><?=$billing['ostatak_za_uplatu'][$vso]?> <?=$vso?>
				<?php } ?>
			</span>
		</div>
		<div class="clear-float"> </div>
		<?php } ?>
	</div>
	<!-- podaci za plaćanje -->
	<div class="payment" style="page-break-inside: avoid;" >
		<div class="payment-numbers-container">
			<?php if($nacin_placanja) {?>
			<p class="font-normal ">
				<span class="font-small weight-small">Način plaćanja:</span> <?=$nacin_placanja?> <?=($kreditna_kartica)? "(" . $kreditna_kartica. ")" :""?> 
			</p>
			<?php } ?>
			<p class="font-normal ">
				<span class="font-small weight-small">Operater:</span> <?=$racun_izdao?>   <br>
			</p>
			<?php if($zki && $jir){ ?> 
			<p class="font-small zki">
				<span class="font-small weight-small">ZKI:</span>  <?=$zki?>  <br>
			</p>
			<p class="font-small jir">
				<span class="font-small weight-small">JIR :</span> <?=$jir?>
			</p>
			<?php } ?>
			<?php 
			if($zki && $jir){ 
    			$qr_code_url = "https://chart.googleapis.com/chart?cht=qr&chld=H&chs=200x200&chl=https%3A%2F%2Fporezna.gov.hr%2Frn%3F";
                $qr_code_time = DateTime::createFromFormat("d.m.Y H:i", $datum_kreiranja)->format("Ymd_Hi");
                $qr_code_total = str_replace([",","."],["",","],$billing['ostatak_za_uplatu']['HRK']);
                $qr_code_url_final = $qr_code_url . "jir%3D" . $jir . "%26datv%3D" . $qr_code_time . "%26izn%3D" . $qr_code_total;
                $b64_qr_code_image = "data:image/png;base64,". base64_encode(file_get_contents($qr_code_url_final));
			?> 
			<div>
			    <img src="<?=$b64_qr_code_image?>" style="width: 120px; height: 120px;" />
			</div>
			<?php } ?>
		</div>
		<p class="font-normal">
			<span class="font-small weight-small">Napomene:<br> <?=$napomena_placanja?></span>
		</p>
		<?php if($generate_payment_data) { ?> 
		<p class="font-small payment-numbers">Podaci za uplatu</p>
		<div class="payment-numbers-container">
			<p class="font-normal">
				<span class="font-small weight-small">IBAN:</span> <?=$iban?>  
				<?php if($poziv_na_broj){ ?>
				<span class="font-small weight-small">Poziv na broj:</span> <?=$poziv_na_broj?><br> 
				<?php } ?>
			</p>
			<p class="font-normal">
				<span class="font-small weight-small">SWIFT:</span> <?=$swift?>
			</p>
			<?php if($generate_2d_barcode){ ?> <p class="font-normal weight-high">2D bar kod</p> <?php } ?>
		</div>
		<?php } ?>

		<div style="margin-top: 20px;" >
			<?php if($generate_2d_barcode){ ?>
			<img height="76" src="<?=$barcode_base64?>" />
			<?php } ?>
			<?php if($generate_signing_spot){ ?>
			<div class="singning-spot">
				Mjesto za potpis
			</div>
			<div style="display: inline-block; height: 27px;"> &nbsp; </div>
			<?php } ?>
		</div>
	</div>
	<div class="company-info" style="page-break-inside: avoid;">
		<table class="first-info-line font-xsmall">
			<tr>
				<td>
					Naše adrese<br>
					Sjedište: <?=$opci_podaci_sjediste?><br>
					Internet: <?=$opci_podaci_web_stranica?>
				</td>
				<td>
					Pravni podaci:<br>
					OIB: <?=$izdavac_racuna_oib?><br>
					IBAN: <?=$iban?><br>
					SWIFT: <?=$swift?>
				</td>
				<td>
					Kontakt<br>
					Telefon: <?=$opci_podaci_telefon?><br>
					Mobilni: <?=$opci_podaci_mobilni?><br>
					E-mail: <?=$opci_podaci_email?>
				</td>
			</tr>
		</table>
		<p class="font-xsmall bottom-disclaimer">
			<?=$opci_podaci_disclaimer?>
		</p>
	</div>
</body>
</html>
<?php

$html = ob_get_clean();
// ======= generiraj PDF ========
if(!isset($_POST['debug'])){

	$options = new Dompdf\Options();
	$options->set('isHtml5ParserEnabled', true);
	$options->set('isPhpEnabled', true);

	$dompdf = new Dompdf\Dompdf($options);
	$dompdf->loadHtml($html);
	$dompdf->setPaper('A4', 'portrait');
	$dompdf->render();
	$file_name = $izdavac_racuna_oib."-".str_replace([" ",".","/","\\"], "-", $broj_dokumenta);
	$dompdf->stream($file_name);

} else {
	echo $html;
}

?>