How can PHP be used to generate a PDF on the server and attach it to an email for printing purposes?

To generate a PDF on the server and attach it to an email for printing purposes using PHP, you can use libraries like TCPDF or FPDF to create the PDF file. Once the PDF is generated, you can use PHPMailer or the built-in mail function in PHP to send an email with the PDF file attached.

// Include the TCPDF library
require_once('tcpdf/tcpdf.php');

// Create new PDF document
$pdf = new TCPDF();

// Add content to the PDF document
$pdf->SetTextColor(0, 0, 0);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello, this is a PDF document generated using PHP!', 0, 1, 'C');

// Save the PDF to a file
$pdfFilePath = 'path/to/save/pdf/document.pdf';
$pdf->Output($pdfFilePath, 'F');

// Send email with the PDF attachment
require 'PHPMailer/PHPMailer.php';
$mail = new PHPMailer();
$mail->AddAttachment($pdfFilePath);
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'PDF Attachment';
$mail->Body = 'Please find the attached PDF for printing purposes.';
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_smtp_username';
$mail->Password = 'your_smtp_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->send();