如何从php文件“制作”pdf文件?

Lets say I have the following file:

layout.php

<?php
session_start();
?>
<table width="700" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th>Id</th>
    <th>Nombre</td>
  </tr>
  <tr>
    <td><?php echo $_SESSION['id']; ?></td>
    <td><?php echo $_SESSION['nombre']; ?></td>
  </tr>
</table>

Is there a way where I can download that file as a PDF that will output the file like you see it in the browser?

I tried this but it didnt work:

<?php
session_start();

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=info_user.pdf");
readfile('layout.php');
?>

There are several packages that allow you to generate PDF files. You can have a look at these:

Chrome can save a web page to PDF, if that helps you.

The PDFlib library of PHP is probably the most developed library for this.

You can read about them here on PHP dev.

Other alternate libraries include: