PHP code example of satthi / tcpdfwrapper

1. Go to this page and download the library: Download satthi/tcpdfwrapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

satthi / tcpdfwrapper example snippets



cpdfWrapper\TcpdfWrapper;

class hoge{

    public function fuga(){
        $TcpdfWrapper = new TcpdfWrapper();
        $TcpdfWrapper->setPrintHeader(false);
        $TcpdfWrapper->setPrintFooter(false);

        // フォント設定キャッシュファイルの出力先ディレクトリを指定(任意。指定しない場合はデフォルト /vendor/tecnickcom/tcpdf/fonts に保存される)
        $this->pdfWriter->setFontSettingCacheFileOutDir(TMP . 'pdf/');

        //独自フォント利用の場合
        $TcpdfWrapper->setFont('testfont', dirname(__FILE__) . '/fonts/○○.ttf');

        //1ページ目
        $TcpdfWrapper->addPage(dirname(__FILE__) . '/template.pdf', 1);
        $option = [
            'w' => 200,
            'h' => 0,
            'border' => 0,
            'align' => 'C',
            'fill' => false,
            'link' => '',
            'x' => 40,
            'y' => 37,
            'color' => '000000',
            //独自フォント利用
            'font' => 'testfont',
            'size' => 30,
            'fstroke' => 0.3,
        ];
        $TcpdfWrapper->setVal('山田 太郎', $option);

        $option = [
            'x' => 55,
            'y' => 110,
            'w' => 30,
            'link' => 'http://fusic.co.jp',
            'resize' => true,
            'dpi' => '300',
        ];
        $TcpdfWrapper->setImage(dirname(__FILE__) . '/test.png', $option);

        //2ページ目
        $TcpdfWrapper->addPage(dirname(__FILE__) . '/template.pdf', 2);

        $option = [
            'w' => 200,
            'h' => 0,
            'border' => 0,
            'align' => 'L',
            'fill' => false,
            'link' => '',
            'x' => 40,
            'y' => 68,
            'color' => '000000',
            'font' => 'kozgopromedium',
            'size' => 11,
            'fstroke' => 0,
        ];
        $TcpdfWrapper->setVal('福岡県テストテスト○○-○○', $option);

        $TcpdfWrapper->write(dirname(__FILE__) . '/export.pdf');
    }
}

$hoge = new hoge();
$hoge->fuga();

/**
* setPrintHeader
* ヘッダー出力のon/off
* @param boolean $pring 出力フラグ
* @author hagiwara
*/
$TcpdfWrapper->setPrintHeader(false);

/**
* setPrintFooter
* フッター出力のon/off
* @param boolean $pring 出力フラグ
* @author hagiwara
*/
$TcpdfWrapper->setPrintFooter(false);

/**
* setFont
* 独自フォントのセット
* @param string $name フォント名
* @param string $path フォントパス nullでデフォルトセット
* @author hagiwara
*/
$TcpdfWrapper->setFont('testfont', dirname(__FILE__) . '/fonts/○○.ttf');

/**
* addPage
* ページの追加
* @param string $template テンプレートパス
* @param integer $templateIndex テンプレートページ
* @author hagiwara
*/
$TcpdfWrapper->addPage(dirname(__FILE__) . '/template.pdf', 1);

/**
* setVal
*
* @param string $text テキスト
* @param array $option オプション
* @author hagiwara
*/
$option = [
    //テキストを入れるブロックのサイズ
    'w' => 200,
    'h' => 0,
    /*
    //ブロックに対する罫線
    0: 罫線を引かない(デフォルト)
    1: 罫線を引く
    L: 左
    T: 上
    R: 右
    B: 下
    */
    'border' => 0,
    /*
    L or 空文字: 左揃え(既定)
    C: 中央揃え
    R: 右揃え
    J: 両端揃え
    */
    'align' => 'C',
    //ブロック塗りつぶすか
    'fill' => false,
    //テキストにリンクを設定
    'link' => '',
    //ブロックの配置
    'x' => 40,
    'y' => 37,
    //文字色
    'color' => '000000',
    //フォントの種類
    'font' => 'kozgopromedium',
    //文字サイズ
    'size' => 30,
    // 文字の太さ 0が普通
    'fstroke' => 0.3,
];
$TcpdfWrapper->setVal('山田 太郎', $option);

/**
* setValTategaki(改行などはうまく動かないと思う
*
* @param string $text テキスト
* @param array $option オプション
* @author hagiwara
*/
// 縦書き設定は、必要に応じて`$tcpdfWrapper->setTategakiType`でメンバ変数を修正してください
$option = [
    // 1文字ごとの鷹さ
    'h' => 10,
    /*
    //ブロックに対する罫線
    0: 罫線を引かない(デフォルト)
    1: 罫線を引く
    L: 左
    T: 上
    R: 右
    B: 下
    */
    'border' => 0,
    //ブロック塗りつぶすか
    'fill' => false,
    //テキストにリンクを設定
    'link' => '',
    //ブロックの配置
    'x' => 40,
    'y' => 37,
    //文字色
    'color' => '000000',
    //フォントの種類
    'font' => 'kozgopromedium',
    //文字サイズ
    'size' => 30,
    // 文字の太さ 0が普通
    'fstroke' => 0.3,
];
$TcpdfWrapper->setValTategaki('山田 太郎', $option);

/**
* setHtml
*
* @param string $html HTML
* @param array $option オプション
* @author hagiwara
*/
$option = [
    //テキストを入れるブロックのサイズ
    'w' => 200,
    'h' => 0,
    /*
    //ブロックに対する罫線
    0: 罫線を引かない(デフォルト)
    1: 罫線を引く
    L: 左
    T: 上
    R: 右
    B: 下
    */
    'border' => 0,
    /*
    L or 空文字: 左揃え(既定)
    C: 中央揃え
    R: 右揃え
    J: 両端揃え
    */
    'align' => 'C',
    //ブロック塗りつぶすか
    'fill' => false,
    //テキストにリンクを設定
    'link' => '',
    //ブロックの配置
    'x' => 40,
    'y' => 37,
    //文字色
    'color' => '000000',
    //フォントの種類
    'font' => 'kozgopromedium',
    //文字サイズ
    'size' => 30,
];
$TcpdfWrapper->setVal('山田 太郎', $option);

/**
* setImage
*
* @param string $image 画像パス
* @param array $option オプション
* @author hagiwara
*/
$option = [
    //画像の配置
    'x' => 55,
    'y' => 110,
    //画像のサイズ。片方だけ指定で縦横比を維持してリサイズ
    'w' => 30,
    'h' => 0,
    'link' => 'http://fusic.co.jp',
    //画像自体をリサイズするか
    'resize' => true,
    //解像度
    'dpi' => '300',
];
$TcpdfWrapper->setImage($setImagePath, $option);

/**
* write
* PDFファイルの出力
* @param string $file 出力ファイル
* @author hagiwara
*/
$TcpdfWrapper->write(dirname(__FILE__) . '/hoge.pdf');