Stack Exchange
Dompdf 0.7.0 - Fail to generate PDF file from HTML-PHP dynamic code
I am developing a commercial system that needs to generate receipts and send them to a printer. I am trying to generate it via calling the file that contains the code below via JQuery (the whole code is into an only file). Is as follows:
First part (PHP)
<?php
ob_start();
$conn = mysqli_connect('localhost','root','','dbname');
mysqli_query($conn,"SET CHARACTER SET UTF8"); 
setlocale(LC_TIME, "Spanish_Colombia");
date_default_timezone_set("America/Bogota");
    $codrc=$_REQUEST["codrc"];
    $sqlRD="SELECT p.product_name,rd.units,rd.unit_cost,df.subtotal
                FROM product p, receipt_detail rd, receipt r
                WHERE p.product_code=df.product_code AND r.receipt_code=rd.receipt_code
AND df.receipt_code=".$codrc;
    $queryRD=mysqli_query($conn,$sqlRD);
    
    $sqlR="SELECT rec_date_time,total FROM receipt WHERE receipt_code=".$codrc;
    $queryR=mysqli_query($conn,$sqlR);
    $fetchR=mysqli_fetch_assoc($queryR);
    $t=strtotime($fetchR["rec_date_time"]);
?>

Second part (HTML-PHP/Obtaining results of the query, it works):
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
</head>
<body>
    <h3 align="center">ENTERPRISE NAME</h3>
  <p align="center">LEGAL REGISTRY CODE</p>
  
  <p align="center">Date and time:<?php echo utf8_encode(strftime("%A, %d de %B de %Y - %I:%M %p",$t)); ?></p>
  <table border="0">
    <tr>
        <td>Product</td>
      <td>Unit cost</td>
      <td>Units</td>
      <td>Subtotal</td>
    </tr>
   <?php while($fetchDF=mysqli_fetch_assoc($queryDF)){ ?>
    <tr>
        <td><?php echo $fetchRD["product_name"]; ?></td>
      <td><?php echo $fetchRD["unit_cost"]; ?></td>
      <td><?php echo $fetchRD["units"]; ?></td>
      <td><?php echo $fetchRD["subtotal"]; ?></td>
    </tr>
   <?php } ?>
    <tr colspan="4" align="right"><td>No-tax total: $<?php echo $fetchR["total"]*.84; ?></td></tr>
    <tr colspan="4" align="right"><td>Tax: $<?php echo $fetchR["total"]*.16; ?></td></tr>
    <tr colspan="4" align="right"><td>Total: $<?php echo $fetchR["total"]; ?></td></tr>
  </table>
  <p align="center">Thank you for buying, see you soon!</p>
</body>
</html>

Third part (Dompdf 0.7.0, here is the problem):
<?php
    require_once('dompdf/autoload.inc.php');
    use Dompdf\Dompdf;
    
    $dompdf=new Dompdf();
    $dompdf->set_option('defaultFont','Helvetica');
    
    $dompdf->loadHtml(ob_get_clean());
    //'a4' just a test of paper size...
    $dompdf->setPaper('a4','landscape');
    
    $dompdf->render();
    $pdf=$dompdf->output();
    $filename="receipt".$codrc; 
    $dompdf->stream($filename); 
?>

Now, the only response that I get in the console is a messy string. So, some questions arise:
Output buffering is neccesary to obtain the data?
I researched this question,but I found out it uses an older version of the Dompdf library. Anyone knows if any change from 0.5.x to 0.7.0 is affecting the result? Or should I test with an older version of Dompdf?
Is there anyway to directly send the generated PDF to a ticket printer?
Would appreciate very much any answer, as soon as possible. Thank you very much.

Look at the syntax highlighting in your first PHP snippet. It's wrong; you have a missing quote. Please make sure the code in your question accurately reflects your question. We have no way of knowing what mistakes are significant.

Note: "fecha_hora" on the last line of the first part of the code is really "rec_date_time"... I converted to english almos all the field names (the project and the database are intended to be in Spanish...) Sorry for the error.

Chris:all right, I missed a quote...

Corrected code, @Chris ... The error comes on the third part... The first and second parts of the code are working well, I have already tested them several times.

What does "the only response that I get in the console is a messy string" mean? What does that string look like?

@Chris like this...
JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2VzIDMgMCBSID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+ ... and soooo on...

It looks like $dompdf->stream() compresses its payload and prompts the browser to open a download dialog by default. Have you tried accessing your code in a web browser instead of the console?

Maybe I am explaining myself wrong, @Chris. I am executing the files into the web browser. The "console" is the one in Developer Tools. And another issue is the fact that the browser DOES NOT open a download dialog by default.

@Chris In normal circumstances, plain HTML works with the third part of the code... But when I am trying to get the output from a PHP file, by some reason it doesn't work. I am trying to use ob_start() and ob_get_clean to get the output, but I don't know if there is another way to generate the PDF file without using stream().
