0
votes

I'm working on a Laravel application, and have a function to export a PDF file that is generated based on input that the user has submitted to a form.

I am trying to make the content of that PDF conditional, based on the value of one of the PHP variables, so have added the @if, @else and @endif conditions to the blade file, but for some reason, now get an error in the console when trying to export the PDF:

Parse error: syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) (View: /home/.../reminder.blade.php)

The blade.php file is currently written with:

<html>

<head>
    <title>Provisional Reminder</title>
    <link rel="stylesheet" href="{{ url('') }}/css/pdf.css">
</head>
@if(( count( $request->transactionItem ) == 1 ) && $request->transactionItem->currentStatusId == '1010') 

<body style="-webkit-font-smoothing: antialiased; font-family: 'Roboto'; font-weight: normal; margin: 0; padding: 0;">
    <p>if statement run in reminder.blade.php</p>
</body>

@else
<body>
    <p>else statement run in reminder.blade.php</p>
</body>
@endif

</html>

The else & endif are both there, so why am I getting this error? How can I get the content of the blade file to be displayed conditionally, based on the value of the request variable?

The PDF is being generated by the PHP function:

private function generateProvPDF($transactions, $globalData)
{
    $data = ['transactions' => $transactions, 'globalData' => $globalData];
    //dd("transactions: ", $transactions);

    //$view = \View::make('pdfs.reminder', $data);
    //$contents = $view->render();

    //echo $contents;
    //die;

    $pdf = \PDF::loadView('pdfs.reminder', $data) 
    ->setOption('encoding', 'utf-8')
    ->setOption('margin-top', 0)
    ->setOption('margin-bottom', 0)
    ->setOption('margin-left', 0)
    ->setOption('margin-right', 0)
    ->setPaper('a4');

    return $pdf->stream();
}

Edit

The code in my PHP controller that calls this function is:

public function getSingleTransactionPDF(Request $request)
{
    $transactionItem = $request->input('transactionItem');

    $vTransactionItem = (array)DB::table('transaction.vTransactionPDF')->where('transactionItemId', $transactionItem['transactionItemId'])->first();

    if (!empty($vTransactionItem))
    {
        $transactionItem = array_merge($transactionItem, $vTransactionItem);

        $loggedInUser = auth()->user();
        $globalData = $this->retrieveGlobalPDFData();
        $transactionsData = $this->retrievePDFPrimaryLineItemData([$transactionItem], $loggedInUser);

        dd("transactionsData, globalData: ", $transactionsData, $globalData);

        return $this->generateProvTaxPDF($transactionsData, $globalData);
    }
    else
    {
        return response()->json([
            'error' => true,
            'message' => 'Transaction not found.',
        ], 404);
    }
}

If I comment out the dd("transactionsData ...", ...) line here, I get the Parse error I've mentioned above. However, if I leave this dd() in, I can see the variables hold the values I'm expecting, and a PDF is downloaded, although it doesn't actually load when I try to open it- it comes up with an error.

2

2 Answers

0
votes

Try replacing the first @if statement with this

@if( count( $request->transactionItem ) == 1  && $request->transactionItem->currentStatusId == '1010') 
0
votes

There is no error in your reminder blade. I have checked your blade code and its running successfully. So please recheck your code in controller and other dependent file of this task. Running code is :

<html>
<head>
<title>Provisional Reminder</title>
<link rel="stylesheet" href="{{ url('') }}/css/pdf.css">
</head>
@php $a = array('1');  $status = 'hello';@endphp
@if((count($a) == 1 ) && $status == 'hello') 
 <body style="-webkit-font-smoothing: antialiased; font-family: 'Roboto'; font-weight: normal; margin: 0; padding: 0;">
<p>if statement run in reminder.blade.php</p>
</body>
@else
<body>
<p>else statement run in reminder.blade.php</p>
</body>
@endif

and output is : if statement run in reminder.blade.php