アシアル株式会社主催 開発者向け、一歩先をいくためのテクニカルセミナー

<< MySQLに接続されているのにmysql_real... 質問一覧 日付の比較について >>

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

 こんにちは。
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');

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

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

QFPDFでセルの中がうまく改行できない toichi  [07月11日 10時40分] 
┗満開Re:FPDFでセルの中がうまく改行できない weekendphp  [07月14日 15時36分] 
  ┗Re:Re:FPDFでセルの中がうまく改行できない toichi  [07月16日 20時18分] 

コメント一覧

並び替え( ツリー順 / 投稿順[降順] / 投稿順[昇順]

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

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();

参考まで

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

出来ました!!
ありがとうございました。
MySQLに接続されているのにmysql_real... 質問一覧 日付の比較について