Search code examples
phptransactionstcpdffpdf

TCPDF Transaction: Empty font family


I am using TCPDF to generate a PDF and I get the following error when i use transaction: TCPDF ERROR: Empty font family

I have the following code snippet(with transaction for pagebreak):

            $titleDesc = $sPDFQuestion;
            $pageNum = $this->pdf->PageNo();
            $this->pdf->startTransaction();

            $this->pdf->Bookmark($sPDFQuestion, 1, 0);

            $this->pdf->titleintopdf($pdfTitle, $sPDFQuestion);
            if($pageNum != $this->pdf->PageNo()){
               $this->pdf->rollbackTransaction(false);
                $this->pdf->AddPage('P', 'A4');
                $this->pdf->Bookmark($sPDFQuestion, 1, 0);
                $this->pdf->titleintopdf($pdfTitle, $sPDFQuestion);
            }
            else {
                $this->pdf->commitTransaction();
            }

This is the function titleintopdf():

    public function titleintopdf($title, $description = '')
{
    if (!empty($title)) {
        $title = $this->delete_html($title);
        $oldsize = $this->FontSizePt;
        $this->SetFontSize($oldsize + 4);
        $this->Line(5, $this->y, ($this->w - 5), $this->y);
        $this->ln(3);
        $this->MultiCell('', '', $title, '', 'C', 0);
        $this->MultiCell('', '', "Number:".$this->PageNo(), '', 'C', 0);
        if (!empty($description) && isset($description)) {
            $description = $this->delete_html($description);
            $this->ln(7);
            $this->SetFontSize($oldsize + 2);
            $this->MultiCell('', '', $description, '', 'C', 0);
            $this->ln(2);
        } else {
            $this->ln(4);
        }
        $this->MultiCell('', '', "Number:".$this->PageNo(), '', 'C', 0);

        $this->Line(5, $this->y, ($this->w - 5), $this->y);
        $this->ln(5);
        $this->SetFontSize($oldsize);
    }
}

When I don't rollback the transaction and I just commit it instead, everything works fine. I don't have a clue, why this error occurs. Do you know what the problem could be?

Greets!


Solution

  • The error lies in

    $this->pdf->rollbackTransaction(false);
    

    'false' means here to not restore $this->pdf to its original state, but to return the original state as a TCPDF object, so correct would be either:

    $this->pdf = $this->pdf->rollbackTransaction(false);
    

    or

    $this->pdf->rollbackTransaction(true);
    

    The error "TCPDF ERROR: Empty font family" is just a follow-up error of $this->pdf not being valid anymore.