Missing Character 'ہ' in Urdu Fonts when Generating PDF using mPDF in PHP

116 Views Asked by At

I am facing a unique challenge with the mPDF library in PHP when generating PDFs containing Urdu text. The specific character causing issues is 'ہ' (Heh), which appears to be either missing or not rendering correctly in the generated PDFs. The chosen font for Urdu content is Jameel Noori Nastaleeq.

Here is a more detailed breakdown of the problem:

1) Font Configuration:

I have set the font to Jameel Noori Nastaleeq, which is well-known for rendering Urdu text accurately.

2) HTML Content Generation:

The HTML content is structured to display Urdu text, including the problematic character 'ہ'. Other Urdu characters are rendering correctly.

3) Rendering Issue:

Despite efforts to adjust the font and styling, the 'ہ' character consistently fails to display correctly in the generated PDFs.

4) Seeking Solutions:

I am seeking insights from the community on potential reasons for this specific character rendering issue in mPDF, especially when using Urdu fonts.

5) Alternative Approaches:

If anyone has encountered similar problems or has suggestions for alternative approaches or potential workarounds, I would greatly appreciate your expertise. I am grateful for any assistance or guidance provided in resolving this issue. Thank you for your time and support!

<?php
require_once 'vendor/autoload.php'; // Adjust the path accordingly

// Function to generate PDF using mPDF
function generatePDF($data) {
    $mpdf = new \Mpdf\Mpdf();

    $mpdf->SetFont("Jameel Noori Nastaleeq");

   // Load HTML content
   $html = "<html dir='rtl' lang='ur'><head><style>
   body {
    font-family: 'Jameel Noori Nastaleeq', 'Noto Nastaliq Urdu', 'Nafees Nastaleeq', 'Urdu Naskh Asiatype', 'Tahoma', sans-serif;
  }

   /* Add or modify styles for better layout */
   .info-container {
       border: 1px solid #ccc;
       padding: 10px;
       margin: 20px auto;
       max-width: 600px; /* Adjust as needed */
   }
   
   .info-label {
       font-weight: bold;
       display: inline-block; /* Display labels inline */
       width: 150px; /* Adjust label width as needed */
       margin: 0;
   }
   
   .info-value {
       display: inline-block; /* Display values inline */
       margin: 0;
   }
   
   </style></head><body>";
   


$html .= "<div class='info-container'>";
$html .= "<p class='info-label'>والد کا پیشہ:</p><p class='info-value'>{$data['father_occupation']}</p>";
$html .= "<p class='info-label'>کیا والدین حیات ہیں؟:</p><p class='info-value'>" . ($data['are_parents_alive'] == 1 ? 'ہاں' : 'نہیں') . "</p>";
$html .= "</div>";

$html .= "</body></html>";

$mpdf->mirrorMargins = true;
$mpdf->SetDisplayMode('fullpage');
    // Load HTML to mPDF
    $mpdf->WriteHTML($html);

    // Output PDF to the bdataser
    $mpdf->Output($data['name'] . '_' . date('Y-m-d') . '.pdf', 'D');

}

include('db.php');

// Check if ID is provided in the URL
if (isset($_GET['id'])) {
    $id = $_GET['id'];

    // Fetch data for the specified ID
    $result = $conn->query("SELECT * FROM users_data WHERE id=$id");

    if ($result->num_rows == 1) {
        // Fetch the row as an associative array
        $fetched_data_for_id = $result->fetch_assoc();

        // Call the generatePDF function with the fetched data
        generatePDF($fetched_data_for_id);
    } else {
        echo "No data found for the specified ID.";
    }
} else {
    echo "No ID specified.";
}
?>

enter image description here

1

There are 1 best solutions below

0
Muhammad Yahya Imran On

It seems like you want to change the font settings in your mPDF configuration to resolve an issue with missing characters in Urdu fonts. To achieve this, you'll need to make the following modifications:

  1. Remove the line:
    $mpdf->SetFont("Jameel Noori Nastaleeq");
  1. Replace the existing CSS rule in your HTML or stylesheet with:
body {
    font-family: freeserif;
}

By doing this, you are updating the font settings to use the 'freeserif' font for the body instead of 'Jameel Noori Nastaleeq'. This change should help in resolving the issue with missing characters.

Make sure to re-generate your PDF after these modifications to see if the problem persists. If you face any further issues, feel free to provide additional details for more assistance.