Laravel Preview, Download PDF from HTML

Trong bài viết này mình sẽ chia sẻ cách preview, download file pdf từ HTML với laravel 5.7+. Mình sẽ sử dụng dompdf (barryvdh/laravel-dompdf package) để tạo file pdf trong ví dụ nhé. Step1: Install laravel-dompdf Package Đầu tiên mình sẽ cài đặt barryvdh/laravel-dompdf bởi composer command bên dưới trong ứng dụng. composer require barryvdh/laravel-dompdf

Trong bài viết này mình sẽ chia sẻ cách preview, download file pdf từ HTML với laravel 5.7+.
Mình sẽ sử dụng dompdf (barryvdh/laravel-dompdf package) để tạo file pdf trong ví dụ nhé.

Step1: Install laravel-dompdf Package

Đầu tiên mình sẽ cài đặt barryvdh/laravel-dompdf bởi composer command bên dưới trong ứng dụng.

composer require barryvdh/laravel-dompdf

Sau khi cài đặt thành công package, mình sẽ mở file config/app.php và thêm service provider and alias.
config/app.php

'providers' => [
	....
	BarryvdhDomPDFServiceProvider::class,
],
  
'aliases' => [
	....
	'PDF' => BarryvdhDomPDFFacade::class,
]

Step 2: Create Routes

Mở file routes/web.php và thêm code như bên dưới.
routes/web.php

Route::get('generate-pdf','[email protected]');

Step 3: Create Controller

Mình sẽ tạo ItemController.php với phương thức generatePDF trong route và thêm code như bên dưới.
app/Http/Controllers/ItemController.php

<?php

namespace AppHttpControllers;

use AppModelsItem;
use IlluminateHttpRequest;
use PDF;

class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function generatePDF()
    {
        $data = ['title' => 'Welcome to Viet Nam'];
        $pdf = PDF::loadView('myPDF', $data);
        //Nếu muốn hiển thị file pdf theo chiều ngang
        // $pdf->setPaper('A4', 'landscape');
        
        //Nếu muốn download file pdf
        return $pdf->download('myPDF.pdf');
        
        //Nếu muốn preview in pdf
        //return $pdf->stream('myPDF.pdf');
    }
}

Trong phương thức generatePDF mình đã ghi chú cách preview, download và hiển thị pdf theo kiểu landscape(bề ngang).

Step 4: Create Blade File

Mình sẽ tạo file myPDF.blade.php(resources/views/myPDF.blade.php) để hiển thị layout của file pdf và thêm code như bên dưới.
resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>Hi</title>
</head>
<body>
	<h1>Welcome to ItSolutionStuff.com - {{ $title }}</h1>
	<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
	tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
	quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
	consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
	cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
	proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

Thay vì fix cứng dữ liệu hiển thị ra file pdf, các bạn có thể hiển thị dữ liệu từ database vào file pdf nhé.
Cuối cùng chạy code và kiểm tra kết quả.

Nguồn: viblo.asia

Bài viết liên quan

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th