Làm cách nào để tạo html pdf trong php?

Định dạng tài liệu cổng thông tin (PDF) là một trong những định dạng tệp phổ biến nhất hiện nay. Các cá nhân có thể xem, gửi và nhận tài liệu PDF bất kể hệ điều hành, phần cứng hoặc phần mềm của họ. Như vậy, nội dung của tài liệu PDF sẽ hiển thị giống nhau trên mọi nền tảng. Trong hướng dẫn từng bước này, chúng ta sẽ thảo luận cách làm việc với các tệp PDF trong PHP

Bạn có thể truy cập mã hoàn chỉnh sẽ được sử dụng trong hướng dẫn này từ kho lưu trữ GitHub này

Lịch sử của PDF

Adobe đã giới thiệu PDF vào năm 1993 để cho phép mọi người chia sẻ và trình bày tài liệu dễ dàng. Kể từ đó, PDF đã trở thành một tiêu chuẩn mở và được hỗ trợ bởi Tổ chức Tiêu chuẩn hóa Quốc tế (ISO). Ngày nay, các tệp PDF đã phát triển để chứa logic nghiệp vụ, hình ảnh, âm thanh, nút, biểu mẫu và thậm chí cả liên kết. Do đó, có một số kiến ​​thức về làm việc với các tệp PDF trong PHP là điều cần thiết

điều kiện tiên quyết

Để làm theo hướng dẫn này, bạn cần có một số kiến ​​thức cơ bản về PHP, HTML và SQL. Bạn cũng nên cài đặt XAMPP trên máy tính của mình. Khung này cho phép chúng tôi lưu trữ trang web cục bộ

Cách tạo tệp PDF trong PHP

Điều hướng đến thư mục xampp và sau đó nhấp vào thư mục htdocs. Trong hầu hết các trường hợp, thư mục xampp mặc định trên Windows được tìm thấy trên Local Disk C

Tạo một thư mục mới và gán cho nó tên dự án ưa thích của bạn. Trong trường hợp của tôi, tôi sẽ đặt tên nó là pdfexample. Bây giờ chúng ta có thể mở thư mục này trong trình chỉnh sửa mã, chẳng hạn như Visual Studio Code

Trong thư mục pdfexample, thêm một tệp mới và đặt tên là

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
9. Tệp này sẽ đóng vai trò là trang gốc cho trang web của chúng tôi

Thêm mã soạn sẵn sau vào

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
9


 lang="en">

     charset="UTF-8">
     http-equiv="X-UA-Compatible" content="IE=edge">
     name="viewport" content="width=device-width, initial-scale=1.0">
    </span>Document<span>



Welcome to PDF Example

Tiếp theo, khởi chạy bảng điều khiển XAMPP và bắt đầu phiên bản Apache và MySQL. Khi bạn điều hướng đến localhost/pdfexample/, bạn sẽ thấy một trang có thông báo mẫu Chào mừng bạn đến với PDF

Cho đến bước này, chúng tôi đã thiết lập môi trường phát triển và lưu trữ ứng dụng cục bộ bằng XAMPP

Bây giờ, hãy thêm một biểu mẫu đơn giản và sau đó tạo tệp PDF

Lưu ý rằng chúng tôi sẽ sử dụng Bootstrap để tạo kiểu. Do đó, hãy nhớ thêm dòng sau vào phần

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
1

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

Trong tệp

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
9, hãy thêm nội dung sau vào phần
 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
3

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF

Trong đoạn mã trên, chúng tôi có một biểu mẫu đơn giản cho phép người dùng nhập tên, email, nơi cư trú và câu chuyện của họ. Khi người dùng nhấp vào nút gửi, họ sẽ được chuyển đến một trang mới nơi họ sẽ tải xuống tệp PDF

Trong thư mục gốc của dự án, tạo một tệp mới và đặt tên là

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
4. Nó sẽ chứa logic để tạo PDF dựa trên dữ liệu người dùng

Chúng tôi cần tải xuống và nhập thư viện mpdf vào dự án của mình. Để làm như vậy, đảm bảo rằng bạn đã cài đặt Composer

Trong thư mục gốc của dự án, hãy khởi chạy một thiết bị đầu cuối tích hợp và sau đó cài đặt

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
5 bằng lệnh bên dưới

composer require mpdf/mpdf

Khi quá trình cài đặt hoàn tất thành công, hãy mở tệp

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
4 mà chúng tôi đã tạo trước đó và sau đó thêm đoạn mã sau


require_once __DIR__ . '/vendor/autoload.php';

Dòng trên cho phép chúng tôi tự động tải nhiều lớp PHP, do đó giảm lỗi. Chúng ta cần khởi tạo thư viện

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
5 trước khi tiếp tục

$mpdf = new \Mpdf\Mpdf();

Bước tiếp theo là truy xuất các giá trị của người dùng trong phương thức

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
8 và lưu trữ chúng trong các biến mới

$username=$_POST['username'];
$usermail=$_POST['usermail'];
$residence=$_POST['residence'];
$userstory=$_POST['yourstory'

Chúng tôi sẽ lưu trữ dữ liệu trên trong một biến, như sau

$infor ='';

$infor .='

Details

'
; $infor .='Username: ' . $username . '
'
; $infor .='Email: ' . $usermail .'
'
; $infor .='Residence: ' . $residence .'
'
; $infor .='Testimonial: ' . $userstory .'
'
;

Bây giờ chúng tôi có thể hiển thị dữ liệu trong trình duyệt dưới dạng tệp PDF bằng mã sau

$mpdf->WriteHTML($infor);
$mpdf->Output();

Toàn bộ mã trong tệp

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
4 được hiển thị bên dưới


require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$username=$_POST['username'];
$usermail=$_POST['usermail'];
$residence=$_POST['residence'];
$userstory=$_POST['yourstory'];

$infor ='';

$infor .='

Details

'
; $infor .='Username: ' . $username . '
'
; $infor .='Email: ' . $usermail .'
'
; $infor .='Residence: ' . $residence .'
'
; $infor .='Testimonial: ' . $userstory .'
'
; $mpdf->WriteHTML($infor); $mpdf->Output(); ?>

Khi bạn điều hướng đến trình duyệt của mình và nhấp vào nút tạo tệp PDF, bạn sẽ thấy nội dung tương tự như sau

Làm cách nào để tạo html pdf trong php?

Cách thêm bản ghi cơ sở dữ liệu vào tệp PDF

Hầu hết các trang web lưu trữ thông tin người dùng trong cơ sở dữ liệu. Do đó, hiểu cách trình bày dữ liệu này trong PDF là rất quan trọng

Trong bước này, chúng tôi sẽ tìm nạp các bản ghi từ cơ sở dữ liệu MYSQL và sau đó sử dụng chúng để tạo tệp PDF. Để bắt đầu, hãy điều hướng đến bảng điều khiển

composer require mpdf/mpdf
0 của bạn và tạo cơ sở dữ liệu mới

Tiếp theo, thêm một bảng và một vài bản ghi. Bạn có thể tìm hiểu thêm về cơ sở dữ liệu MYSQL tại đây

Làm cách nào để tạo html pdf trong php?

Khi cơ sở dữ liệu được tạo thành công, hãy điều hướng đến thư mục gốc của dự án của bạn và tạo tệp

composer require mpdf/mpdf
1. Nó sẽ chứa logic để kết nối với cơ sở dữ liệu MYSQL

Trong tệp

composer require mpdf/mpdf
1, hãy thêm đoạn mã sau

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
0

Trong đoạn mã trên, chúng tôi đang kết nối với cơ sở dữ liệu cục bộ có tên

composer require mpdf/mpdf
3. Quản trị viên cơ sở dữ liệu là
composer require mpdf/mpdf
4 và không có mật khẩu (dành cho mục đích thử nghiệm)

Nếu kết nối cơ sở dữ liệu không thành công, một thông báo lỗi sẽ được hiển thị

Để hiển thị các bản ghi cơ sở dữ liệu của chúng tôi, hãy tạo một tệp mới và đặt tên cho nó là

composer require mpdf/mpdf
5. Chúng tôi sẽ nhập tệp
composer require mpdf/mpdf
1 vào đây để cho phép chúng tôi kết nối với cơ sở dữ liệu

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
1

Bước tiếp theo là truy cập bảng của chúng tôi và truy xuất tất cả các bản ghi

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
2

Chúng ta sẽ sử dụng vòng lặp

composer require mpdf/mpdf
7 để duyệt qua tất cả các mục nhập trong cơ sở dữ liệu và sau đó hiển thị chúng trên trang web

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
3

Các mục sau sẽ được hiển thị trên trình duyệt

Làm cách nào để tạo html pdf trong php?

Hãy nhớ thêm một nút ở cuối trang để cho phép chúng tôi tải xuống tệp PDF. Chúng tôi sẽ liên kết nút này với tệp

composer require mpdf/mpdf
8 mà chúng tôi sẽ tạo trong bước tiếp theo

Đây là mã cho tệp

composer require mpdf/mpdf
5

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
4

Để tạo tệp PDF với dữ liệu trên, hãy tạo một tệp mới có tên

composer require mpdf/mpdf
8 trong thư mục gốc của dự án

Trong tệp này, một lần nữa chúng tôi sẽ nhập tệp

composer require mpdf/mpdf
1 để cho phép truy cập vào cơ sở dữ liệu

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
5

Tiếp theo, chúng tôi khởi tạo thư viện

 class="container mt-5">
     class="text-center">Welcome to PDF example
     class="text-center">Please fill the details before

class="col-md-6 offset-md-3"> method="POST" class="form" action="createfile.php"> class="form-label" for="username">Name
class="form-control" name="username" type="text" required>
class="form-label" for="usermail">Email
class="form-control" name="usermail" type="text" required>
class="form-label" for="residence">Residence
class="form-control" name="residence" type="text" required>
class="form-label" for="yourstory">Your Story
class="form-control" name="yourstory" rows="5" required> class="d-grid gap-2 col-6 mx-auto mt-5"> type="submit" class="btn btn-primary btn-block">Download PDF
5

$mpdf = new \Mpdf\Mpdf();

Sau đó, chúng tôi truy cập các bản ghi trong bảng cơ sở dữ liệu bằng vòng lặp

composer require mpdf/mpdf
7. Chúng tôi sẽ nối từng mục nhập cơ sở dữ liệu vào một chuỗi (

require_once __DIR__ . '/vendor/autoload.php';
4) và sau đó hiển thị nó ở cuối

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
7

Mã cuối cùng trong tệp

composer require mpdf/mpdf
8 sẽ xuất hiện như hình bên dưới

 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
8

Khi bạn truy cập tệp

composer require mpdf/mpdf
8 trong trình duyệt của mình, bạn sẽ thấy một tài liệu PDF chứa tất cả các mục cơ sở dữ liệu của bạn

Làm cách nào để tạo html pdf trong php?

Có thể tải xuống tệp PDF bằng cách nhấp vào biểu tượng tải xuống trên trình duyệt của bạn

Những hạn chế cần chú ý

Trong hướng dẫn này, chúng ta đã học cách tạo tệp PDF trong PHP. Thư viện


require_once __DIR__ . '/vendor/autoload.php';
7 giúp việc tạo và định dạng tệp PDF dễ dàng hơn so với sử dụng các mô-đun gốc. Tuy nhiên, bạn nên đảm bảo rằng dữ liệu được trình bày ở định dạng chính xác để tránh lỗi hoặc lỗi trong mã của bạn. Chẳng hạn, phương thức

require_once __DIR__ . '/vendor/autoload.php';
8 chỉ chấp nhận các chuỗi làm tham số của nó

Làm cách nào để tạo tệp HTML sang PDF trong PHP?

$pdf = $client->convertString("");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");");"); Hoặc sử dụng convertFile() để chuyển đổi tệp HTML cục bộ. $pdf = $client->convertFile("/path/to/MyLayout. html");

Làm cách nào để in trang HTML dưới dạng PDF trong PHP?

php'; . //www. thí dụ. com"); // đặt tiêu đề phản hồi HTTP header("Content-Type. ứng dụng/pdf"); tiêu đề

Làm cách nào để tạo PDF của một trang web trong PHP?

php yêu cầu('. /fpdf. php'); . '); . Upon execution, the PHP script will generate a PDF file in your browser.

Làm cách nào tôi có thể tạo tệp HTML trong PHP?

Trong trường hợp của bạn, chỉ cần thay đổi phần mở rộng của tên tệp . Thí dụ. tên tệp của bạn. html vào tên tệp của bạn. php.