FPDFでセルの中がうまく改行できない - PHPプロ!Q&A掲示板

1298

  • 0P

FPDFでセルの中がうまく改行できない

質問日時 / 2008年7月11日 10:40    回答数 / 2件

Questioner:  toichi  このエントリーをはてなブックマークに追加 

キーワード / FPDF    改行    cell   

 こんにちは。
FPDFで出力する請求書フォーマットを作成しています。
例えば
伝票番号・件名・数量・金額
の並びで明細部分を作成しました。

これらを表示するのにcellを使っているのですが、ここで件名の文字数を多くすると件名を突き抜けて、数量と文字が重なってしまいました。
cellでは改行できないようなので、multicellを使ってみたのですが、cellのように横並びができませんでした。
あと、改行できてもcellの高さは変えないようにしたいです。

下記のソースは明細を表示する部分です。for文使って、行数文繰り返しています。

  1. $pdf->setFont(MINCHO,'',10);
  2. $pdf->cell(16,6,$dendt_m[$i].'月'.$dendt_d[$i].'日',1,0,'C');
  3. $pdf->cell(16,6,$denno[$i],1,0,'C');
  4. $pdf->cell(10,6,$denkb[$i],1,0,'R');
  5. $pdf->setFont(MINCHO,'',8);
  6.  
  7. $pdf->cell(70,6,$kenmei[$i],1,0,'L');
  8.  
  9. $pdf->setFont(MINCHO,'',10);
  10. $pdf->cell(10,6,$su[$i],1,0,'R');
  11. $pdf->cell(22,6,number_format($tanka[$i]),1,0,'R');
  12. $kn=number_format($tanka[$i]*$su[$i]);
  13. $pdf->cell(22,6,$kn,1,0,'R');
  14. $pdf->setFont(MINCHO,'',8);
  15. $pdf->cell(34,6,$bikou[$i],1,1,'L');

ご教授の程、よろしくお願い致します。

この質問への意見の募集は締め切られ、ポイントは既に配分されました。
意見を投稿することはできますが、ポイントを受け取ることはできません。



ツリー一覧

┗A01weekendphpsetXYとかやりながら、自分で改行処理をコツコツやる
 ┗A01-1toichi出来ました!! ありがとうございました。

回答一覧

並び替え:

A01 満足
answererweekendphp [7月14日 15:36]

setXYとかやりながら、自分で改行処理をコツコツやるしかないような気がします。

適当ですが、下記みたいな感じでしょうか。。。。

  1. <?php
  2.  
  3. require 'fpdf.php';
  4.  
  5. define('X_COL_HEIGHT'12);
  6. // 幅
  7. $cols = array(10,20,40,50,70);
  8. // 表示データ
  9. $data = array(
  10.         array('col1''col2''col3''col4''col5'),
  11.         array('col1''col2''col3''col4''this is test message.this is test message.this is test message.'),
  12.         array('col1''col2''col3''this is test message.this is test message.''col5'),
  13.         array('col1''col2''this is test message.this is test message.''col4''col5'),
  14.         array('col1''this is test message.''col3''col4''col5'),
  15.         array('col1''col2''col3''this is test message.this is test message.''this is test message.this is test message.this is test message.this is test message.this is test message.this is test message.')
  16.     );
  17.  
  18. $pdf = new FPDF();
  19. $pdf->AddPage();
  20.  
  21. $pdf->SetFont('Arial'''9);
  22.  
  23. // 表の描画
  24. foreach ($data as $line) {
  25.     foreach ($cols as $colNo => $colWidth) {
  26.         $str = $line[$colNo];
  27.         $lines = array();
  28.  
  29.         $_x = $pdf->getX();
  30.         $_y = $pdf->getY();
  31.  
  32.         // Widthを越えないように調整
  33.         do {
  34.             $_tmpStr = '';
  35.  
  36.             // widthを越えていたら
  37.             while ($pdf->GetStringWidth($str) > $colWidth) {
  38.                 // 最後の一文字短くする
  39.                 $_tmpStr = substr($str, -1) . $_tmpStr;
  40.                 $str = substr($str0, -1);
  41.             }
  42.  
  43.             $lines[] = $str;
  44.             $str = $_tmpStr;
  45.         } while ($str != '');
  46.  
  47.         $colHeight = X_COL_HEIGHT / count($lines);
  48.         for ($i = 0$i < count($lines)$i++) {
  49.             if (count($lines) == 1) {
  50.                 $border = 'TBLR';
  51.             } else if ($i == 0) {
  52.                 $border = 'TLR';
  53.             } else if ($i == count($lines) - 1) {
  54.                 $border = 'BLR';
  55.             } else {
  56.                 $border = 'LR';
  57.             }
  58.  
  59.             $pdf->setXY($_x$_y + $colHeight * $i)// Yは行数に応じて調整
  60.             $pdf->Cell($colWidth$colHeight$lines[$i]$border);
  61.         }
  62.  
  63.         // 次のセル開始位置へ移動
  64.         $pdf->setXY($_x + $colWidth$_y);
  65.     }
  66.  
  67.     // 改行
  68.     $pdf->Ln();
  69.     $pdf->setY($_y + X_COL_HEIGHT);
  70. }
  71.  
  72. $pdf->Output();

参考まで

この意見に回答する

ツリーへ TOPへ

A01-1
replyertoichi [7月16日 20:18]

出来ました!!
ありがとうございました。

この意見に回答する

ツリーへ TOPへ

<<質問一覧へ



Pick Up Q&A

Q
PHPのHTML埋め込み記述について
 このエントリーをはてなブックマークに追加 
A
$_POST["data"] == "男" ? $val = "checked" : $val = "" ; の意味は以下と同じです。 if($_POST["data"] == "男"){ $val = "checked; } e...

>>続きを読む

kende様のご指摘通り、三項演算子を使用する際には、コードの複雑度などを考慮する必要がありますね。書きやすさと共に可読性も追求したいところですね。

▲解説者:岡本(アシアル株式会社 教育コーディネーター兼 システムエンジニア)