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

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=

Bài 1 – React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an t

Chuyển đổi từ monolith sang microservices qua ví dụ

1. Why microservices? Microservices là kiến trúc hệ thống phần mềm hướng dịch vụ,