omaralalwi / gpdf
支持阿拉伯语的定制PDF包装器
Requires
- php: ^8.1|^8.2|^8.3
- aws/aws-sdk-php: ^3.308
- dompdf/dompdf: ^3.0
- khaled.alshamaa/ar-php: ^6.3
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-04 17:54:17 UTC
README
Gpdf : PHP & Laravel的HTML到PDF转换器
开源PHP包,用于在PHP & Laravel应用程序中转换HTML到PDF,支持存储到s3,内置对阿拉伯语和其他语言的支持。扩展dompdf以添加新功能和解决如阿拉伯语支持等问题。
要求
- PHP版本8.1或更高
- DOM扩展
- MBString扩展
- php-font-lib
- php-svg-lib
安装
composer require omaralalwi/gpdf
发布资源
安装后,在项目根路径中运行以下命令来发布配置和字体资源:
php vendor/omaralalwi/gpdf/scripts/publish_fonts.php php vendor/omaralalwi/gpdf/scripts/publish_config.php
发布问题提示:如果在发布时遇到任何问题,请手动将vendor/omaralalwi/gpdf/assets/fonts
文件夹复制到public/vendor/gpdf
,并确保字体位于public/vendor/gpdf/fonts
中。此外,将vendor/omaralalwi/gpdf/config/gpdf.php
复制到根路径中的/config
文件夹。
与原生PHP应用程序一起使用
安装包并发布资源后,包含autoload.php
并使用Gpdf
类。
基本用法
require_once __DIR__ . '/vendor/autoload.php'; use Omaralalwi\Gpdf\Gpdf; use Omaralalwi\Gpdf\GpdfConfig; $htmlFile = __DIR__ . '/contents/example-1.html'; $content = file_get_contents($htmlFile); $gpdfConfigFile = require_once 'config/gpdf.php'; $config = new GpdfConfig($gpdfConfigFile); $gpdf = new Gpdf($config); $pdfContent = $gpdf->generate($content); header('Content-Type: application/pdf'); echo $pdfContent;
注意:根据需要自定义设置文件。
流生成PDF文件
使用generateWithStream
直接将PDF流到浏览器
require_once __DIR__ . '/vendor/autoload.php'; use Omaralalwi\Gpdf\Gpdf; use Omaralalwi\Gpdf\GpdfConfig; $htmlFile = __DIR__ . '/contents/example-1.html'; $content = file_get_contents($htmlFile); $gpdfConfigFile = require_once 'config/gpdf.php'; $config = new GpdfConfig($gpdfConfigFile); $gpdf = new Gpdf($config); $pdfContent = $gpdf->generateWithStream($content, 'demo-file-name', true); header('Content-Type: application/pdf'); echo $pdfContent;
存储到本地
使用generateWithStore
将PDF文件保存到本地存储
注意:默认情况下,它将文件存储到本地驱动器。
<?php require_once __DIR__ . '/vendor/autoload.php'; use Omaralalwi\Gpdf\Gpdf; use Omaralalwi\Gpdf\GpdfConfig; use Omaralalwi\Gpdf\Enums\{GpdfDefaultSettings, GpdfSettingKeys, GpdfDefaultSupportedFonts}; $htmlFile = __DIR__ . '/contents/example-1.html'; $content = file_get_contents($htmlFile); $gpdfConfigFile = require_once ('config/gpdf.php'); $config = new GpdfConfig($gpdfConfigFile); $gpdf = new Gpdf($config); $sslVerify = false; $file = $gpdf->generateWithStore($content,null,null, false , $sslVerify); // $sslVerify must be true in production $fileUrl = $file['ObjectURL']; return $fileUrl; // get file url as string to store it in db or do any action
generateWithStore参数
存储到S3
与本地存储类似,只需将本地路径替换为存储桶名称,并将generateWithStore
替换为generateWithStoreToS3
。
<?php require_once __DIR__ . '/vendor/autoload.php'; use Omaralalwi\Gpdf\Gpdf; use Omaralalwi\Gpdf\GpdfConfig; use Omaralalwi\Gpdf\Enums\{GpdfDefaultSettings, GpdfSettingKeys, GpdfDefaultSupportedFonts}; $htmlFile = __DIR__ . '/contents/example-1.html'; $content = file_get_contents($htmlFile); $gpdfConfigFile = require_once ('config/gpdf.php'); $config = new GpdfConfig($gpdfConfigFile); $gpdf = new Gpdf($config); $fileName = "pdf-file-with-store-to-s3"; $sslVerify = true; $file = $gpdf->generateWithStoreToS3($content,null,$fileName, true, $sslVerify); $fileUrl = $file['ObjectURL'];
原生PHP应用程序示例
请参阅此原生PHP应用程序示例,其中包含更多详细示例和情况,例如为HTML文件传递动态参数以及传递内联配置等。
与Laravel一起使用
使用Gpdf外观
use Omaralalwi\Gpdf\Facade\Gpdf as GpdfFacade; public function generatePdf() { $html = view('pdf.example-1')->render(); $pdfContent = GpdfFacade::generate($html); return response($pdfContent, 200, ['Content-Type' => 'application/pdf']); }
使用依赖注入
use Omaralalwi\Gpdf\Gpdf; public function generateSecondWayPdf(Gpdf $gpdf) { $html = view('pdf.example-2')->render(); $pdfFile = $gpdf->generate($html); return response($pdfFile, 200, ['Content-Type' => 'application/pdf']); }
流生成PDF文件
使用generateWithStream
直接将PDF流到浏览器
// by default it store files to local driver (path should in public path). public function generateAndStream() { $html = view('pdf.example-2')->render(); $gpdf = app(Gpdf::class); $gpdf->generateWithStream($html, 'test-streamed-pdf', true); return response(null, 200, ['Content-Type' => 'application/pdf']); }
存储生成的PDF文件
存储到本地
使用generateWithStore
将PDF保存到存储
注意:默认情况下,它将文件存储到本地驱动器(确保存储路径可读写)。
public function generateAndStore() { $html = view('pdf.example-2')->render(); $gpdf = app(Gpdf::class); $storePath = storage_path('app/downloads/users/'); $gpdf->generateWithStore($html, $storePath, 'test-stored-pdf-file', true, false); // ssl verify should be true in production . return $file['ObjectURL']; // return file url as string , to store in db or do any action } // may be you will face problems with stream in local, so you can disable ssl verify in local, but should enable it in production.
存储到S3
与本地存储类似,只需将本地路径替换为存储桶名称,并将generateWithStore
替换为generateWithStoreToS3
。
注意:请确保在配置文件中设置了s3配置。
public function generateAndStoreToS3() { $data = $this->getDynamicParams(); $html = view('pdf.example-2',$data)->render(); $gpdf = app(Gpdf::class); $bucketName = 'your_s3_bucket_name'; // should be read abel and write able . $file = $gpdf->generateWithStoreToS3($html, $bucketName, 'test-store-pdf-fle', true, true); // with s36 the ssl verify will work in local or production (always secure). return $file['ObjectURL']; // return file url as string , to store in db or do any action }
生成带有固定标题的高级PDF
如果需要为所有页面添加固定标题,请参阅此示例。
演示Laravel应用程序
此演示Laravel应用程序包含更多详细示例和情况。
支持的字体
Gpdf支持以下已安装的字体(无需任何额外配置即可使用)
对阿拉伯语的支持
Gpdf支持开箱即用的阿拉伯语内容。只需在您的HTML内容中传递阿拉伯文文本。请确保使用默认包含的阿拉伯语字体。
支持的阿拉伯语字体
以下内置字体支持阿拉伯语
DejaVu Sans Mono
, Tajawal
, Almarai
, Cairo
, Noto Naskh Arabic
, Markazi Text
。
我们推荐使用从 Omaralalwi\Gpdf\Enums\GpdfDefaultSupportedFonts
枚举类中获取的字体名称,例如在配置文件中的 默认字体名称
。
示例
安装新字体
要安装新字体,请按照以下步骤操作
- 确保默认字体已发布到
public/vendor/gpdf/fonts
。 - 为每个字体家族(
(Normal, Bold, Italic, BoldItalic)
)准备至少一个字体(正常)。 - 将字体复制到任何路径(不是默认字体路径)。
- 字体家族名称必须用双引号括起来,并写成小写。
- 字体名称必须采用连字符命名法,并首字母大写。
- 使用以下命令运行安装字体脚本
php vendor/omaralalwi/gpdf/scripts/install_font.php "family name" ./path_to_font/Font-Normal.ttf ./path_to_font/Font-Bold.ttf ./resources/fonts/Tajawal-Italic.ttf ./path_to_font/Font-BoldItalic.ttf
例如,要安装 Tajawal
字体家族
php vendor/omaralalwi/gpdf/scripts/install_font.php "tajawal" ./resources/fonts/Tajawal-Normal.ttf ./resources/fonts/Tajawal-Bold.ttf ./resources/fonts/Tajawal-Italic.ttf ./resources/fonts/Tajawal-BoldItalic.ttf
功能
- 与任何标准PHP应用程序或框架兼容。
- 直接将PDF文件存储到S3或本地存储。
- 从URL(本地或S3)流式传输PDF文件。
- 默认支持17种字体,包括7种支持阿拉伯语的字体。
- 允许安装自定义字体。
- 提供与Laravel应用程序的简单集成。
- 提供PDF生成的自定义选项。
- 包含详细的文档。
- 提供演示应用程序以快速启动 演示原生PHP应用程序 , 演示Laravel应用程序 .
- 单元测试 包括单元测试。
感谢
测试
composer test
或
php run-tests.php
变更日志
请参阅 CHANGELOG 了解最新更改。
贡献
请参阅 CONTRIBUTING 了解详情。
安全
如果您发现任何与安全相关的问题,请发送电子邮件至 omaralwi2010@gmail.com
。
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅 LICENSE。