Вопрос по php, pdf – Как рассчитать высоту MultiCell / writeHTMLCell в TCPDF?

11

Error: User Rate Limit Exceededin advanceError: User Rate Limit Exceeded

Error: User Rate Limit ExceededTCPDF

Ваш Ответ

5   ответов
14
Error: User Rate Limit Exceededtcpdf.org/docs/srcdoc/tcpdf/class-TCPDF/#_getStringHeight
Error: User Rate Limit Exceeded
4

Error: User Rate Limit Exceeded

$yy = $this->pdf->GetY();

$check_pagebreak = $this->pdf->checkPageBreak($height+$padding,$yy,false);

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

1

Error: User Rate Limit Exceeded

  1. Calculating MultiCell heights can be a nightmare if the cells/columns end on different pages.

  2. Using transactions or additional pdf objects can make things very slow.

  3. Using functions such as getNumLines() and getStringHeight() to calculate the 'estimated' (see docs) height before the cells are printed do not always work correctly. Especially if the text ends just before or just after the right border of the cell - resulting in rows being printed on top of each other.

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

public function MultiRow($columnsArray) {
    $page_start = $this->getPage();
    $y_start    = $this->GetY();

    $pageArray  = array();
    $yArray     = array();

    // traverse through array and print one column at a time.
    $columnCount = count($columnsArray);
    for($i=0; $i<$columnCount; $i++)
    {
        if($i+1 < $columnCount)
        {
            // Current column is not the last column in the row.
            // After printing, the pointer will be moved down to
            // the right-bottom of the column - from where the
            // next multiCell in the following loop will use it
            // via $this->GetX().
            $ln = 2;
        }
        else
        {
            // Current column is the last column in the row.
            // After printing, the pointer will be moved to new line.
            $ln = 1;
        }
        $this->MultiCell(30, 0, $columnsArray[$i], 1, 'L', 1, $ln,
            $this->GetX() ,$y_start, true, 0);

        $pageArray[$i]  = $this->getPage();
        $yArray[$i]     = $this->GetY();

        // Go to page where the row started - to print the
        // next column (if any).
        $this->setPage($page_start);
    }

    // Test if all columns ended on the same page
    $samePage = true;
    foreach ($pageArray as $val) {
       if($val != $pageArray['0']) 
       {
          $samePage = false;
          break;
       }
    }

    // Set the new page and row position by case
    if($samePage == true) 
    {
        // All columns ended on the same page.
        // Get the longest column.
        $newY = max($yArray);
    }
    else
    {
        // Some columns ended on different pages.
        // Get the array-keys (not the values) of all columns that
        // ended on the last page.
        $endPageKeys = array_keys($pageArray, max($pageArray));

        // Get the Y values of all columns that ended on the last page,
        // i.e. get the Y values of all columns with keys in $endPageKeys.
        $yValues = array();
        foreach($endPageKeys as $key)
        {
            $yValues[] = $yArray[$key];
        }

        // Get the largest Y value of all columns that ended on
        // the last page.
        $newY = max($yValues);
    }

    // Go to the last page and start at its largets Y value
    $this->setPage(max($pageArray));
    $this->SetXY($this->GetX(),$newY);
}
22

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

// pdf2 set x margin to pdf1's xmargin, but y margin to zero
// to make sure that pdf2 has identical settings, you can clone the object (after initializing the main pdf object)
$pdf2 = clone $pdf;
pdf2->addpage
pdf2->writeCell
$height = pdf2->getY()
pdf2->deletePage(pdf2->getPage())
pdf1->checkPageBreak($height);
pdf1->writeCell()

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
7

Error: User Rate Limit ExceededgetStringHeightError: User Rate Limit Exceeded$heightError: User Rate Limit Exceeded

// store current object
$pdf->startTransaction();
// store starting values
$start_y = $pdf->GetY();
$start_page = $pdf->getPage();
// call your printing functions with your parameters
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='',     $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// get the new Y
$end_y = $pdf->GetY();
$end_page = $pdf->getPage();
// calculate height
$height = 0;
if ($end_page == $start_page) {
    $height = $end_y - $start_y;
} else {
    for ($page=$start_page; $page <= $end_page; ++$page) {
        $pdf->setPage($page);
        if ($page == $start_page) {
            // first page
            $height = $pdf->h - $start_y - $pdf->bMargin;
        } elseif ($page == $end_page) {
            // last page
            $height = $end_y - $pdf->tMargin;
        } else {
            $height = $pdf->h - $pdf->tMargin - $pdf->bMargin;
        }
    }
}
// restore previous object
$pdf = $pdf->rollbackTransaction();
Error: User Rate Limit Exceeded$pdf->hError: User Rate Limit Exceeded$pdf->getPageHeight(), $pdf->bMarginError: User Rate Limit Exceeded$pdf->getBreakMargin()Error: User Rate Limit Exceeded$pdf->tMarginError: User Rate Limit Exceeded$pdf->getMargins()['top']
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы