0

I have a Laravel/ Angular application, and want to alter the contents of a PDF that's generated on one of the pages based on the content of the variables that will be displayed on that PDF.

The PDF is in the file reminder.blade.php, and currently has the content:

<html>
<head>
    ...
</head>

<body style="-webkit-font-smoothing: antialiased; font-family: 'Roboto'; font-weight: normal; margin: 0; padding: 0;">
    ...
</body>
</html>

I want to alter the content displayed in the <body></body> depending on the number of items to be displayed on the PDF, and the 'status' of those items, so I've surrounded the HTML for this with a PHP if statement:

<html>
<head>
    ...
</head>

@if(( count( {{ $transaction['transactionItem'] }} ) == 1) && {{ $transaction['transactionItem']['currentStatusId'] }} == '1010')
    <body style="-webkit-font-smoothing: antialiased; font-family: 'Roboto'; font-weight: normal; margin: 0; padding: 0;">
        ... Only first page of PDF here
    </body>
@else
    <body style="-webkit-font-smoothing: antaliased; font-family: 'Roboto'; font-weight: normal; margin: 0; padding: 0;">
        ... Full PDF content here
    </body>
@endif
</html>

However, when I now click the button to generate & download the PDF, I get an error in the Network tab of the console, that says:

Parse error: syntax error, unexpected '<' (View: /home/vagrant/code/resources/views/pdfs/prov/reminder.blade.php)

in 419cdd3906d761bc1bb55581502a15d04c1fe7a7.php line 8 at CompilerEngine->handleViewException(object(FatalThrowableError), 1) in PhpEngine.php line 46

which seems to either be complaining about the @if statement I've added in, or, according to the unexpected '<' answer here, is due to the fact that I'm using single quotes inside the double quotes on the <body ...> line... even though I am closing both correctly- at least as far as I can tell...

Anyone have any idea what I'm doing wrong here? How can I get the content of the PDF generated to be conditional, like I'm trying to?

Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
  • I can't see an @endif tag – wadleo Oct 31 '18 at 15:40
  • Apologies- missed it when typing the question. It is there in my code, and I've just updated my post to show it. – Noble-Surfer Oct 31 '18 at 15:43
  • okay, and there is not php in the head element? – wadleo Oct 31 '18 at 15:46
  • No, just a couple of HTML tags that aren't really relevant to the issue. – Noble-Surfer Oct 31 '18 at 15:48
  • then it may be in the if or else closures, are there any php or blade code there? – wadleo Oct 31 '18 at 15:49
  • It might well be the `@if` or `@else` or their closures, as that's all I've changed about the file since it was last working- adding those in, & copying & pasting the first page of the HTML into the `@if` section. Thing is... I can't see what I'm doing wrong with them, or why I would be getting this error. I'm not sure what the 'blade' code is in particular- there is PHP & HTML in the file... – Noble-Surfer Oct 31 '18 at 15:53
  • Yeah, that's my understanding... I'm relatively inexperienced with PHP- until a couple of months ago, the last time I had used it was about 10 years ago, at university- and then only very briefly... I just don't understand why it says that there's a parse error when I surround the HTML/ Angular with the PHP `@if` statement, but is fine when there's no `@if` statement. I think I must be using it wrong, but can't figure out how/ why for the life of me... – Noble-Surfer Oct 31 '18 at 16:04
  • then share the whole file here, that's the only way we can really help – wadleo Oct 31 '18 at 16:11
  • The actual content of the file (i.e. what's inside the `` tags) is irrelevant- I know that it all displays correctly, as I haven't made any changes to that. The PDF was working (downloading & displaying) correctly until I made its contents conditional by adding the `@if()`, `@else` & `@endif`. The issue here is why the `@if()` statement doesn't seem to be working/ is causing the app to complain about a syntax error. – Noble-Surfer Oct 31 '18 at 16:19
  • I am not doubting your code, but you have to help us help you, thanks for understanding – wadleo Oct 31 '18 at 16:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182886/discussion-between-wadleo-and-someone2088). – wadleo Oct 31 '18 at 16:32
  • Well I can post the full blade.php file if you like, but everything I've left out is completely irrelevant to the issue (the file is over 300 lines long, so it would really make the question a drag to read). As mentioned in my OP, the error shown in the Network tab of the browser console appears to be complaining specifically about the `@if` or the `` line. I'll update the details of the error message in my OP now – Noble-Surfer Oct 31 '18 at 16:34

0 Answers0