使用PHP FPDF包装长文本的问题

时间:2018-09-08 11:27:10

标签: php pdf fpdf

enter image description here

我有一个如上图的报告,标题栏文本太长,我想在此单元格上进行文本换行,但是它不起作用并在垂直列中显示数据。我阅读了FPDF文档,并尝试了很多,但仍然无法正常工作,这是我通过代码得到的结果:

enter image description here

我该如何包装第一列文字?

function toPDF($data, $cols = NULL, $cols_event = NULL){
 global $action, $meta;  

if ($cols) {
    // translate column weighting to page width in mm
    $colno = 0;
    $max_col = count($cols);
    foreach ($cols as $col) {
        $colno += $col[0];
    }
    foreach ($cols as $key => $col) {
        $cols[$key][0] = (int)($col[0] / $colno * 196);
    }
} elseif($cols_event){
    $colno = 0;
    $max_col = count($cols_event);
    foreach ($cols_event as $col) {
        $colno += $col[0];
    }
    foreach ($cols_event as $key => $col) {
        $cols[$key][0] = (int)($col[0]);
    }
} else {
    $max_col = 0;
    foreach ($data as $line) {
        if (is_array($line)) $max_col = max(count($line), $max_col);
    }
    $width = $max_col == 0 ? 196 : 196 / $max_col;
    $cols = array();
    for ($cc = 0; $cc < $max_col; $cc++) {
        $cols[] = array($width, "");
    }
}


class PDF extends FPDF{

    function Header()
    {
    }

    function Footer()
    {
        //Go to 1.5 cm from bottom
        $this->SetY(-15);
        //Select Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        //Print centered page number
        $this->Cell(0, 10, 'Page ' . ($this->page . ' of {nb}'), 0, 0, 'C');
    }

}

// $pdf = new PDF('P', 'mm', 'Letter');
$pdf = new PDF('P','mm',array(550,300));
$pdf->SetDisplayMode("real", "continuous");
$pdf->AliasNbPages();
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(200, 200, 200);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetLineWidth(.1);
$pdf->SetLeftMargin(10);
$pdf->AddPage();

$pdf->Cell(190,10, $meta['title'],"B",1,"C",0);

$first = true;
foreach ($data as $line) {
    if (is_array($line)) {
        for ($field = 0; $field < $max_col; $field++) {
            if (isset($line[$field])) {
                if(is_array($line[$field])) {
                    $pdf->Cell($cols[$field][0], 6, strip_tags($line[$field][0]), 1, 0, $cols[$field][1], 1);
                } else {
                    if($first) {
                        $pdf->Cell($cols[$field][0], 6, strip_tags($line[$field]), 1, 0, 'L', 1);
                    } else {
                        $pdf->MultiCell($cols[$field][0], 6, strip_tags($line[$field]));
                    }
                }
            } else {
                $pdf->Cell($cols[$field][0], 6, "", 1, 0, 'L', 1);
            }
        }
    }
    $pdf->Ln(6);
    $first = false;
}

$pdf->Output($action . ".pdf", "I");
}

2 个答案:

答案 0 :(得分:2)

最后,我通过自定义FPDF示例中的代码解决了该问题。这是链接http://www.fpdf.org/?go=script&id=3

@Marco Messina谢谢你帮了我很多忙

class HomeViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    setupNavBar()
    view.addSubview(collectionView)
    setUpConstraints()
    configure(collectionView: collectionView)
}
func setUpConstraints() {
    _ = collectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 10, leftConstant: 10, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 0)
    collectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}

lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.alwaysBounceVertical = true
    cv.clipsToBounds = true
    cv.showsHorizontalScrollIndicator = false
    cv.showsVerticalScrollIndicator = false
    cv.backgroundColor = .clear
    cv.isHidden = false
    return cv
}()
}
private let reuseIdentifier = "Cell"
extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
internal func configure(collectionView: UICollectionView) {
    collectionView.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 7
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HomeCollectionViewCell
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.bounds.width, height: 600)
}
}

答案 1 :(得分:1)

嘿,我一直在寻找解决方案,因为我发现了一些东西!我认为我们可以将getY()(在多单元格之前)和getY(在多单元格之后)的差值用作值,以便设置正确的坐标。为了使此工作有效,我必须进行一些小的更改。

现在唯一的问题只是边缘。而是正确设置了单元格。

  • 我在“ foreach($数据作为$行)”行之前设置了三个变量:

    $ y1 = $ pdf-> GetY();

    $ i = 1;

    $ y2 = 1;

  • 我修改了具有Multicell的代码段:

                    $pdf->SetLeftMargin(20);                    
                    $x = $pdf->GetX();
    
                    $y = $pdf->GetY();
    
                    $pdf->MultiCell($cols[$field][0], 6, strip_tags($line[$field]), "L T R");
    
                    $y2 = $pdf->GetY();
    
                    $i++;
    
                    if($y2 > $y1 ){
                        $y1 = $y2;  
                    }
    
                    if($i > 5){
                        $pdf->SetXY($x + $cols[$field][0], $y1 - 6);
                        $i = 1;                 
                    }else{
                        $pdf->SetXY($x + $cols[$field][0], $y);
                    }   
    

This is a photo of a test