title: HTML to PDF when TCPDF base on foreach and while
body: <p>This might have been already answered, yes, I used Google, and the search here stackoverflow, but the problem remains...</p>
  
  <p>I'm using TCPDF library to generate PDF file, it works just fine, but I have the following situation, in order for my to generate a PDF ot just the HTML I need to put a few foreach and while's and IF's inside the HTML so that I can get the layout that the user is requesting...</p>
  
  <p>so...<br></p>
  
  <pre><code>if(isset($_POST['submit'])) {
  $ids = htmlentities($_POST['id'], ENT_NOQUOTE;
  $con = conectionDB();
  $query 'SELECT * FROM books WHERE id = "$ids"';
  $doit = $con-&gt;query($query);
  // at this point everything is file
  // a few if's and we are done with the fetching "books" data
  // a few other tuff that is required from the library nothing fancy...
  // now here is the hard part
  // next line will build my layout to display my PDF
  $build_HTML = &lt;&lt;&lt;EOD
  &lt;style&gt;
  .clasess {...}
  .nother {...}
  &lt;/style&gt;
  &lt;table width="100%" border="1" cellpadding="0" bordercolor="#333333" &gt;
  &lt;tr&gt;
  &lt;td&gt;Author&lt;/td&gt;
  &lt;td&gt;Books&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt; Jong &lt;/td&gt;
  &lt;td&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;Title&lt;/td&gt;&lt;td&gt;Year&lt;/td&gt;
  &lt;/tr&gt;
  // Here is the problem I need to put a query to fetch the related data from
  // another table
  $books = conectionDB();
  $bookQ = "SELECT * FROM titles WHERE name = '$author_name'";
  $doitTitles = $books-&gt;query($bookQ);
  if ($doitTitles-&gt;num_row &gt; 1) {
  while($dos = $doitTitles-&gt;fetch_assoc()){
  // my td's, but this doest work...
  }
  }
  &lt;/table&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  EOD;
  $pdf-&gt;writeHTML($build_HTML, true, false, false, false, '');
  } else {
  // Go back...
  }
  </code></pre>
  
  <p>As you can see I need that query right there, you may have notice that I have <strong>$doitTitles->num_row > 1</strong> why? because if there is more than 1 tittle the layout would different if there is only 1 record...</p>
  
  <p>We know that, that wont work, so the question is, is there another way to do that?<br>
  now, before the user go to the PDF, I display the information in plain html, which mean that the HTML that I use before the user go in to the PDF will be the same... so I was wondering, there another library that I can use to render the HTML in to PDF instead of building the PDF from inside the file... the user is able to see the result so those result just put 'em in a PDF...</p>
  
  <p>Thank you!</p>