1

PDFファイルを生成する簡単なスクリプトがあります。データベースから必要な情報を取得し、を使用しforeachて PDF ファイルのページを作成しています。最後のページには、DB から取得されていないグラフやその他の情報がいくつかあります。この最後のページを除くすべてのページには、サブヘッダーが必要です。

私の現在のコードは次のとおりです。

foreach ($sql as $key => $value) 
{
    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['value'], '', '', 'L');
}

基本的に、各行を説明するサブヘッダーが必要です。しかし、これを行うと、すべての行にサブヘッダーが含まれます。

foreach ($sql as $key => $value) 
{
    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
    $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');

    // 

    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

最後のページを除いて、各ページにこのサブヘッダーが必要です。pageNo()関数でクレイジーなコードを実行しようとしましたが、うまくいきませんでした。

ありがとう!

4

3 に答える 3

1

クラスでFPDFのメソッドheader()をオーバーライドします。

class PDF extends FPDF
{
    public function Header() {
        $this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R');
    }
}

このメソッドは、新しいページを追加するときに FPDF によって自動的に呼び出されます。

于 2016-01-04T05:43:03.013 に答える
1

わかりました、私は問題を解決しました。似たようなものを探している人のために、私がやったことは次のとおりです。GetY()関数はその行の Y 位置を返すため、ページの最初の行の場合、この値は 0 (または定数、レイアウトによって異なります) になります。今やりました:

foreach ($sql as $key => $value) 
{

    if ($pdf->GetY() == 0)
    {
        $pdf->SetFont('Arial', 'B', $font_size);
        $pdf->Ln(2 * $line_height);

        $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
        $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
        $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
        $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');
    }

    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

皆さん、ありがとうございました!

于 2013-03-21T18:36:16.623 に答える
0

各ページでヘッダーを取得しようとして、同じ問題が発生しました。これは、MySql データベースにも接続します。これが私のために働いたものです。

<?php
//PDF USING MULTIPLE PAGES
//FILE Originally CREATED BY: Carlos José Vásquez Sáez
//I fixed this on May 3/2013 as it was not working correctly for me.

define('FPDF_FONTPATH', '/font/');
require('fpdf.php');

//Connect to your database
$link = mysql_connect("localhost","","") or die ('Could not select database.');
$db_select = mysql_select_db("", $link) or die ('Could not select database.');


//Create new pdf file
$pdf=new FPDF();

//Open file
$pdf->Open();

//Disable automatic page break
$pdf->SetAutoPageBreak(false);

//Add first page
$pdf->AddPage();

//set initial y axis position per page
$y_axis_initial = 10;

//Set Row Height
$row_height = 8;

//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
$pdf->Cell(80, $row_height, 'Products', 1, 0, 'C', 1);
$pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1);
$pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1);

//Select the Products you want to show in your PDF file
$result=mysql_query('SELECT Product,Price,Customer FROM your_table ORDER BY Product', $link);

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 30;

//Data Table y axis position starting point
$y_axis = $y_axis_initial + $row_height;

while($row = mysql_fetch_array($result))
{
    //If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage();

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetX(10);
        $pdf->Cell(80, $row_height, 'Product', 1, 0, 'C', 1);
        $pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1);
        $pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1);

        //Set $i variable to 0 (first row)
        $i = 0;
    //Reset the y axis value
    $y_axis = $y_axis_initial + $row_height;;
    }

    $Product = $row['Product'];
    $Price = $row['Price'];
    $Cust = $row['Customer'];

    $pdf->SetY($y_axis);
    $pdf->SetX(10);
    $pdf->Cell(80, $row_height, $Product, 1, 0, 'L', 1);
    $pdf->Cell(20, $row_height, $Price, 1, 0, 'L', 1);
    $pdf->Cell(30, $row_height, $Cust, 1, 0, 'L', 1);

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
}

mysql_close($link);

//Create file
$pdf->Output();
于 2013-05-04T00:45:10.057 に答える