HTML to PDF, dynamic document generation

reference

HTML to PDF

composer require dompdf/dompdf

require 'vendor/autoload.php';  
  
use Dompdf\Dompdf;  
   
$dompdf = new Dompdf();  
  
// load html
$html = file_get_contents('your_html_file.html');  

$dompdf->loadHtml($html);  
  
// dimension and direction  
$dompdf->setPaper('A4', 'portrait');  
  
// render
$dompdf->render();  

Dynamic

ob_start(); 
include __DIR__ . '/document.php'; 
$html = ob_get_clean();

Output

//get the content
$content = $dompdf->output();

// output
file_put_contents('./target/output.pdf', $content);

External Stylesheet

to correctly apply external stylesheet, use system path

$dompdf->getOptions()->setChroot(__DIR__);
<link href="./dist/style.css" rel="stylesheet">