PHP 的強大之處在於其卓越的擴展性,使開發者能輕鬆整合新技術。在眾多擴展模組中,PDFLib 讓 PHP 具備動態生成 Adobe Portable Document Format (PDF) 文件的能力,廣泛應用於報表輸出、發票列印、圖表生成等場景。本文將從環境安裝、核心 API 解析、圖形繪製到實戰專案,完整介紹如何使用 PHP 操作 PDF。
在開始編碼前,必須先安裝 PDFLib 擴展庫:
php_pdf.dll,放入 PHP 擴展目錄,並在 php.ini 中啟用:extension=php_pdf.dll
或在腳本中動態載入:
dl("php_pdf.dll");
此外,請確保安裝 Adobe Acrobat Reader 以便預覽生成的 PDF 文件。
使用 PDFLib 生成 PDF 的核心流程可歸納為四步:
$pdf = PDF_new();
PDF_new() 回傳一個 PDF 文件控制代碼,後續所有操作皆需傳入此參數。
PDF_open_file($pdf, "PDFTest.pdf");
PDF_begin_page($pdf, 595, 842); // A4 尺寸,單位為 point (1 point = 1/72 inch)
頁面尺寸參數為寬、高。PHP 內建常見規格(如 A4),開發者無需手動換算。
$arial = PDF_findfont($pdf, "Arial", "host", 1); // 取得字體物件
PDF_setfont($pdf, $arial, 10); // 設定字體大小
PDF_show_xy($pdf, "This is an exam of PDF Documents, It is a good Lib,", 50, 750);
PDF_show_xy($pdf, "If you like, please try yourself!", 50, 730);
PDF_findfont() 參數依序為:控制代碼、字體名稱、編碼方式、是否嵌入字體。回傳的字體物件需傳給 PDF_setfont() 使用。座標原點 (0,0) 位於頁面左下角。
PDF_end_page($pdf);
PDF_close($pdf);
執行腳本後,PDF 文件將儲存至 PDF_open_file() 指定的路徑。
$image = PDF_open_image_file($pdf, "jpeg", "PDFImageTest.jpg");
PDF_place_image($pdf, $image, 50, 650, 0.25); // 最後一個參數為縮放比例
支援 GIF、JPEG、TIFF、PNG 格式。座標指定圖片左下角位置。
PDF_setcolor($pdf, "stroke", "rgb", 0, 0, 0); // 設定筆畫顏色 (黑色)
PDF_moveto($pdf, 20, 780); // 移動起點
PDF_lineto($pdf, 575, 780); // 設定終點
PDF_stroke($pdf); // 實際畫線
顏色採用 0~1 的百分比表示,如紅色為 1, 0, 0,黃色為 1, 1, 0。
// 黃色填充、黑色邊框的矩形
PDF_setcolor($pdf, "fill", "rgb", 1, 1, 0);
PDF_setcolor($pdf, "stroke", "rgb", 0, 0, 0);
PDF_rect($pdf, 50, 500, 200, 300); // x, y, width, height
PDF_fill_stroke($pdf);
// 綠色填充、藍色邊框的圓形
PDF_setcolor($pdf, "fill", "rgb", 0, 1, 0);
PDF_setcolor($pdf, "stroke", "rgb", 0, 0, 1);
PDF_circle($pdf, 400, 600, 100); // 圓心 x, y, 半徑
PDF_fill_stroke($pdf);
PDF_set_info_author($pdf, "net_lover");
PDF_set_info_creator($pdf, "Meng Xianhui");
PDF_set_info_title($pdf, "PHP Exam");
PDF_set_info_subject($pdf, "PHP");
PDF_set_info_keywords($pdf, "PHP PDF PDFLib");
這些資訊可在 Acrobat Reader 的「檔案 > 文件屬性 > 摘要」中查看。
以下簡化版邏輯示範如何將 POST 過來的數值轉換為 PDF 餅圖:
// 1. 接收資料並計算角度
$data = $_POST['data'];
$slices = explode(",", $data);
$sum = array_sum($slices);
$degrees = [];
foreach ($slices as $val) {
$degrees[] = ($val / $sum) * 360;
}
// 2. 建立 PDF
$pdf = PDF_new();
PDF_open_file($pdf, "chart.pdf");
PDF_begin_page($pdf, 500, 500);
$radius = 100;
$centerX = $centerY = 250;
$last_angle = 0;
$colours = [[0,0,1], [0,1,0], [1,0,0], [0,1,1], [1,1,0], [1,0,1]];
// 3. 繪製每一切片
foreach ($degrees as $i => $deg) {
PDF_setcolor($pdf, "fill", "rgb", ...$colours[$i]);
PDF_moveto($pdf, $centerX, $centerY);
$end_x = $centerX + $radius * cos(deg2rad($last_angle));
$end_y = $centerY + $radius * sin(deg2rad($last_angle));
PDF_lineto($pdf, $end_x, $end_y);
PDF_arc($pdf, $centerX, $centerY, $radius, $last_angle, $last_angle + $deg);
PDF_fill_stroke($pdf);
$last_angle += $deg;
}
// 4. 繪製外框圓並輸出
PDF_circle($pdf, $centerX, $centerY, $radius);
PDF_stroke($pdf);
PDF_end_page($pdf);
PDF_close($pdf);
// 5. 直接輸出至瀏覽器 (可選)
$buf = PDF_get_buffer($pdf);
header("Content-type: application/pdf");
header("Content-Length: " . strlen($buf));
header("Content-Disposition: inline; filename=Pie_Chart.pdf");
echo $buf;
PDF_delete($pdf);
PDFLib 不僅支援 PHP,還相容 Java、C#、VB.NET、Python、Perl、C/C++ 等多種語言,運行平台涵蓋 Windows、Linux/Unix、macOS、IBM iSeries/zSeries 等。這使其成為企業級報表系統的理想選擇。
掌握 PDFLib 核心 API 後,開發者可靈活構建各類動態 PDF 文件,從簡單文字報表到複雜圖表圖形,皆能以程式化方式高效生成,極大提升 Web 應用的文件處理能力。