How to use TCPDF in Form Custom Button

Hi All,
I am trying to use the TCPDF in buillt library. This library is available in the external library section of scriptcase.
I am using a form. I have added a custom button PHP (I tried AJAX too), to the Form.
Then in the Button I put the following statement
$pdf = $this->Pdf;
$pdf->SetFont(‘helvetica’, ‘N’, 9);
$pdf->SetTextColor(130);
$pdf->Multicell(80, 5, $end, // w, h, txt
0, ‘R’, true, 1, // border, align, fill, ln
118, $y + 12, true, 0); // x, y, reseth, stretch

Fatal error: Uncaught Error: Call to a member function SetFont() on null i
If I comment the SetFont Line, I get the next error
Fatal error: Uncaught Error: Call to a member function SetTextColor() on null

This continues…
Can someone please give ideas.
The same above commands are working in the Report section of the Code

this can help you

Try to do this:

$this->Pdf->SetFont(‘helvetica’, ‘N’, 9);
$this->Pdf->SetTextColor(130);

And so on. It seems Scriptcase does not accept this line:

$pdf = $this->Pdf;

Thanks. But sorry, it is not working. As per your instructions, my code as follows with the same error as I first mentioned :slight_smile:
$pdf = $this->Pdf;
$this->Pdf->SetFont(‘helvetica’, ‘N’, 9);
$this->Pdf->SetTextColor(130);

Thanks . Tried as per your link above with Error as TCPDF ERROR: Some data has already been output, can’t send PDF file. Then I added the ob_end_clean() which cleared the issue.
My code
sc_include_lib(“tcpdf”);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘’, false);

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(‘Nicola Asuni’);
$pdf->SetTitle(‘TCPDF Example 50’);
$pdf->SetSubject(‘TCPDF Tutorial’);
//$pdf->SetKeywords(‘TCPDF’, ‘PDF’, ‘example’, ‘test’, 'guide’);
$pdf->AddPage(‘P’,‘A4’); //A4 Portrait
//$pdf->AddPage(‘P’,‘A4’); //A4 Landscape
// ob_end_clean(); // THIS IS REQUIRED TO REMOVE THE ERROR Some data has already been output, can’t send PDF file
$pdf->Output(‘testing.pdf’, ‘I’);

ERROR MESSAGE : ** TCPDF ERROR: Some data has already been output, can’t send PDF file, if ob_end_clean() not given

Ok, but as I said, you could use just the 2 lines I showed you, excluding the $pdf = $this->Pdf; line.

And another thing is: are you calling the library at the top of your code?

Please forgive me for my ignorance.
Please check the following code
sc_include_lib(“tcpdf”);
$pdf = $this->Pdf;
$this->Pdf->SetFont(‘helvetica’, ‘N’, 9);
$this->Pdf->SetTextColor(130);
I am getting this error "Fatal error: Uncaught Error: Call to a member function SetFont() on null ".
Whereas the following code is working in the custom PHP Button:
sc_include_lib(“tcpdf”);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘’, false);

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(‘Nicola Asuni’);
$pdf->SetTitle(‘TCPDF Example 50’);
$pdf->SetSubject(‘TCPDF Tutorial’);
//$pdf->SetKeywords(‘TCPDF’, ‘PDF’, ‘example’, ‘test’, 'guide’);
$pdf->AddPage(‘P’,‘A4’); //A4 Portrait
//$pdf->AddPage(‘P’,‘A4’); //A4 Landscape
// ob_end_clean(); // THIS IS REQUIRED TO REMOVE THE ERROR Some data has already been output, can’t send PDF file
$pdf->Output(‘testing.pdf’, ‘I’);

Is that all your code in the Custom Button?

Yes. That ws for the test. And the code threw error at the SetFont segment. But actually, I have further code for the entire report to be printed as pdf. That is part of the REPORT application in SC. Now, with the above working code, i will move the REPORT code to the form / Generate PDF button (Custom button)