Need help in converting PDF

Well, in the prior link, they reference dompdf and that looks promising…
http://dompdf.github.io/

can you please provide the example code in which, a table to be displayed which is queried from sql database. using dompdf :frowning:

From https://code.google.com/p/dompdf/wiki/Usage

Note: I haven’t actually tested this code…

<?php
ob_start();
include 'DBConnect.php';
require("dompddf_config.inc.php"); // may need to require dompdf.php too?

$query = "Select * from tb_php_sql";
$result = mysql_query($query);
$string = '<html><body><form><table><tr>';


$string .= '<td align="left"><table border="1" width="33%" ><tr bgcolor="#4472c4" align="center"><th><font color="white">ID</font></th><th><font color="white">Name</font></th><th><font color="white">Phone</font></th></tr>';
while($row= mysql_fetch_array($result))
{
$string .= '<tr>';
$string .= '<td>'. $row['id']. '</td>';
$string .= '<td>'. $row['Name']. '</td>';
$string .= '<td>'. $row['Phone']. '</td>';
$string .= '</tr>';
}
$string .= '</table></td>';

$query1 = "Select * from tb_php_sql1";
$result1 = mysql_query($query1);

$string .= '<td align="right"><table border="1" width="33%" align="right" margin-top="80px"><tr bgcolor="#4472c4"><th><font color="white">First Name</font></th><th><font color="white">Last Name</font></th><th><font color="white">Age</font></th></tr>';
while($row1= mysql_fetch_array($result1))
{
$string .= '<tr>';
$string .= '<td>'. $row1['SalesOrder']. '</td>';
$string .= '<td>'. $row1['SalesRep']. '</td>';
$string .= '<td>'. $row1['Location']. '</td>';
$string .= '</tr>';
}
$string .='</table></td>';

$string .= '</tr></table></form></body></html>';

$string = nl2br($string);
echo $string;

$toWrite = ob_get_contents();
ob_end_clean();

$html = new DOMPDF();
$html->load_html($toWrite);
$html->render();
header('Content-type: application/pdf');
$html->stream('pdfformat.pdf');

?>

When I run this application, the following issues are coming, but the output is saving in pdf.

Warning: require(dompddf_config.inc.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\SIVA_DOMPDF\pdf_1.php on line 4

Fatal error: require() [function.require]: Failed opening required ‘dompddf_config.inc.php’ (include_path=‘.;C:\xampp\php\PEAR’) in C:\xampp\htdocs\SIVA_DOMPDF\pdf_1.php on line 4

where is the dompdf_config.inc.php file in relation to your pdf_1.php file?

Both are in same location

Oh, I see. I mis-spelled the file name. change it to dompdf_config.inc.php (right now it has dompddf_config.inc.php – an extra ‘d’ in there)

Thank u so much…

If u don’t mind, I have few more questions regarding database and all.

how can I get a one value from one column from one row from sql database and display it on the table using dompdf itself.

Can you give me more detail to your question? I’m not 100% certain I follow it.

I have a table in my DB in which I have many columns, from there I need to get only a single column value which is to be shown on the table.

Well dompdf is designed to work with HTML output specifically. So I’d start with generating an HTML page that looks like what you want your PDF page to look like, and then you send that HTML into dompdf to generate your PDF.

Does that answer your question?

in the previous code I used while loop to get all the data from the database. In the same way I need to display only a single value

Ah, now I’m on the same page (just omit the while loop, but use the same condition):

$query = "Select my_column from tb_php_sql";
$result = mysql_query($query);
$row= mysql_fetch_array($result);
$html .= "<p>" . $row['my_column'] . "</p>";

can I have styles to that table??

I need to adjust the table 30px from top;

dompdf states it does support CSS 2.1 so you should be able to add style=“margin-top:30px;” to the table

when I append style=“margin-top:30px;” to the table, there is no change in my output pdf :frowning:

maybe try margin: 30px 0 0 0 on the table? I’m trying to google a solution, but I haven’t found anything yet.

I have tried this also… unfortunately there is no change in the table… :frowning:

Can you just put a <br /> or empty <p></p> tags above the table to get the effect you want?