/**
  * 
  * Create a pdf with the labels
  *
  */
 function createPdf()
 {
     global $conf, $mysoc, $db, $langs;
     $langs->load("other");
     require_once DOL_DOCUMENT_ROOT . '/includes/tcpdf/tcpdf.php';
     require_once DOL_DOCUMENT_ROOT . "/product/class/product.class.php";
     require_once DOL_DOCUMENT_ROOT . "/core/lib/product.lib.php";
     $pdf = new TCPDF();
     $pdf->SetFont('dejavusans', '', 10);
     $lab_start = $conf->global->LAB_START;
     if ($conf->global->MAIN_MODULE_LABELPRINT_LABELS_0) {
         $PosY = 5 + floor($lab_start / 3) * 36;
     } else {
         $PosY = 4 + floor($lab_start / 3) * 37;
     }
     //$PosY=3;
     $PosX = 5 + $lab_start % 3 * 70;
     //$PosX=5;
     $Line = 5;
     // define barcode style
     $style = array('position' => '', 'align' => 'C', 'stretch' => false, 'fitwidth' => true, 'cellfitalign' => '', 'border' => false, 'hpadding' => 'auto', 'vpadding' => 'auto', 'fgcolor' => array(0, 0, 0), 'bgcolor' => false, 'text' => true, 'font' => 'helvetica', 'fontsize' => 8, 'stretchtext' => 4);
     //First page
     $pdf->AddPage();
     $sql = "SELECT fk_product, qty, price_level";
     $sql .= " FROM " . MAIN_DB_PREFIX . "labelprint";
     $sql .= " WHERE entity=" . $conf->entity;
     $resql = $db->query($sql);
     if ($resql) {
         $num = $db->num_rows($resql);
         $i = 0;
         while ($i < $num) {
             $objp = $db->fetch_object($resql);
             $product = new Product($db);
             $product->fetch($objp->fk_product);
             $qty = $objp->qty;
             $n = 0;
             while ($n < $qty) {
                 //Position X
                 $PosXLabel = $PosX < 70 ? $PosX : $PosX - 3;
                 //Soc Name
                 $pdf->SetFont('dejavusans', 'B', 10);
                 $pdf->SetXY($PosXLabel, $PosY);
                 $pdf->SetFillColor(230, 230, 230);
                 if ($conf->global->LAB_COMP) {
                     $pdf->MultiCell(65, 5, dol_trunc($mysoc->nom, 25), 0, 'L');
                     $flag = 1;
                 } elseif ($conf->global->LAB_PROD_LABEL) {
                     $pdf->MultiCell(65, 5, dol_trunc($product->libelle, 25), 0, 'L');
                     $flag = 2;
                 } else {
                     $pdf->MultiCell(65, 5, dol_trunc($conf->global->LAB_FREE_TEXT, 25), 0, 'L');
                     $flag = 3;
                 }
                 $pdf->SetFont('dejavusans', '', 10);
                 //Position Y
                 $PosYLabel = $PosY + $Line + 2;
                 //Product label
                 $pdf->SetXY($PosXLabel, $PosYLabel);
                 if ($flag == 1) {
                     if ($conf->global->LAB_PROD_LABEL) {
                         $pdf->Cell(25, 5, dol_trunc($product->libelle, 25), 0, 0, 'L');
                     } else {
                         $pdf->Cell(25, 5, dol_trunc($conf->global->LAB_FREE_TEXT, 25), 0, 0, 'L');
                     }
                 }
                 if ($flag == 2) {
                     $pdf->Cell(25, 5, dol_trunc($conf->global->LAB_FREE_TEXT, 25), 0, 0, 'L');
                 } else {
                     $pdf->Cell(25, 5, "", 0, 0, 'L');
                 }
                 //$pdf->Cell(25,5,dol_trunc($product->libelle,25),0,0,'L');
                 //$pdf->Write($Line,dol_trunc($product->libelle,25));
                 $PosYLabel = $PosYLabel + $Line + 2;
                 $pdf->SetXY($PosXLabel, $PosYLabel);
                 //Barcode
                 if ($conf->barcode->enabled) {
                     $product->fetch_barcode();
                     $pdf->write1DBarcode($product->barcode, $product->barcode_type_code, '', '', 35, 18, 0.4, $style, 'N');
                 }
                 //Price
                 $pdf->SetFont('dejavusans', 'B', 10);
                 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
                     $labelPrice = price($product->price_ttc);
                 } else {
                     $labelPrice = price($product->multiprices_ttc[$objp->price_level]);
                 }
                 $pdf->SetXY($PosXLabel + 38, $PosYLabel);
                 $pdf->Cell(25, 5, $labelPrice, 0, 0, 'R');
                 $PosYLabel = $PosYLabel + $Line + 1;
                 $labelPrice = $langs->trans(currency_name($conf->currency));
                 $pdf->SetXY($PosXLabel + 38, $PosYLabel);
                 $pdf->Cell(25, 5, $labelPrice, 0, 0, 'R');
                 $PosYLabel = $PosYLabel + $Line;
                 if ($conf->global->LAB_WEIGHT) {
                     $labelSet = $product->weight;
                     $labelSet .= " " . measuring_units_string($product->weight_units, "weight");
                 } elseif ($conf->global->LAB_LENGTH) {
                     $labelSet = $product->length;
                     $labelSet .= " " . measuring_units_string($product->length_units, "size");
                 } elseif ($conf->global->LAB_AREA) {
                     $labelSet = $product->surface;
                     $labelSet .= " " . measuring_units_string($product->surface_units, "surface");
                 } elseif ($conf->global->LAB_VOLUME) {
                     $labelSet = $product->volume;
                     $labelSet .= " " . measuring_units_string($product->volume_units, "volume");
                 } elseif ($conf->global->LAB_COUNTRY) {
                     $labelSet = getCountry($product->country_id, '', 0, $langs, 0);
                 }
                 $pdf->SetXY($PosXLabel + 38, $PosYLabel);
                 $pdf->Cell(25, 5, $labelSet, 0, 0, 'R');
                 $pdf->SetAutoPageBreak(false);
                 $PosX = $PosX + 70;
                 if ($PosX >= 200) {
                     $PosX = 5;
                     if ($conf->global->MAIN_MODULE_LABELPRINT_LABELS_0) {
                         $PosY = $PosY + 36;
                     } else {
                         $PosY = $PosY + 37;
                     }
                     if ($PosY >= 265) {
                         if ($conf->global->MAIN_MODULE_LABELPRINT_LABELS_0) {
                             $PosY = 5;
                         } else {
                             $PosY = 4;
                         }
                         $pdf->AddPage();
                     }
                 }
                 $n++;
             }
             $i++;
         }
     }
     ini_set('display_errors', 'Off');
     $buf = $pdf->Output("", "S");
     $len = strlen($buf);
     //$file_temp = ini_get("session.save_path")."/".dol_now().".pdf";
     $file_temp = DOL_DATA_ROOT . "/" . dol_now() . ".pdf";
     $gestor = fopen($file_temp, "w");
     fwrite($gestor, $buf);
     fclose($gestor);
     $url = dol_buildpath("/labelprint/download.php", 1) . "?file=" . $file_temp;
     print "<meta http-equiv='refresh' content='0;url=" . $url . "'>";
 }