ข้ามไปยังเนื้อหา

เริ่มต้นใช้งานอย่างรวดเร็ว — PDF แรกของคุณใน CodeIgniter 4

ในคอนโทรลเลอร์ ให้รีโซลฟ์เซอร์วิส pdf เพิ่มเนื้อหาลงในเอกสาร แล้วส่งคืนการตอบสนองแบบดาวน์โหลด เพียงเท่านี้ก็จะได้ไฟล์ Portable Document Format (PDF) ภายในสามบรรทัด พร้อมการตอบสนอง Hypertext Transfer Protocol (HTTP) เพียงครั้งเดียว

Terminal window
composer require nextpdf/codeigniter

ดูข้อกำหนดและการตรวจสอบการค้นพบแพ็กเกจได้ที่ /integrations/codeigniter/install/

สร้างคอนโทรลเลอร์ที่ส่งคืน PDF โดยไลบรารี Pdf จะสร้างเอกสารใหม่ให้ แล้วแปลงเอกสารนั้นเป็น DownloadResponse ของ CodeIgniter

ตัวอย่าง PHP ทั้งหมดในหน้านี้วาง declare(strict_types=1); ไว้หลังแท็กเปิด โดยอยู่บนบรรทัดของตนเอง (PSR-12 §x1.x3.p11; §x1.x3.p34)

<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\DownloadResponse;
use NextPDF\CodeIgniter\Config\Services;
final class InvoiceController extends BaseController
{
public function download(int $id): DownloadResponse
{
$pdf = Services::pdf();
$pdf->document()->addPage();
$pdf->document()->cell(0, 10, "Invoice #{$id}");
return $pdf->download("invoice-{$id}.pdf");
}
}

Services::pdf() จะส่งคืน Pdf ใหม่ ชุดทดสอบเชิงฟังก์ชันของแพ็กเกจยังตรวจสอบว่าเอกสารเบื้องหลังเป็นเอกสารใหม่ในการเรียกแต่ละครั้งด้วย การเรียก download() จะสร้าง DownloadResponse โดยมี attachment เป็น disposition

แมปเส้นทางกับเมธอดของคอนโทรลเลอร์ใน app/Config/Routes.php:

$routes->get('invoices/(:num)/pdf', 'InvoiceController::download/$1');

เปิด /invoices/42/pdf แล้วเบราว์เซอร์จะดาวน์โหลด invoice-42.pdf การตอบสนองนี้มี Content-Type: application/pdf และเฮดเดอร์เสริมความปลอดภัยของการตอบสนองจากแพ็กเกจ

รูปแบบทางเลือก — แสดงตัวอย่างแบบอินไลน์ด้วยตัวช่วย

หัวข้อที่มีชื่อว่า “รูปแบบทางเลือก — แสดงตัวอย่างแบบอินไลน์ด้วยตัวช่วย”

ตัวช่วยส่วนกลาง pdf() เทียบเท่ากับ Services::pdf() ให้เรียก inline() เพื่อแสดง PDF ในเบราว์เซอร์แทนการดาวน์โหลด:

<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\DownloadResponse;
final class ReportController extends BaseController
{
public function preview(): DownloadResponse
{
$pdf = pdf();
$pdf->document()->addPage();
$pdf->document()->cell(0, 10, 'Monthly Report');
return $pdf->inline('report.pdf');
}
}

หากมี Document อยู่แล้ว ให้สร้างการตอบสนองโดยตรงด้วย PdfResponse ได้ ตัวช่วย pdf_document() จะให้เอกสารใหม่ที่กำหนดค่าไว้ล่วงหน้า

<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\DownloadResponse;
use NextPDF\CodeIgniter\Http\PdfResponse;
final class DocumentController extends BaseController
{
public function generate(): DownloadResponse
{
$document = pdf_document();
$document->addPage();
$document->cell(0, 10, 'Hello World');
return PdfResponse::download($document, 'hello.pdf');
}
}
  • คอนโทรลเลอร์ที่รีโซลฟ์เซอร์วิส NextPDF และส่งคืน DownloadResponse แบบระบุชนิด
  • จุดเริ่มต้นที่เทียบเท่ากันสองแบบ: คลาส Services และตัวช่วย pdf() / pdf_document()
  • การตอบสนองที่มี application/pdf เฮดเดอร์ความปลอดภัยของแพ็กเกจ และชื่อไฟล์ที่ผ่านการทำความสะอาดแล้ว

เพื่อให้บทแนะนำเบื้องต้นนี้กระชับ จึงไม่ได้กล่าวถึงการจัดการข้อผิดพลาด สำหรับคอนโทรลเลอร์ระดับโปรดักชันที่มี dependency injection การจัดการ exception การสังเกตการณ์ และงานในคิว โปรดดูต่อที่ /integrations/codeigniter/production-usage/ หน้านั้นจะแสดงรูปแบบที่ช่วยเสริมความปลอดภัย

  • แท็กเปิด PHP อยู่บนบรรทัดของตนเองในตัวอย่างโค้ด (PSR-12 Extended Coding Style §x1.x3.p11)
  • รูปแบบคำสั่ง declare(strict_types=1) ในตัวอย่างโค้ด (PSR-12 Extended Coding Style §x1.x3.p34)
  • /integrations/codeigniter/overview/ — application programming interface (API) ทั้งหมด
  • /integrations/codeigniter/configuration/ — การเปลี่ยนค่าเริ่มต้นและพาธ
  • /integrations/codeigniter/production-usage/ — คอนโทรลเลอร์ระดับโปรดักชันและงานแบบอะซิงโครนัส
  • /integrations/codeigniter/troubleshooting/ — เมื่อเส้นทางไม่ส่งคืน PDF