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

Rmag Breaking News

Installation

Clone or download the project repository.
Navigate to the project directory in your terminal.
Run composer install to install all dependencies.

composer require mpdf/mpdf

Configuration

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

Custom Bangla Font Installation

Download your desired Bangla font and place it in the resources/fonts directory.
Download bangla font Nikosh.ttf
Copy the font paste laravel-PDF-bangla-fontvendormpdfmpdfttfonts

Open config/pdf.php and update the ‘default_font’ key with the name of your Bangla font file (without the extension).

Usage

Define your route to generate the PDF.

Route::get(‘/generate-pdf’, [FormController::class, ‘generatePDFAndSendEmail’]);

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-600×800.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.”;
}

Implement the generatePDF and sendEmailWithAttachment methods in your controller.

private function generatePDF($data)
{
// Create new mPDF instance
$mpdf = new MpdfMpdf([
‘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’);
});
}

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 –>

Running the Application

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

Leave a Reply

Your email address will not be published. Required fields are marked *