Arthur
Arthur

Reputation: 513

In the model, click on the button to download the pdf file, But not working (Laravel)

I have products. Products have 1product(model).If user click button download pdf in model. I create function create pdf but not working.

Pdf Generator function in this controller

    public static function pdf_g(){
        // HTML
        $html = '<h1>This html</h1>';

        // CREATE NEW PDF
        $mpdf = new Mpdf();

        // WRITE HTML
        $mpdf->WriteHTML($html);

        // DEST: D - DOWNLOAD
        return  $mpdf->Output('asd.pdf', 'D');
    }

Controller SEND RESPONSE->JSON mpdf

    public function show_cadastre(Folder $folder,Cadastre $cadastre){

        // GET THIS CADASTRE ID VALDOS
        $get_valdos = Valdos_matmenys::where('KAD_ID', $cadastre->ID)->first();

        // GET VISAS_PLOTAS
        $visas_plotas = round($get_valdos->PLOTAS_K / 10000, 2);

        // GET MISKO
        $misko =  round($get_valdos->MISK / 10000, 2);

        // GET Vandens plotas
        $vandens = round($get_valdos->PLOTAS_V / 10000, 2) ;

        // GET KITO PLOTO
        $kito_ploto = round($visas_plotas - ($misko + $vandens), 2);

        // GET Girininkija
        $girininkija = $cadastre->GIRININKIJ;

        // GET Apskirtis
        $apskirtis = Savivaldybe::where('SAV_ID', $cadastre->SAV_ID)->first();
        $apskirtis_pav = $apskirtis->apskritis;

        // GET Uredija
        $uredija = Uredija::where('ured', $cadastre->ured)->first();
        $ured_pavadinimas = $uredija->pavadinimas;

        // GET Rajonas
        $raj = Cadastre::getRagion($cadastre->RAJ);

        // GET MAP
         $mapUrl = Cadastre::GenerateMap($cadastre->NTR_ID, array('style' => 'ORTOFOTO_TIK_VALDA', 'offsetRatio' => 0.3))[0];
        // GET BACKGROUND
        $backgroundUrl = Cadastre::GenerateMap($cadastre->NTR_ID, array('style' => 'ORTOFOTO_TIK_VALDA', 'offsetRatio' => 0.3))[1];

        // CREATE PDF FOR Zemelapis is virsaus


       // PDF DOWNLOAD
       $mpdf = self::pdf_g();

        return response()->json(array(
            'cadastre' => $cadastre,
            'visas_plotas' => $visas_plotas,
            'misko' => $misko,
            'vandens' => $vandens,
            'kito_ploto' => $kito_ploto,
            'girininkija' => $girininkija,
            'apskirtis_pav' => $apskirtis_pav,
            'ured_pavadinimas' => $ured_pavadinimas,
            'raj' => $raj,
            'mapUrl' => $mapUrl,
            'backgroundUrl' => $backgroundUrl,
           'mpdf' => $mpdf
        ));
    }

Html button (this button is in the model)

<a id="pdfMapFromTop" class="text-center text-decoration-none text-smooth-dark p-1">
   <span class="align-self-center">
         <i class="fa fa-file-pdf text-danger"></i>
   </span>
   <span class="text-default-dark small align-self-center d-inline-flex flex-column">Žemėlapis_iš_viršaus.pdf</span>
</a>

Jquery model show.

   $.ajax({
            type: "GET",
            crossDomain: true,
            url: '/dashboard/folder/' + folder_id + '/cadastre/' + cadastre_id,
            success: function (data) {
                console.log(data)
                // KODASTRINES NUMERIS
                $('#ID').text(data.cadastre.ID);
                // get visas_plotas
                $('#visas_plotas').text(data.visas_plotas + 'ha');
                // get misko
                $('#misko').text(data.misko + 'ha');
                // get vandens
                $('#vandens').text(data.vandens + 'ha');
                // get kito_ploto
                $('#kito_ploto').text(data.kito_ploto + 'ha');
                // get girininkija
                $('#girininkija').text(data.girininkija);
                // get apskirtis
                $('#apskirtis').text(data.apskirtis_pav);
                // get uredija
                $('#uredija').text(data.ured_pavadinimas);
                // get rajonas
                $('#rajonas').text(data.raj);
                // get map url (img)
                $('#mapUrl').attr('src', data.mapUrl);
                // get map backgroundUrL (img)
                $('#backgroundUrl').attr('src', data.backgroundUrl);

                // If click this button download pdf
                $('#pdfMapFromTop').on('click', function () {
                    return data.mpdf;
                })

                // Show this model
                $('#show-cadastre-model-xl').modal('show');

            },

            // error: function (error) {
            //   console.log(error);
            // }
        });

The problem is when I click to open the model. Shows such incomprehensible text in the console.

enter image description here

Help please! Thanks all!!!!

Upvotes: 0

Views: 3879

Answers (2)

Alexey Khachatryan
Alexey Khachatryan

Reputation: 307

Looks like your response doesn't have necessary headers. Tell browser, that the response is PDF and it should be downloaded. You are passing text/html here.

Pass this:

Content-type:application/pdf

Check this: correct PHP headers for pdf file download

Bonus:

This is the best method to generate/store/download the PDFs.

The DOMPDF Laravel wrapper.

https://github.com/barryvdh/laravel-dompdf

Upvotes: 0

milad hedayatpoor
milad hedayatpoor

Reputation: 636

this is your raw pdf data! all you need is to load it to a blade look at here

$license = license::findOrFail($id);
    $data = [
        'license' => $license,
        'user' => Auth::user(),
        'labels' => LicValues::where('license_id',$id)->get()
    ];
    return PDF::loadView('print', $data)->download('print.pdf');

i wrote this couple weeks ago .dont know its your package or not but solution is same i loaded my data in blade called print and then download it as pdf read your package documentaion and issues you will find your solution

Upvotes: 2

Related Questions