DEV Community

Tahsin Abrar
Tahsin Abrar

Posted on

How to Make Dynamic Bangla PDF using Laravel mPDF and Send Them via Email

Installation

  1. Clone or download the project repository.
  2. Navigate to the project directory in your terminal.
  3. Run composer install to install all dependencies.
composer require mpdf/mpdf
Enter fullscreen mode Exit fullscreen mode

Configuration

  1. Copy .env.example to .env.
  2. Configure your database settings in the .env file.
  3. Set up your email settings in the .env file.

Custom Bangla Font Installation

  1. Download your desired Bangla font and place it in the resources/fonts directory.
  2. Download bangla font Nikosh.ttf
  3. Copy the font paste laravel-PDF-bangla-font\vendor\mpdf\mpdf\ttfonts
  4. Open config/pdf.php and update the 'default_font' key with the name of your Bangla font file (without the extension).

Usage

  1. Define your route to generate the PDF.
Route::get('/generate-pdf', [FormController::class, 'generatePDFAndSendEmail']);
Enter fullscreen mode Exit fullscreen mode
  1. Implement the generatePDFAndSendEmail method in your controller.
public function generatePDFAndSendEmail(Request $request)
{
    // Prepare data for the PDF
     $emailData = [
        'email' => 'example@gmail.com',
        'password' => '123',
        'eng_name' => 'Hasibur Rahman',
        'ban_name' => 'হাসিবুর রহমান',
        'father_name' => 'Hasmat Ali',
        'mother_name' => 'Zainab Begum',
        'permanent_address' => 'California',
        'mobile' => '01700000000',
        'blood_group' => 'AB+',
        'department_id' => 9,
        'date_of_birth' => '1998-12-21',
        'passing_year_id' => 4,
        'image' => 'https://www.redwolf.in/image/cache/catalog/stickers/tom-face-sticker-india-600x800.jpg',
        'signature' => 'https://signature.freefire-name.com/img.php?f=3&t=Tom',
        'lm_no' => '2000',
        'current_date' => '2022-11-30',
    ];

    // Generate PDF content
    $pdfContent = $this->generatePDF($emailData);

    // Send email with PDF attachment
    $this->sendEmailWithAttachment($emailData, $pdfContent, 'recipient@example.com');

    // Return a response indicating success
    return "Email sent with PDF attachment.";
}
Enter fullscreen mode Exit fullscreen mode
  1. Implement the generatePDF and sendEmailWithAttachment methods in your controller.
private function generatePDF($data)
{
    // Create new mPDF instance
    $mpdf = new \Mpdf\Mpdf([
        'default_font_size' => 16,
        'default_font' => 'nikosh', // Change this to your desired Bengali font
    ]);

    // Generate PDF content with data
    $pdfContent = view('pdf.bengali_pdf', $data)->render();

    // Write HTML content to PDF
    $mpdf->WriteHTML($pdfContent);

    // Get PDF content as string
    return $mpdf->Output('', 'S');
}

private function sendEmailWithAttachment($emailData, $pdfData, $recipientEmail)
{
    // Send email with PDF attachment
    Mail::send('pdf.bangla_email', $emailData, function ($message) use ($emailData, $pdfData, $recipientEmail) {
        $message->to($recipientEmail, 'Recipient Name')
            ->subject('New User Registration')
            ->attachData($pdfData, 'LM-'.$emailData['lm_no'].'.pdf');
    });
}
Enter fullscreen mode Exit fullscreen mode

View

Your view file (pdf.bengali_pdf.blade.php) should contain the HTML structure for your PDF content.

<!-- Sample PDF content -->
<h1>{{ $emailData['eng_name'] }}</h1>
<h2>{{ $emailData['ban_name'] }}</h2>
<p>Father's Name: {{ $emailData['father_name'] }}</p>
<p>Mother's Name: {{ $emailData['mother_name'] }}</p>
<p>Permanent Address: {{ $emailData['permanent_address'] }}</p>
<p>Mobile: {{ $emailData['mobile'] }}</p>
<p>Blood Group: {{ $emailData['blood_group'] }}</p>
<p>Date of Birth: {{ $emailData['date_of_birth'] }}</p>
<!-- You can include additional dynamic data as required -->

Enter fullscreen mode Exit fullscreen mode

Running the Application

  1. Start your Laravel development server.
  2. Visit the /generate-pdf route in your browser.
  3. Verify that the email is sent with the PDF attachment.

Top comments (0)