// generate_pdf.php require_once('vendor/autoload.php'); require_once('barcode/BarcodeGenerator.php'); require_once('barcode/BarcodeGeneratorPNG.php'); require_once('config.php'); // Include the database configuration use TCPDF; use Picqer\Barcode\BarcodeGeneratorPNG; // Input from form $organization = $_POST['organization']; $application = $_POST['application']; $audit_type = $_POST['audit_type']; $url = $_POST['url']; $testing_date = $_POST['testing_date']; $revalidation_date = $_POST['revalidation_date']; $report_id = $_POST['report_id']; $issued_on = $_POST['issued_on']; $certificate_number = $_POST['certificate_number']; $conclusion = $_POST['conclusion']; $recommendations = $_POST['recommendations']; // Generate a unique 128-length random key $unique_key = bin2hex(random_bytes(64)); // 128 characters (64 bytes) // Generate the barcode $generator = new BarcodeGeneratorPNG(); $barcode = $generator->getBarcode($unique_key, BarcodeGeneratorPNG::TYPE_CODE_128); // Insert the certificate details into the database try { $stmt = $pdo->prepare("INSERT INTO certificates (organization, application, audit_type, url, testing_date, revalidation_date, report_id, issued_on, certificate_number, conclusion, recommendations, verification_key) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([ $organization, $application, $audit_type, $url, $testing_date, $revalidation_date, $report_id, $issued_on, $certificate_number, $conclusion, $recommendations, $unique_key ]); // Get the last inserted certificate ID (if needed for other purposes) $certificate_id = $pdo->lastInsertId(); } catch (PDOException $e) { die("Error saving certificate to the database: " . $e->getMessage()); } // Initialize TCPDF $pdf = new TCPDF(); $pdf->AddPage(); // Set font $pdf->SetFont('helvetica', '', 12); // Add the certificate details to PDF $pdf->Cell(0, 10, "Certificate of Audit", 0, 1, 'C'); $pdf->Ln(10); $pdf->Cell(0, 10, "Organization: $organization", 0, 1); $pdf->Cell(0, 10, "Application: $application", 0, 1); $pdf->Cell(0, 10, "Type of Audit: $audit_type", 0, 1); $pdf->Cell(0, 10, "URL: $url", 0, 1); $pdf->Cell(0, 10, "Initial Testing Date: $testing_date", 0, 1); $pdf->Cell(0, 10, "Revalidation Date: $revalidation_date", 0, 1); $pdf->Cell(0, 10, "Report ID: $report_id", 0, 1); $pdf->Cell(0, 10, "Issued On: $issued_on", 0, 1); $pdf->Cell(0, 10, "Certificate Number: $certificate_number", 0, 1); $pdf->Cell(0, 10, "Conclusion: $conclusion", 0, 1); $pdf->Cell(0, 10, "Recommendations: $recommendations", 0, 1); // Embed the 128-length random key in the PDF $pdf->Cell(0, 10, "Verification Key: $unique_key", 0, 1); // Output the barcode image to PDF $pdf->Image('@' . $barcode, 160, 250, 40, 15); // Save the PDF $pdf->Output("certificate_$certificate_number.pdf", 'I'); // 'I' for inline display in browser