The verbal descriptions take a while to read through to get a feel for the expected results for fopen modes. This csv table can help break it down for quicker understanding to find which mode you are looking for:
________số 8
Hôm nay tôi sẽ trình bày một trường hợp sử dụng của một khách hàng yêu cầu một ứng dụng PHP có thể xuất các tệp PDF chứa đầy dữ liệu đã thu thập
Để minh họa, tôi sẽ trình bày và ví dụ nhỏ project trong Laravel
Thiết lập môi trường
Hãy bắt đầu tạo một dự án Laravel
composer create-project laravel/laravel PDFTest
Bước tiếp theo là tạo một docker container cho dự án này
version: '3.6'
services:
webserver:
image: nginx:latest
container_name: pdf_web
env_file: ./docker/environment.env
volumes:
- ./:/code
- ./docker/nginx/site.conf:/etc/nginx/conf.d/site.conf
- ./docker/nginx/laravel.site.conf:/etc/nginx/conf.d/api.conf
- ./docker/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf
ports:
- "80:80"
- "8888:80"
php:
image: erdiko/php-fpm:latest
container_name: pdf_php
env_file: ./docker/environment.env
volumes:
- ./:/code
ports:
- "9000:9000"
thư viện PDF
Sau một số nghiên cứu, tôi đã chọn pdftk, vì nó dường như được sử dụng rộng rãi nhất
Pdftk là một nhị phân đa nền tảng cung cấp một loạt các tùy chọn dòng lệnh tiện dụng để thao tác với các tệp PDF như lấy nội dung văn bản thuần túy, liệt kê các trường, liệt kê dữ liệu trường, điền vào biểu mẫu, v.v.
Một điểm vào đơn giản. tập lệnh sh được sử dụng để cài đặt pdftk
#!/bin/bash
apt update && apt install -y pdftk;
php-fpm
cái đó phải được ánh xạ vi và thực thi trong các dịch vụ php, nhận được một cái gì đó như thế này
php: image: erdiko/php-fpm:latest container_name: pdf_php env_file: ./docker/environment.env volumes: - ./:/code - ./docker/entrypoint.sh:/usr/local/etc/php/entrypoint.sh ports: - "9000:9000" entrypoint: /usr/local/etc/php/entrypoint.sh
Cách tích hợp trong PHP
Ở đây chúng tôi sẽ cài đặt gói php-pdftk. Php-pdftk là một gói php chuyển tiếp trên pdftk để thao tác các tài liệu PDF từ các ứng dụng php
composer require mikehaertl/php-pdft
cung cấp một lớp PHP, có tên là PDF, với tất cả các phương thức cần thiết để làm việc với các tệp PDF
Chung tay code
Bước đầu tiên là tạo bộ điều khiển, tôi gọi nó là Pdf Editor
php artisan make:controller PdfEditor
Dưới đây là cách triển khai trông như thế nào,
file['pdf_file'];
$filename = $uploadedFile->getClientOriginalName[];
$request->session[]->put['current_pdf', $filename];
Storage::disk['local']->putFileAs[
'files/'.$filename,
$uploadedFile,
$filename
];
return redirect[]->back[]->with['success', 'Your file is submitted Successfully'];
}
public function preview[]
{
if[Session::has['current_pdf']] {
$filepath = Storage::disk['local']->getDriver[]->getAdapter[]->getPathPrefix[] . 'files/'. Session::get['current_pdf'];
$filename = $filepath . '/' . Session::get['current_pdf'];
return response[file_get_contents[$filename]]->withHeaders[[
'Content-Type' => 'application/pdf'
]];
}
return redirect[]->back[]->with['error', 'Sorry. File not found.'];
}
public function edit[]
{
$filepath = Storage::disk['local']->getDriver[]->getAdapter[]->getPathPrefix[] . 'files/'. Session::get['current_pdf'];
$filename = $filepath . '/' . Session::get['current_pdf'];
$pdf = new Pdf[$filename];
$fields = $pdf->getDataFields[true]->__toArray[];
return view['pdftest.edit']->with['fields',$fields];
}
public function save[Request $request]
{
$filepath = Storage::disk['local']->getDriver[]->getAdapter[]->getPathPrefix[] . 'files/'. Session::get['current_pdf'];
$filename = $filepath . '/' . Session::get['current_pdf'];
$target = $filepath . '/filled_' . Session::get['current_pdf'];
$pdf = new Pdf[$filename];
$inputs = $request->all[];
unset[$inputs['_token']];
foreach [$inputs as $field=>$value] {
$data[str_replace['_',' ',$field]] = $value;
}
$pdf->fillForm[$data]
->needAppearances[];
if [!$pdf->saveAs[$target]] {
$error = $pdf->getError[];
dd[$error];
}
return response[file_get_contents[$target]]->withHeaders[[
'Content-Type' => 'application/pdf'
]];
}
}
Chúng tôi cũng sẽ cần hai chế độ xem, một để tải tệp PDF lên và chế độ xem còn lại để tạo biểu mẫu động cho phép chúng tôi chỉnh sửa nội dung của biểu mẫu PDF