在表格单元格中使用FPDF进行自动换行

时间:2014-11-06 11:12:05

标签: php pdf fpdf

我在使用FPDF将文本转换为自动换行时遇到问题。我需要包装的唯一行是product_description,您可以在下面的代码中看到。我尝试过MultiCell没有成功。

有人可以帮忙吗?

感谢。

function CreatePDFQuote($var)
{
    require_once(DIR.'/classes/fpdf/fpdf.php');
    require_once(DIR.'/classes/fpdi/fpdi.php');

    $pdf = new FPDI();
    $pdf->AddPage();
    $pound_sign = html_entity_decode("£", ENT_COMPAT, 'ISO-8859-1');

    /* Image & address */
    $pdf->Image('../images/headsup_pdf.jpg', 8,8);
    $pdf->Cell(120,5,"",0,0,'',false);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','B',12);
    $pdf->Cell(40,5,"Company Limited",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(120,5,"",0,0,'',false);
    $pdf->SetFont('Arial','B',9);
    $pdf->Cell(40,5,"Address 1",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(120,5,"",0,0,'',false);
    $pdf->Cell(40,5,"Tel: 09800 000 123",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(120,5,"",0,0,'',false);
    $pdf->Cell(40,5,"sales@domain.com",0,0,'',false);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->Ln();
    $pdf->Ln();
    $pdf->Ln();

    /* Invoice number & Date */
    $pdf->SetDrawColor(218, 218, 218);
    $pdf->SetFillColor(241, 241, 241);
    $pdf->SetFont('Arial','B',9);
    $pdf->Cell(50,5,"Quote Number",1,0,'',true);
    $pdf->Cell(60,5,"Date",1,0,'',true);
    $pdf->Ln();
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(50,5,$var['pdf_quote_id'],1,0,'',true);
    $pdf->Cell(60,5,date('j/n/Y', strtotime($var['pdf_date'])),1,0,'',true);
    $pdf->Ln();
    $pdf->Ln();

    /* Company, contact & telephone */
    $pdf->SetDrawColor(218, 218, 218);
    $pdf->SetFillColor(241, 241, 241);
    $pdf->SetFont('Arial','B',9);
    $pdf->Cell(60,5,"Company",1,0,'',true);
    $pdf->Cell(60,5,"Contact",1,0,'',true);
    $pdf->Cell(50,5,"Telephone",1,0,'',true);
    $pdf->Ln();
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(60,5,$var['pdf_company_name'],1,0,'',true);
    $pdf->Cell(60,5,$var['pdf_first_name'].' '.$var['pdf_last_name'],1,0,'',true);
    $pdf->Cell(50,5,$var['pdf_telephone'],1,0,'',true);
    $pdf->Ln();
    $pdf->Ln();

    $page_height = 286.93; // page height
    $height_of_cell = 8;
    $offset = 35;
    $bottom_margin = 30;

    foreach($var['pdf_products'] as $product)
    {
        $space_left = $page_height - ($pdf->GetY() + $bottom_margin + $offset); // space left on page

        if($height_of_cell > $space_left)
        {
            $pdf->AddPage();

            /* Image & address */
            $pdf->Cell(120,5,$pdf->Image('../images/headsup_pdf.jpg',4,8),0,0,'',false);
            $pdf->SetFillColor(255, 255, 255);
            $pdf->SetFont('Arial','B',12);
            $pdf->Cell(40,5,"Heads Up PGB Limited",0,0,'',false);
            $pdf->Ln();
            $pdf->Cell(120,5,"",0,0,'',false);
            $pdf->SetFont('Arial','B',9);
            $pdf->Cell(40,5,"13 Church Hill, Epping, Essex, CM16 4RA",0,0,'',false);
            $pdf->Ln();
            $pdf->Cell(120,5,"",0,0,'',false);
            $pdf->Cell(40,5,"Tel: 0203 058 2320",0,0,'',false);
            $pdf->Ln();
            $pdf->Cell(120,5,"",0,0,'',false);
            $pdf->Cell(40,5,"sales@domain.com",0,0,'',false);
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
        }

        // Product code
        $pdf->SetDrawColor(218, 218, 218);
        $pdf->SetFillColor(241, 241, 241);
        $pdf->SetFont('Arial','B',9);
        $pdf->Cell(50,8,"Product Code",1,0,'',true);
        $pdf->Cell(60,8,$product['product_code'],1,0,'',true);
        $pdf->Cell(60,8, $pdf->Image(SITEURL.'/'.$product['product_image'], $pdf->GetX(), $pdf->GetY(), 33.78),0,0,0,false);
        $pdf->Ln();

        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(110,8,$product['product_name'],LR,0,'',true);
        $pdf->Ln();

        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(110,5,$product['product_description'],LR,0,'',true);
        $pdf->Ln();

        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(110,8,"Price includes a ".$product['logo_colours']." colour print.",LRB,0,'',true);
        $pdf->Ln();

        $pdf->SetFillColor(241, 241, 241);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(50,8,$product['quantity'].' @ '.$pound_sign.$product['unit_cost'].' each',1,0,'',true);
        $pdf->Cell(60,8,$pound_sign.$product['line_total'],1,0,'',true);
        $pdf->Ln();

        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(50,8,'1 x Origination',1,0,'',true);
        $pdf->Cell(60,8,$pound_sign.$product['origination'],1,0,'',true);
        $pdf->Ln();

        if($product['extra_costs'] > 0)
        {
            $pdf->SetFillColor(241, 241, 241);
            $pdf->SetFont('Arial','',9);
            $pdf->Cell(50,8,'Extra Costs',1,0,'',true);
            $pdf->Cell(60,8,$pound_sign.$product['extra_costs'],1,0,'',true);
            $pdf->Ln();
        }

        $pdf->SetFillColor(241, 241, 241);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(50,8,'Delivery to 1 UK Address',1,0,'',true);
        $pdf->Cell(60,8,$pound_sign.$product['delivery_charge'],1,0,'',true);
        $pdf->Ln();

        $vat = ($product['line_total'] + $product['origination'] + $product['delivery_charge'] + $product['extra_costs']) * 0.2;
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(50,8,'VAT 20%',1,0,'',true);
        $pdf->Cell(60,8,$pound_sign.number_format($vat, 2),1,0,'',true);
        $pdf->Ln();

        $total = ($product['line_total'] + $product['origination'] + $product['delivery_charge'] + $product['extra_costs'] + $vat);
        $pdf->SetFillColor(241, 241, 241);
        $pdf->SetFont('Arial','B',9);
        $pdf->Cell(50,8,'Total',1,0,'',true);
        $pdf->Cell(60,8,$pound_sign.number_format($total, 2),1,0,'',true);
        $pdf->Ln();
        $pdf->Ln();
    }

    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(110,10,"Thank you for your quote request.  Please do not hesitate to contact us, should you have any further requirements.",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(110,5,"Kind Regards,",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(110,5,"Team",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(110,5,"020 3058 2320",0,0,'',false);
    $pdf->Ln();
    $pdf->Cell(110,5,"sales@domain.com",0,0,'',false);
    $pdf->Ln();

    /* Save PDF file */
    $pdf_filename = str_replace(' ', '', $var['pdf_id']).'_'.strtotime($var['pdf_date']).'_'.uniqid().'.pdf';
    $pdf->Output(DIR.'/'.$var['pdf_folder'].'/'.$pdf_filename, 'F');

    return $pdf_filename;
}

1 个答案:

答案 0 :(得分:0)

我使用简单的概念包装了你的product_description。试试这个让我知道输出。有关详细信息,请参阅我的回答Wrap Text in Fpdf in Php

function CreatePDFQuote($var)
{
require_once(DIR.'/classes/fpdf/fpdf.php');
require_once(DIR.'/classes/fpdi/fpdi.php');

$pdf = new FPDI();
$pdf->AddPage();
$pound_sign = html_entity_decode("£", ENT_COMPAT, 'ISO-8859-1');

/* Image & address */
$pdf->Image('../images/headsup_pdf.jpg', 8,8);
$pdf->Cell(120,5,"",0,0,'',false);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,5,"Company Limited",0,0,'',false);
$pdf->Ln();
$pdf->Cell(120,5,"",0,0,'',false);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(40,5,"Address 1",0,0,'',false);
$pdf->Ln();
$pdf->Cell(120,5,"",0,0,'',false);
$pdf->Cell(40,5,"Tel: 09800 000 123",0,0,'',false);
$pdf->Ln();
$pdf->Cell(120,5,"",0,0,'',false);
$pdf->Cell(40,5,"sales@domain.com",0,0,'',false);
$pdf->Ln();
$pdf->Ln();
$pdf->Ln();
$pdf->Ln();
$pdf->Ln();

/* Invoice number & Date */
$pdf->SetDrawColor(218, 218, 218);
$pdf->SetFillColor(241, 241, 241);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(50,5,"Quote Number",1,0,'',true);
$pdf->Cell(60,5,"Date",1,0,'',true);
$pdf->Ln();
$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50,5,$var['pdf_quote_id'],1,0,'',true);
$pdf->Cell(60,5,date('j/n/Y', strtotime($var['pdf_date'])),1,0,'',true);
$pdf->Ln();
$pdf->Ln();

/* Company, contact & telephone */
$pdf->SetDrawColor(218, 218, 218);
$pdf->SetFillColor(241, 241, 241);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(60,5,"Company",1,0,'',true);
$pdf->Cell(60,5,"Contact",1,0,'',true);
$pdf->Cell(50,5,"Telephone",1,0,'',true);
$pdf->Ln();
$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(60,5,$var['pdf_company_name'],1,0,'',true);
$pdf->Cell(60,5,$var['pdf_first_name'].' '.$var['pdf_last_name'],1,0,'',true);
$pdf->Cell(50,5,$var['pdf_telephone'],1,0,'',true);
$pdf->Ln();
$pdf->Ln();

$page_height = 286.93; // page height
$height_of_cell = 8;
$offset = 35;
$bottom_margin = 30;

foreach($var['pdf_products'] as $product)
{
    $space_left = $page_height - ($pdf->GetY() + $bottom_margin + $offset); // space left on page

    if($height_of_cell > $space_left)
    {
        $pdf->AddPage();

        /* Image & address */
        $pdf->Cell(120,5,$pdf->Image('../images/headsup_pdf.jpg',4,8),0,0,'',false);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('Arial','B',12);
        $pdf->Cell(40,5,"Heads Up PGB Limited",0,0,'',false);
        $pdf->Ln();
        $pdf->Cell(120,5,"",0,0,'',false);
        $pdf->SetFont('Arial','B',9);
        $pdf->Cell(40,5,"13 Church Hill, Epping, Essex, CM16 4RA",0,0,'',false);
        $pdf->Ln();
        $pdf->Cell(120,5,"",0,0,'',false);
        $pdf->Cell(40,5,"Tel: 0203 058 2320",0,0,'',false);
        $pdf->Ln();
        $pdf->Cell(120,5,"",0,0,'',false);
        $pdf->Cell(40,5,"sales@domain.com",0,0,'',false);
        $pdf->Ln();
        $pdf->Ln();
        $pdf->Ln();
        $pdf->Ln();
        $pdf->Ln();
    }

    // Product code
    $pdf->SetDrawColor(218, 218, 218);
    $pdf->SetFillColor(241, 241, 241);
    $pdf->SetFont('Arial','B',9);
    $pdf->Cell(50,8,"Product Code",1,0,'',true);
    $pdf->Cell(60,8,$product['product_code'],1,0,'',true);
    $pdf->Cell(60,8, $pdf->Image(SITEURL.'/'.$product['product_image'],  $pdf->GetX(), $pdf->GetY(), 33.78),0,0,0,false);
    $pdf->Ln();

    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(110,8,$product['product_name'],LR,0,'',true);
    $pdf->Ln();

    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
$product_description=$product['product_description'];
$len=strlen($product_description);// check the length 
if($len>90){
$w_text=str_split($product_description,90);// split it 
 $x_axis=$pdf->getx();
$pdf->Cell(110,3,$w_text[0],'',0,'',true);
$pdf->setx($x_axis);
$pdf->Cell(110,6,$w_text[1],'',0,'',true);
$pdf->setx($x_axis);
$pdf->Cell(110,10,$w_text[2],'',0,'',true);
$pdf->setx($x_axis);
$pdf->Cell(110,5,'',LR,0,'',true);
$pdf->Ln(10);
}else{
    $pdf->Cell(110,5,$product['product_description'],LR,0,'',true);
$pdf->Ln();
}
   $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(110,8,"Price includes a ".$product['logo_colours']." colour print.",LRB,0,'',true);
    $pdf->Ln();

    $pdf->SetFillColor(241, 241, 241);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(50,8,$product['quantity'].' @ '.$pound_sign.$product['unit_cost'].' each',1,0,'',true);
    $pdf->Cell(60,8,$pound_sign.$product['line_total'],1,0,'',true);
    $pdf->Ln();

    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(50,8,'1 x Origination',1,0,'',true);
    $pdf->Cell(60,8,$pound_sign.$product['origination'],1,0,'',true);
    $pdf->Ln();

    if($product['extra_costs'] > 0)
    {
        $pdf->SetFillColor(241, 241, 241);
        $pdf->SetFont('Arial','',9);
        $pdf->Cell(50,8,'Extra Costs',1,0,'',true);
        $pdf->Cell(60,8,$pound_sign.$product['extra_costs'],1,0,'',true);
        $pdf->Ln();
    }

    $pdf->SetFillColor(241, 241, 241);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(50,8,'Delivery to 1 UK Address',1,0,'',true);
    $pdf->Cell(60,8,$pound_sign.$product['delivery_charge'],1,0,'',true);
    $pdf->Ln();

    $vat = ($product['line_total'] + $product['origination'] + $product['delivery_charge'] + $product['extra_costs']) * 0.2;
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(50,8,'VAT 20%',1,0,'',true);
    $pdf->Cell(60,8,$pound_sign.number_format($vat, 2),1,0,'',true);
    $pdf->Ln();

    $total = ($product['line_total'] + $product['origination'] + $product['delivery_charge'] + $product['extra_costs'] + $vat);
    $pdf->SetFillColor(241, 241, 241);
    $pdf->SetFont('Arial','B',9);
    $pdf->Cell(50,8,'Total',1,0,'',true);
    $pdf->Cell(60,8,$pound_sign.number_format($total, 2),1,0,'',true);
    $pdf->Ln();
    $pdf->Ln();
}

$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(110,10,"Thank you for your quote request.  Please do not hesitate to contact us, should you have any further requirements.",0,0,'',false);
$pdf->Ln();
$pdf->Cell(110,5,"Kind Regards,",0,0,'',false);
$pdf->Ln();
$pdf->Cell(110,5,"Team",0,0,'',false);
$pdf->Ln();
$pdf->Cell(110,5,"020 3058 2320",0,0,'',false);
$pdf->Ln();
$pdf->Cell(110,5,"sales@domain.com",0,0,'',false);
$pdf->Ln();

/* Save PDF file */
$pdf_filename = str_replace(' ', '', $var['pdf_id']).'_'.strtotime($var['pdf_date']).'_'.uniqid().'.pdf';
$pdf->Output(DIR.'/'.$var['pdf_folder'].'/'.$pdf_filename, 'F');

return $pdf_filename;
}