title: FPDF WriteHTML method - how to reduce line-height of <p> tag
body: <p>I got stuck and would appreciate your help.</p>
  
  <p>I'm revamping parts of an existing web-application. Currently I'm on generating a PDF out of a php form.</p>
  
  <p>I'm using the FPDF library but have first done it very static (no automated line breaks with multi-cells, etc.). Now the request came in to render HTML.</p>
  
  <p>Since nearly the whole PDF works I would rather not switch to another library, so I've used the WriteHTML extension for FPDF as mentioned here:
  <a href="http://www.fpdf.org/en/script/script42.php" rel="nofollow noreferrer">http://www.fpdf.org/en/script/script42.php</a></p>
  
  <p>Everything works fine, but when a user is entering data for the PDF (invoice rows) he's doing it via a WYSIWYG editor.</p>
  
  <p>Now, if the user hits the 'return' key, it will wrap the lines with a HTML <p> tag.</p>
  
  <p>For example:
  "Test 1
  Test 2"</p>
  
  <p>renders as:</p>
  
  <pre><code>&lt;p&gt;Test 1&lt;/p&gt;&lt;p&gt;Test 2&lt;/p&gt;.
  </code></pre>
  
  <p>So far all good as the output as it is correct. However, the line-height, margin, padding or whatever it is, is too high so the linbreak between 2 lines is too much. I was not able to reduce it in any way.</p>
  
  <p>I've attached a screenshot to explain further.</p>
  
  <p><a href="https://i.stack.imgur.com/yAz0b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yAz0b.png" alt="Invoice-Snippet"></a>
  The code below shows the little addon I'm using for FPDF to use the WriteHTML method. As the line-breaks are only that high for the content generate by this method. When using the Text() method, or a cell() the line height is not that hight since I'm using GetY(), SetY() methods to determine the start of a line, so I'm pretty sure I need to change something within the following provided code, but I'm unsure what.</p>
  
  <pre><code>&lt;?php
  //HTML2PDF by Clément Lavoillotte
  //ac.lavoillotte@noos.fr
  //webmaster@streetpc.tk
  //http://www.streetpc.tk
  
  require('fpdf.php');
  
  //function hex2dec
  //returns an associative array (keys: R,G,B) from
  //a hex html code (e.g. #3FE5AA)
  function hex2dec($couleur = "#000000"){
      $R = substr($couleur, 1, 2);
      $rouge = hexdec($R);
      $V = substr($couleur, 3, 2);
      $vert = hexdec($V);
      $B = substr($couleur, 5, 2);
      $bleu = hexdec($B);
      $tbl_couleur = array();
      $tbl_couleur['R']=$rouge;
      $tbl_couleur['V']=$vert;
      $tbl_couleur['B']=$bleu;
      return $tbl_couleur;
  }
  
  //conversion pixel -&gt; millimeter at 72 dpi
  function px2mm($px){
      return $px*25.4/72;
  }
  
  function txtentities($html){
      $trans = get_html_translation_table(HTML_ENTITIES);
      $trans = array_flip($trans);
      return strtr($html, $trans);
  }
  ////////////////////////////////////
  
  class PDF_HTML extends FPDF
  {
  //variables of html parser
  protected $B;
  protected $I;
  protected $U;
  protected $HREF;
  protected $fontList;
  protected $issetfont;
  protected $issetcolor;
  
  function __construct($orientation='P', $unit='mm', $format='A4')
  {
      //Call parent constructor
      parent::__construct($orientation,$unit,$format);
      //Initialization
      $this-&gt;B=0;
      $this-&gt;I=0;
      $this-&gt;U=0;
      $this-&gt;HREF='';
      $this-&gt;fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol');
      $this-&gt;issetfont=false;
      $this-&gt;issetcolor=false;
  }
  
  function WriteHTML($html)
  {
      //HTML parser
      $html=strip_tags($html,"&lt;b&gt;&lt;u&gt;&lt;i&gt;&lt;a&gt;&lt;img&gt;&lt;p&gt;&lt;br&gt;&lt;strong&gt;&lt;em&gt;&lt;font&gt;&lt;tr&gt;&lt;blockquote&gt;"); //supprime tous les tags sauf ceux reconnus
      $html=str_replace("\n",' ',$html); //remplace retour à la ligne par un espace
      $a=preg_split('/&lt;(.*)&gt;/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //éclate la chaîne avec les balises
      foreach($a as $i=&gt;$e)
      {
          if($i%2==0)
          {
              //Text
              if($this-&gt;HREF)
                  $this-&gt;PutLink($this-&gt;HREF,$e);
              else
                  $this-&gt;Write(5,stripslashes(txtentities($e)));
          }
          else
          {
              //Tag
              if($e[0]=='/')
                  $this-&gt;CloseTag(strtoupper(substr($e,1)));
              else
              {
                  //Extract attributes
                  $a2=explode(' ',$e);
                  $tag=strtoupper(array_shift($a2));
                  $attr=array();
                  foreach($a2 as $v)
                  {
                      if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
                          $attr[strtoupper($a3[1])]=$a3[2];
                  }
                  $this-&gt;OpenTag($tag,$attr);
              }
          }
      }
  }
  
  function OpenTag($tag, $attr)
  {
      //Opening tag
      switch($tag){
          case 'STRONG':
              $this-&gt;SetStyle('B',true);
              break;
          case 'EM':
              $this-&gt;SetStyle('I',true);
              break;
          case 'B':
          case 'I':
          case 'U':
              $this-&gt;SetStyle($tag,true);
              break;
          case 'A':
              $this-&gt;HREF=$attr['HREF'];
              break;
          case 'IMG':
              if(isset($attr['SRC']) &amp;&amp; (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) {
                  if(!isset($attr['WIDTH']))
                      $attr['WIDTH'] = 0;
                  if(!isset($attr['HEIGHT']))
                      $attr['HEIGHT'] = 0;
                  $this-&gt;Image($attr['SRC'], $this-&gt;GetX(), $this-&gt;GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
              }
              break;
          case 'TR':
          case 'BLOCKQUOTE':
          case 'BR':
              $this-&gt;Ln(5);
              break;
          case 'P':
              $this-&gt;Ln(10);
              break;
          case 'FONT':
              if (isset($attr['COLOR']) &amp;&amp; $attr['COLOR']!='') {
                  $coul=hex2dec($attr['COLOR']);
                  $this-&gt;SetTextColor($coul['R'],$coul['V'],$coul['B']);
                  $this-&gt;issetcolor=true;
              }
              if (isset($attr['FACE']) &amp;&amp; in_array(strtolower($attr['FACE']), $this-&gt;fontlist)) {
                  $this-&gt;SetFont(strtolower($attr['FACE']));
                  $this-&gt;issetfont=true;
              }
              break;
      }
  }
  
  function CloseTag($tag)
  {
      //Closing tag
      if($tag=='STRONG')
          $tag='B';
      if($tag=='EM')
          $tag='I';
      if($tag=='B' || $tag=='I' || $tag=='U')
          $this-&gt;SetStyle($tag,false);
      if($tag=='A')
          $this-&gt;HREF='';
      if($tag=='FONT'){
          if ($this-&gt;issetcolor==true) {
              $this-&gt;SetTextColor(0);
          }
          if ($this-&gt;issetfont) {
              $this-&gt;SetFont('arial');
              $this-&gt;issetfont=false;
          }
      }
  }
  
  function SetStyle($tag, $enable)
  {
      //Modify style and select corresponding font
      $this-&gt;$tag+=($enable ? 1 : -1);
      $style='';
      foreach(array('B','I','U') as $s)
      {
          if($this-&gt;$s&gt;0)
              $style.=$s;
      }
      $this-&gt;SetFont('',$style);
  }
  
  function PutLink($URL, $txt)
  {
      //Put a hyperlink
      $this-&gt;SetTextColor(0,0,255);
      $this-&gt;SetStyle('U',true);
      $this-&gt;Write(5,$txt,$URL);
      $this-&gt;SetStyle('U',false);
      $this-&gt;SetTextColor(0);
  }
  
  }//end of class
  ?&gt;
  </code></pre>
  
  <p>And I'm calling it via </p>
  
  <pre><code>$pdf-&gt;WriteHTML($value['description']);
  
  $value['description'] = &lt;p&gt;First line&lt;/p&gt;&lt;p&gt;New Line&lt;/p&gt;
  </code></pre>
  
  <p>Could somebody please help me how I can reduce the line break height between those lines? Appreciate any help.</p>
  
  <p>Thanks</p>