php save webpage pdf code example
Example 1: php create pdf
<?php
use Dompdf\Dompdf;
session_start();
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$html = '<table border=1>';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>ID</th>';
$html .= '<th>Collum1</th>';
$html .= '<th>Collum2</th>';
$html .= '<th>Collum3</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
$sql = "SELECT * FROM tableName";
$sql = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($sql)){
$html = '';
}
require_once("dompdf/autoload.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html('
<h1 style="text-align: center;">RentCar</h1>
'. $html .'
');
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream(
"form.pdf",
array(
"Attachment" => false
)
);
?>
Example 2: add page nos. to existing pdf in php
<?php
require_once('TCPDF/tcpdf.php');
require_once('FPDI/fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile('existing_file.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Courier', '', 10);
$pdf->SetXY(20, 10);
$pdf->Write(0, "Text to be stamped");
$pdf->Output("output_sample.pdf", "D");
?>