0
votes

Am working on a Laravel application whereby I fetch some data from an API (which is an executable link which opens a PDF document when clicked). Am fetching the data storing in a variable then pass to the view but when I add it inside the href attribute of anchor tag I get error of Undefined variable: quotePdf.

But when I get the data after dd and paste it in the anchor link tag it works perfectly

AJAX code rendering the view

 $.ajax({
    type: "POST",
    url: "getquote",
    data: JSON.stringify(allData),
    contentType: "application/json",
    dataType: "json",
    cache: false,
    success: function(data){ 
      //The url that will render the view called quote.blade.php
      window.location.href="showquote" ;
    },
    error: function(data) {
        var errors = '';
        for(datos in data.responseJSON){
            errors += data.responseJSON[datos] + '\n';
        }
        alert(errors);
    }
});

Controller that fetches the code and shows on the frontend

 public function createQuote(Request $request)
    {

        $quote = $this->global_Curl_Meta(
            $data, 'api/travel/create-quote')->data;

        //dd($quote);

        $quote_data = $quote;

        $quoteholder_name = $form['FirstName'] . ' ' . $form['MiddleName'] . ' ' . $form['LastName'];
        $quoteholder_email = $form['email'];
        $travel_plan = $plan;
        $quoteID = $quote_data->QuotationId;
        $quoteRef = $quote_data->QuoteNumber;

        $data = [
            'sts_quote_number' => $quoteRef
        ];

        //STS PDF Quote
        $quotePdf = $this->global_Curl_Meta(
            $data, 'api/v1/travel/sts-quote-doc')->data;

        //dd($quotePdf);

        //Get all teh variables and return to the view
        $quote_data = view("B2C::travel.quote", compact('quote_data', 'quoteholder_name', 'quoteholder_email', 'travel_plan' ,'phone' , 'quotePdf' ));

        return 'true';
    }

Response on the browser after dd

data:application/pdf;base64,JVBERi0xLjQKJaqrrK0KNCAwIG9iago8PAovQ3JlYXRvciAoQXBhY2hlIEZPUCBWZXJzaW9uIFNWTiB0YWdzL2ZvcC0xXzEpCi9Qcm9kdWNlciAoQXBhY2hlIEZPUCBWZXJzaW9uIFNWTiB0YWdzL2ZvcC0xXzEpCi9DcmVhdGlvbkRhdGUgKEQ6MjAxODEyMjcwODA0NTkrMDInMDAnKQo+PgplbmRvYmoKNSAwIG9iago8PAogIC9OIDMKICAvTGVuZ3RoIDExIDAgUgogIC9GaWx0ZXIgL0ZsYXRlRGVjb2RlCj4+CnN0cmVhbQp4nJ2Wd1RU1xaHz713eqHNMNIZeu9tAOldQHoVhWFmgKEMMMwAIjZEVCCiiIhgAwkKGDAaisSKKBaCggL2gAQBJQaj2FDJjKyV+PLy3svL7497v7XP3ueeXc5aFwCSdyorLQuWAiCNJ+AHe7nSI6Oi6dh+AAM8wAAzAJisrAz/EM9QIJKPhxs9S+QE/kWvhwEkft8yEu8F/j9JszL4AgCgQBFbsDlZLBEXijg1R5Ahts+KmBqfImYYJWa+6IAilhNz4iIbfvZZZGcxs9N4bBGLz5zBTmOLuUfEW7OFHBEjfiIuyuZyckR8W8SaqcI0rojfimPTOExRDiiS2C7gsJJEbCpiEj802E3ESwHAkRK/4PgvWMDJFYiTckvPWM3nJiYJ6LosPbqZrS2D7s3JSeUIBEaBTFYKk8+mu6WnZTB5qwFYzPmzZMS1pYuKbG1ma21tZG5s9kWh/uvi35S4t4v0MuhzzyBa3x+2v/JLrwOAMSeqzY4/bPF7AejYDIDcvT9smocAkBT1rf3GF/nQxPOSJBBk2JmY5OTkGHM5LGNxQX/X/3T4G/rie8bi7X4vD92dk8AUpgro4rqx0lPThXx6VgaTxaEb/XmI/3HgX5/DMJiTwOFzeKKIcNGUcXmJonbz2FwBN51H5/L+UxP

The view that is called quote.blade.php being rendered via AJAX, all the variables are being rendered well except quotePdf

@section('content')

@php
//var_dump($quote_data);
$quote_number = $quote_data->QuoteNumber;
$quote_details = $quote_data->Calculation_Quote->Calculation;
@endphp
<div class="container">

<main class="top Quote">
  <h1 class="quote-title">Travel Insurance Quote
    <!-- Go Back --> 
    <a href="#" onclick="goBack()">  
      <span class="back-us"><i class="fas fa-arrow-left"></i>Back</span>
    </a> 
    <!--END-->
  </h1>
  <table class="table ref">
  <thead class="thead-dark">
    <tr>
      <th colspan="6"> {{$quoteholder_name}} </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="4" class="highlighted">Description</td>
      <td colspan="2" class="highlighted">Pricing</td>
    </tr>
    <tr>
      <td colspan="4"> Quote Reference </td>
      <td colspan="2"> {{$quote_number}} </td>
    </tr>
    <tr>
      <td colspan="4">{{$travel_plan}} Package </td>
      <td colspan="2"> $ {{round($quote_details->TravelBasicPremium,2)}} </td>
    </tr>
    <tr>
      <td colspan="4"><form>
    </tr>

    <tr>
      <td colspan="4" class="highlighted">Total</td>
      <td colspan="2" class="highlighted">$ {{round($quote_details->TravelTotalGrossPremium,2)}}</td>
    </tr>

    <tr>
      <td colspan="2"><i class="fas fa-print quote"></i> <a href="{{ $quotePdf }}" target="_blank">Print Quote </a></td>
    </tr>

  </tbody>
</table>

<h1 class="notice">Important! Please review our policy, terms and conditions before you continue. <a href="#"><span class="noticeClick">CLICK HERE</span></a></h1>

<span class="glyphicon glyphicon-print"></span>

</main>
</div>

@endsection
4
You mean, its giving error when you use in href but if you dd in the view file it is printing the value?Himanshu Upadhyay
@HimanshuUpadhyay Exactly,, thats what its doing..Martin
are you sure that the view file is having blade template?Himanshu Upadhyay
@HimanshuUpadhyay Yes it has a blade template,, it has an extension of quote.blade.phpMartin
@Martin Can you show the code how are you returning the view.Saumini Navaratnam

4 Answers

0
votes

try this one

$quote_data = view("travel.quote", ['quotePdf' => $quotePdf])->render();

otherwise used with method

$quote_data = view("travel.quote")->with('quotePdf',$quotePdf)->render();
0
votes

Try like this,

<a href="{!! $quotePdf !!}" target="_blank">Print Quote </a>

0
votes

I may be wrong but this is what I have observed.

public function createQuote(Request $request)
            {

                $quote = $this->global_Curl_Meta(
                    $data, 'api/travel/create-quote')->data;

                //dd($quote);

                $quote_data = $quote;

                $quoteholder_name = $form['FirstName'] . ' ' . $form['MiddleName'] . ' ' . $form['LastName'];
                $quoteholder_email = $form['email'];
                $travel_plan = $plan;
                $quoteID = $quote_data->QuotationId;
                $quoteRef = $quote_data->QuoteNumber;

                $data = [
                    'sts_quote_number' => $quoteRef
                ];

                //STS PDF Quote
                $quotePdf = $this->global_Curl_Meta(
                    $data, 'api/v1/travel/sts-quote-doc')->data;

                //dd($quotePdf);

                //Get all teh variables and return to the view
                $quote_data = view("B2C::travel.quote", compact('quote_data', 'quoteholder_name', 'quoteholder_email', 'travel_plan' ,'phone' , 'quotePdf' ));

                return 'true';
            }

In your compact function, you are passing 'phone' as an entry which is not defined anywhere and hence it is ignoring each and every entry after that. Try to remove 'phone' from compact function.

0
votes

Instead of returning to base64 string ($quotePdf) to view. You can do something like this.

$decoded = base64_decode ($quotePdf);

$pdf = fopen ('quote.pdf','w');

fwrite ($pdf,$decoded);

fclose ($pdf);

header("Content-type:application/pdf");

header("Content-Disposition:attachment;filename='quote.pdf'");

readfile("quote.pdf");

This code will decode base64 string and create a pdf file. and downloads the same. If you want return the view you can save the pdf in server and return the file path as a response.

Hope this helps!!