so i have generated the pdf file using php and fpdf but i face some problems which am not able to fix the, first of all the checkbox always doesnt fit the line, and second i can not find a way to make table responsive according to the inserted data, here am gonna share the code and screen of the pdf file
pdf file image
here is what i have tried so far
<?php
require('api/fpdf186/fpdf.php');
class PDF extends FPDF
{
// Header function
function Header()
{
$this->SetFillColor(173, 216, 230); // Light blue
$this->Rect(10, 10, 190, 20, 'F');
$this->SetFont('Arial', 'B', 16);
$this->Cell(0, 10, 'Medical Report', 0, 1, 'C');
$this->SetFont('Arial', 'I', 10);
$this->Cell(0, 5, 'Patient Details and Doctor Summary', 0, 1, 'C');
$this->Ln(10); // Gap after header
}
// Section Header with gap below
function SectionHeader($title)
{
$this->Ln(5); // Gap after section header
$this->SetFont('Arial', 'B', 12);
$this->SetFillColor(230, 230, 230); // Light gray background
$this->Cell(190, 8, $title, 0, 1, 'L', true);
$this->Ln(5); // Gap after section header
}
// Form Field (label + underline for data)
function FormField($label, $width)
{
$this->SetFont('Arial', '', 11);
$this->Cell($width, 8, $label, 0, 0, 'L'); // Label
$this->Cell(190 - $width, 8, '', 'B'); // Underline for data
$this->Ln(8); // Line break after each field
}
// Checkbox function to draw checkboxes
function DrawCheckbox($label, $x, $y, $checked = false)
{
// Draw the checkbox (a square)
$this->Rect($x, $y, 5, 5); // Draw the square
// Mark it as checked or not
if ($checked) {
$this->SetXY($x + 1, $y);
$this->Cell(5, 5, 'X', 0, 0, 'C');
}
// Label next to the checkbox
$this->SetXY($x + 8, $y);
$this->Cell(10, 5, $label, 0, 1, 'L');
}
// Diagnosis Table Header (responsive width)
function DiagnosisTableHeader($widths)
{
$this->SetFont('Arial', 'B', 11);
$this->SetFillColor(200, 200, 200); // Light gray background
$this->Cell($widths[0], 8, 'Diagnosis', 1, 0, 'C', true);
$this->Cell($widths[1], 8, 'Result', 1, 0, 'C', true);
$this->Ln();
}
// Diagnosis Table Row (responsive width)
function DiagnosisTableRow($diagnosis, $result, $widths)
{
$this->SetFont('Arial', '', 11);
$this->Cell($widths[0], 8, $diagnosis, 1);
$this->Cell($widths[1], 8, $result, 1);
$this->Ln();
}
// Prescription Table Header (responsive width)
function PrescriptionTableHeader($widths)
{
$this->SetFont('Arial', 'B', 11);
$this->SetFillColor(200, 200, 200); // Light gray background
$this->Cell($widths[0], 8, 'Medicine Name', 1, 0, 'C', true);
$this->Cell($widths[1], 8, 'Dosage', 1, 0, 'C', true);
$this->Cell($widths[2], 8, 'Explanation', 1, 0, 'C', true);
$this->Cell($widths[3], 8, 'Availability', 1, 0, 'C', true);
$this->Ln();
}
// Prescription Table Row (responsive width)
function PrescriptionTableRow($medicine, $dosage, $explanation, $availability, $widths)
{
$this->SetFont('Arial', '', 11);
$this->Cell($widths[0], 8, $medicine, 1);
$this->Cell($widths[1], 8, $dosage, 1);
$this->Cell($widths[2], 8, $explanation, 1);
$this->Cell($widths[3], 8, $availability ? '[x]' : '[ ]', 1, 0, 'C');
$this->Ln();
}
// Calculate maximum widths based on content
function CalculateMaxWidths($data, $defaultWidths)
{
$widths = $defaultWidths;
foreach ($data as $row) {
foreach ($row as $key => $value) {
$textWidth = $this->GetStringWidth($value) + 4; // Extra padding
if ($textWidth > $widths[$key]) {
$widths[$key] = $textWidth;
}
}
}
return $widths;
}
// MultiCell wrapper to handle long text
function MultiCellWrapped($width, $height, $text)
{
$this->MultiCell($width, $height, $text, 1);
}
}
// Create PDF instance
$pdf = new PDF();
$pdf->AddPage();
// Part A: Patient's Information Section
$pdf->SectionHeader('Patient Information');
// Title field with checkboxes
$pdf->Cell(30, 8, 'Title:', 0, 0, 'L');
$pdf->DrawCheckbox('Mr', 45, $pdf->GetY(), true); // Checked by default
$pdf->DrawCheckbox('Mrs', 65, $pdf->GetY(), false);
$pdf->Ln(8); // Line break after checkboxes
// Full Name
$pdf->FormField('Full Name', 40);
// Birth Date
$pdf->FormField('Birth Date', 40);
// Address
$pdf->FormField('Address', 40);
// Diagnosis Table Data
$diagnosisData = [
['Fracture', 'Positive'],
['Concussion', 'Negative'],
['Bruising Bruising Bruising Bruising Bruising Bruising Bruising Bruising', 'Severe bruising around the leg area due to the impact of the accident Severe bruising around the leg area due to the impact of the accident Severe bruising around the leg area due to the impact of the accident Severe bruising around the leg area due to the impact of the accident']
];
// Calculate responsive widths for Diagnosis table
$defaultWidthsDiagnosis = [70, 120]; // Default width proportions
$diagnosisWidths = $pdf->CalculateMaxWidths($diagnosisData, $defaultWidthsDiagnosis);
// Diagnosis Table
$pdf->SectionHeader('Diagnosis');
$pdf->DiagnosisTableHeader($diagnosisWidths);
foreach ($diagnosisData as $row) {
$pdf->DiagnosisTableRow($row[0], $row[1], $diagnosisWidths);
}
// Prescription Table Data
$prescriptionData = [
['Ibuprofen', '200mg', 'Take twice a day', true],
['Amoxicillin', '500mg', 'Take three times a day', false],
['Paracetamol', '500mg', 'Take as needed for pain', true]
];
// Calculate responsive widths for Prescription table
$defaultWidthsPrescription = [60, 40, 50, 30]; // Default width proportions
$prescriptionWidths = $pdf->CalculateMaxWidths($prescriptionData, $defaultWidthsPrescription);
// Prescription Table
$pdf->SectionHeader('Prescriptions');
$pdf->PrescriptionTableHeader($prescriptionWidths);
foreach ($prescriptionData as $row) {
$pdf->PrescriptionTableRow($row[0], $row[1], $row[2], $row[3], $prescriptionWidths);
}
// Doctor's Explanation
$pdf->SectionHeader('Doctor's Explanation');
$pdf->MultiCellWrapped(190, 10, 'The patient has sustained multiple injuries from the accident. Treatment will continue for a period of six weeks and further diagnostics may be required to ensure a complete recovery.');
// Output the PDF
$pdf->Output();
?>
2