使用FPDF创建多列和多页pdf

时间:2018-09-02 14:45:20

标签: php laravel fpdf

我试图在Laravel(crabbly / fpdf-laravel)中使用FPDF模块

需要创建一个1页以上的3列文档。 本质上,我有一个TERM:DESCRIPTION对列表,我需要一个对另一个输出。我在整体逻辑上苦苦挣扎,甚至不确定自己是否使用正确的方法,并且想知道是否有更好的解决方案(熟悉FPDF的人)

  1. 获取“术语:描述”对列表($术语表)
  2. 将每个“描述”分成长度不超过75个字符的行
  3. 计算该列中已有多少行
  4. 在行中添加行
  5. 当行数超过每列51行的定义长度时切换页面/列

这是我想出的代码:

public function getCreatePDF($flascard_id) {
    $flashcard = Flashcard::where('user_id', auth()->id())->findOrFail($flascard_id);

    $glossary = Glossary::with('flashcarditem')
            ->whereHas('flashcarditem', function ($query) use ($flascard_id) {
                $query->where('flashcard_id', '=', $flascard_id);
            })
            ->get();

    $pdf = app('FPDF');
    $pdf->col = 0;
    $pdf->AcceptPageBreak(false);
    //My Loop Controls
    $column = 0; //Control which column to add data
    $columnLenght = 0; //monitor if we need to change the column
    $maxLinesPerColumn = 51;
    $wrapCharacters = 75; //do not exceed this char qty per line
    $collectedDescription = ""; //stack Description here

    $pdf->AddPage(['L', 'Letter']);
    $pdf->SetFont('Arial', 'B');
    $pdf->SetFontSize(18);
    $mid_x = 135; // the middle of the "PDF screen", fixed by now.        
    $pdf->Text($mid_x - ($pdf->GetStringWidth('TITLE') / 2), 102, 'TITLE');
    $pdf->AddPage(['L', 'Letter']);
    $pdf->SetFontSize(7);

    foreach ($glossary as $key => $g) {
        $chunks = explode("\n", wordwrap(strip_tags($g->description), $wrapCharacters));
        $actionCollect = 1;
        $collectedDescription = "";


        for ($i = 0; $i < count($chunks); $i++) {
            if ($columnLenght > $maxLinesPerColumn && $column == 2) {
                $column = 0;
                $columnLenght = 1;

                $pdf->AddPage(['L', 'Letter']);
                $pdf->Footer("d");
                $pdf->col = $column;
                $x = 10 + $column * 95;
                $pdf->SetLeftMargin($x);
                $pdf->SetX($x);
                $pdf->SetY(8);
                $actionCollect = 0;
            } else if ($columnLenght > $maxLinesPerColumn && $column < 2) {
                $columnLenght = 1;
                $column++;
                $pdf->col = $column;
                $x = 10 + $column * 95;
                $pdf->SetLeftMargin($x);
                $pdf->SetX($x);
                $pdf->SetY(8);
                $actionCollect = 0;
            } else {
                $columnLenght ++;
                $actionCollect = 1;
            }

            if ($i == 0) {
                $columnLenght ++;
                $pdf->SetFont('Arial', 'B');
                $term = html_entity_decode($g->term);
                $pdf->MultiCell(90, 3, $term, 'R');
                $pdf->Ln(0);
            }

            $collectedDescription.=strip_tags($chunks[$i]);

            if (count($chunks) - 1 && $i == 0) {

                $pdf->SetFont('Arial');
                $pdf->MultiCell(90, 3, $collectedDescription, 'R');
                $actionCollect = 0;
                $collectedDescription = "";
            } else {
                $pdf->SetFont('Arial');
                $pdf->MultiCell(90, 3, strip_tags($chunks[$i]), 'R');
                $actionCollect = 0;
                $collectedDescription = "";
            }
        }

        $pdf->Ln(1);
    }
}

我想要的状态

desired state

0 个答案:

没有答案