PDF ??? ?? Hyperf ?????? ???? ??
Oct 25, 2023 pm 12:40 PMHyperf ?????? ???? PDF? ????? ???? ?? ??? ?????.
??? ??? ????? PDF(Portable Document Format) ??? ??? ??? ???? ??? ??? ???. PDF ?? ??? ???? ???? ?????? ?? ?????? ?? ?? ?????. ? ????? PDF ??? ???? ?? ???? ?? ?????. ? ????? Hyperf ?????? ???? PDF ??? ???? ??? ???? ??? ?? ??? ?????.
?? Hyperf ?????? ?? ?? ???? ???? ???. Composer? ?? Hyperf ?????? ???? ?? ??? ??? ? ????.
composer create-project hyperf/hyperf my-project
??? ??? ? PDF ??? ?? ?? ?????? ???? ???. ? ????? PDF ??? ?? ?????? tcpdf? ?????. Composer? ?? tcpdf ?????? ???? ?? ??? ??? ? ????.
composer require tecnickcom/tcpdf
??? ???? ?? ??? ??? ? ????. ?? PDF ?? ????? ???? ???. Hyperf ??????? ????? HTTP ??? ???? ?? ??? ???? ? ?????. ?? ??? ???? ????? ??? ? ????.
php bin/hyperf.php generate:controller PdfController
?? ? app/Controller
??? PdfController.php
??? ?????. ? ??? PDF? ???? ??? ??? ? ????. app/Controller
文件夾下會生成一個 PdfController.php
文件。我們可以在這個文件中編寫生成PDF的代碼。
首先,我們需要引入tcpdf庫以及一些必要的類。在 PdfController.php
文件的開頭,添加以下代碼:
use TCPDF; use PsrHttpMessageResponseInterface; use HyperfHttpServerAnnotationController; use HyperfHttpServerAnnotationRequestMapping;
接下來,在 PdfController
類中添加一個方法,用于生成PDF文件。例如,我們創(chuàng)建一個叫做 generate
的方法:
/** * @RequestMapping(path="/pdf/generate", methods="GET") */ public function generate(): ResponseInterface { $pdf = new TCPDF(); // 創(chuàng)建一個TCPDF實例 // 設(shè)置PDF的屬性 $pdf->SetCreator('Hyperf Framework'); $pdf->SetAuthor('Your Name'); $pdf->SetTitle('Sample PDF'); $pdf->SetSubject('Generating PDF using Hyperf Framework'); $pdf->SetKeywords('PDF, Hyperf'); // 添加一頁 $pdf->AddPage(); // 設(shè)置內(nèi)容 $content = 'Hello, this is a sample PDF generated using Hyperf Framework.'; $pdf->writeHTML($content, true, false, true, false, ''); // 輸出PDF文件 $pdf->Output('sample.pdf', 'D'); }
在上面的代碼中,我們首先創(chuàng)建了一個TCPDF實例。然后,設(shè)置了PDF的屬性,包括創(chuàng)建者、作者、標(biāo)題和關(guān)鍵字。接著,我們添加了一頁并設(shè)置了內(nèi)容。最后,通過調(diào)用 Output
方法將生成的PDF文件發(fā)送到瀏覽器。
最后一步,我們需要在路由中注冊我們的控制器。在 routes.php
文件中添加以下代碼:
use AppControllerPdfController; Router::addRoute(['GET'], '/pdf/generate', [PdfController::class, 'generate']);
現(xiàn)在,我們已經(jīng)完成了使用Hyperf框架生成PDF文件的全部過程。通過訪問 /pdf/generate
,就能夠生成一個名為 sample.pdf
PdfController.php
?? ?? ??? ?? ??? ?????. rrreee
???? PDF ?? ??? ??PdfController
???? ???? ?????. ?? ?? generate
?? ???? ????. ??rrreee??? ????? ?? TCPDF ????? ????. ?? ?? ???, ???, ?? ? ???? ???? PDF? ??? ?????. ???? ???? ???? ???? ??????. ????? ??? PDF ??? Output
???? ???? ????? ?????. ??????? ????? ??? ????? ???? ???. routes.php
??? ?? ??? ?????. ??rrreee???? Hyperf ?????? ???? PDF ??? ???? ?? ????? ???????. /pdf/generate
? ????? sample.pdf
?? PDF ??? ???? ??? ????? ? ????. ?????? ??? ?? ??? Hyperf ??????? tcpdf ?????? ???? PDF ??? ???? ??? ?????. ? ?? ?? ?? ??? ?? ??? PDF? ??? ??? ??? ??? ? ????. ? ??? ??? ??? ????! ??? ??? PDF ??? ?? Hyperf ?????? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

FPDF? PDF(PortableDocumentFormat) ??? ???? ? ???? PHP ??? ????????. ?, ???, ???, ??? ?? ???? PHP ?????? ??? ??? PDF ??? ??? ? ????. FPDF? ???? ??? ?????? ????? ???? ??? ??? PDF ??? ?? ?? ? ????. ? ????? ??, PDF ?? ??, ??? ? ??? ??, ??? ???? ?? ? ??? ???? FPDF? ???? PDF ??? ???? ??? ??? ?????.

??? ? ?? ??? ?? Hyperf ?????? ???? ?? ??: ?? ???? ?????? ???? ??? ? ??? ???? ?? ??? ?????. ?????? ??? ??? ??? ???? ??? ??? ????? ?? ??? ? ?? ??? Hyperf ?????? ???? ?? ?? ???????. ? ????? ??? ? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?????. 1. ??? ??? ???? XMLHttpReques? ?? ?????? ???? JavaScript? ?????.

?? ??? ?? Hyperf ?????? ????? ???? ?? ??? ?????. Hyperf? Swoole ??? ???? ??? ??? PHP ??????, ???, ??? ??, AOP, ????, ??? ?? ?? ??? ??? ??? ????. ???, ???? ?? ??? ? ?????? ? ???????? ???? ? ?????. ?? ??????? ??? ???? ???? ?? ??? ????. Hyperf ?????? ?? ?? ??? ????? ? ??? ?? ? ?? ??? ?? ??? ??? ?????. ? ????? ?? ??? ?????.

?? ??? ?? Hyperf ?????? ????? ?? ?? ??? ?????. ??: ????? ?? ?????? ??? ??? ??? ???? ???? ???? ???. ??? PHP ?? ?????? Hyperf ?????? ???? ?? ??? ???? ? ??? ?? ??? ??? ??? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?? ?? ?????. 1. ?? ?? ?? ?? Hyperf ?????? ? ?? ???? ??? ?????.

?? ??? ?? Hyperf ?????? ???? ?? ??: Hyrpf? ??? ?? ??? ??? ?? PHP ?? ??? ???, ?? ??? ??? ????????. ?? ??? ?? ????? ???? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?????. 1. Hyperf ????? ?? ?? Hyperf ?????? ???? ???. Composer? ?? ??? ? ???, ??? ??? ?? ?? ??? ?????.

JWT ??? ?? Hyperf ?????? ???? ?? ??: Hyperf? Swoole ??? ??? ??? ?????? ??? ??? ??? ???? ?????. JWT(JSONWebToken)? ??? ???? ???? ?? ??? ?????. ? ????? Hyperf ??????? JWT ??? ???? ??? ???? ???? ?? ??? ?????. 1. ??? ??? ?? ?? hyperf/jwt ? lcobucci/jw? ???? ???.

?? ???? ?? Hyperf ?????? ???? ?? ??: ?? ???? ?? ??? ??? ???? ??? ? ?? ????? ??? ??? ?????. Hyperf? Swoole ??? ???? ??? ??? ?????? ?? ???? ???? ?? ??? ??? ?? ? ?? ??? ?????. ? ????? ?? ???? ?? Hyperf ?????? ???? ??? ???? ? ?? ?? ?? ??? ?????. 1. ?? ?? ?? ???? ???? ?? Hyperf ??????

?? ???? ?? Hyperf ?????? ???? ?? ??: ???? ????? ?? ???? ?? ????? ??????? ?? ??? ?????. ?3? ???? ?? ???? ?3? ???? ?? ?? ??? ???? ?? ????? ??????? ???? ? ???? ???? ?? ??? ??? ??? ??? ?? ???? ? ????. ? ????? ?? ?? ??? ?? Hyperf ?????? ???? ?? ??? ??? ???? ??? ?????. 1. ???? ?3? ??? ??? ???? ??,
