跳轉到

獎狀範例

需求套件nextpdf/core 難度:入門

完整程式碼

<?php

declare(strict_types=1);

use NextPDF\Core\Document;
use NextPDF\Core\ValueObjects\PageSize;
use NextPDF\Core\ValueObjects\Margin;
use NextPDF\Core\ValueObjects\Position;
use NextPDF\Core\ValueObjects\Dimension;
use NextPDF\Core\ValueObjects\Color;

// ─── 文件初始化(橫向 A4)────────────────────────────────────
$document = Document::createStandalone(
    pageSize: PageSize::A4->landscape(),
    margin:   Margin::uniform(0.0),
);

$page    = $document->pages()->add();
$text    = $document->text();
$drawing = $document->drawing();

// ─── 背景圖片 ─────────────────────────────────────────────────
$background = $document->images()->embed('/assets/certificate-bg.jpg');
$drawing->image(
    image:  $background,
    x:      0.0,
    y:      0.0,
    width:  297.0,  // A4 橫向寬度(mm)
    height: 210.0,  // A4 橫向高度(mm)
);

// ─── 裝飾框線 ─────────────────────────────────────────────────
// 外框
$drawing->rectangle(
    x: 8.0, y: 8.0, width: 281.0, height: 194.0,
    stroke:       Color::fromHex('#B8860B'),
    strokeWidth:  1.5,
    cornerRadius: 3.0,
);

// 內框
$drawing->rectangle(
    x: 12.0, y: 12.0, width: 273.0, height: 186.0,
    stroke:       Color::fromHex('#DAA520'),
    strokeWidth:  0.5,
    cornerRadius: 2.0,
);

// ─── 標題 ─────────────────────────────────────────────────────
$text->write(
    text:      '榮 譽 獎 狀',
    position:  Position::at(x: 148.5, y: 30.0),
    fontSize:  28.0,
    fontName:  'NotoSansCJKtc-Bold',
    color:     Color::fromHex('#7B3F00'),
    alignment: 'center',
    maxWidth:  260.0,
    letterSpacing: 4.0,
);

// 英文副標題
$text->write(
    text:      'CERTIFICATE OF ACHIEVEMENT',
    position:  Position::at(x: 148.5, y: 45.0),
    fontSize:  11.0,
    fontName:  'Garamond',
    color:     Color::fromHex('#B8860B'),
    alignment: 'center',
    maxWidth:  260.0,
    letterSpacing: 2.0,
);

// 裝飾分隔線
$drawing->line(
    x1: 60.0, y1: 54.0, x2: 237.0, y2: 54.0,
    color: Color::fromHex('#DAA520'), width: 0.5,
);

// ─── 受獎人資訊 ───────────────────────────────────────────────
$text->write(
    text:      '茲 證 明',
    position:  Position::at(x: 148.5, y: 68.0),
    fontSize:  12.0,
    fontName:  'NotoSansCJKtc',
    color:     Color::fromHex('#5D4037'),
    alignment: 'center',
    maxWidth:  260.0,
    letterSpacing: 1.0,
);

// 受獎人姓名(主角)
$text->write(
    text:      '王 小 明',
    position:  Position::at(x: 148.5, y: 84.0),
    fontSize:  36.0,
    fontName:  'NotoSansCJKtc-Bold',
    color:     Color::fromHex('#1A237E'),
    alignment: 'center',
    maxWidth:  260.0,
    letterSpacing: 6.0,
);

// 底線裝飾
$drawing->line(
    x1: 90.0, y1: 103.0, x2: 207.0, y2: 103.0,
    color: Color::fromHex('#DAA520'), width: 0.8,
);

// ─── 獎項說明 ─────────────────────────────────────────────────
$text->write(
    text:      '在 2024 年度 NextPDF 卓越貢獻計畫中,表現傑出,',
    position:  Position::at(x: 148.5, y: 116.0),
    fontSize:  12.0,
    fontName:  'NotoSansCJKtc',
    color:     Color::fromHex('#3E2723'),
    alignment: 'center',
    maxWidth:  240.0,
    lineHeight: 1.8,
);

$text->write(
    text:      '特頒此狀,以資獎勵。',
    position:  Position::at(x: 148.5, y: 127.0),
    fontSize:  12.0,
    fontName:  'NotoSansCJKtc',
    color:     Color::fromHex('#3E2723'),
    alignment: 'center',
    maxWidth:  240.0,
);

// ─── 日期與核發單位 ───────────────────────────────────────────
$text->write(
    text:      '中華民國一百一十四年三月十五日',
    position:  Position::at(x: 148.5, y: 150.0),
    fontSize:  11.0,
    fontName:  'NotoSansCJKtc',
    color:     Color::fromHex('#5D4037'),
    alignment: 'center',
    maxWidth:  260.0,
);

// 核發機構(左右對稱)
$text->write(
    text:      'NextPDF 科技有限公司',
    position:  Position::at(x: 148.5, y: 162.0),
    fontSize:  12.0,
    fontName:  'NotoSansCJKtc-Bold',
    color:     Color::fromHex('#7B3F00'),
    alignment: 'center',
    maxWidth:  260.0,
);

// 印章圖片(示意)
$seal = $document->images()->embed('/assets/official-seal.png');
$drawing->image(
    image:   $seal,
    x:       230.0,
    y:       148.0,
    width:   45.0,
    opacity: 0.85,
);

// ─── 輸出 ────────────────────────────────────────────────────
$document->save('/output/certificate.pdf');

程式碼說明

變化延伸

批次生成獎狀

加上 QR Code 驗證碼

延伸閱讀