【问题标题】:In the model, click on the button to download the pdf file, But not working (Laravel)在模型中,点击按钮下载pdf文件,但不工作(Laravel)
【发布时间】:2020-12-06 00:13:04
【问题描述】:

我有产品。产品有 1 个产品(型号)。如果用户单击按钮下载型号中的 pdf。我创建函数 create pdf 但不起作用。

此控制器中的 PDF 生成器功能

    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');
    }

控制器发送响应->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 按钮(此按钮在模型中)

<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 模型展示。

   $.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);
            // }
        });

问题是当我单击打开模型时。在控制台中显示这些难以理解的文字。

请帮忙!谢谢大家!!!!

【问题讨论】:

    标签: php html jquery laravel pdf


    【解决方案1】:

    这是您的原始 pdf 数据! 您只需将其加载到刀片上 看这里

    $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');
    

    我在几周前写了这篇文章。不知道你的包裹是否是你的包裹,但解决方案是一样的 我将我的数据加载到名为 print 的刀片中,然后将其下载为 pdf 阅读您的软件包文档和问题,您会找到解决方案

    【讨论】:

      【解决方案2】:

      您的回复似乎没有必要的标题。告诉浏览器,响应是 PDF,应该下载。您在此处传递 text/html。

      通过这个:

      Content-type:application/pdf
      

      检查这个: correct PHP headers for pdf file download

      奖励:

      这是生成/存储/下载 PDF 的最佳方法。

      DOMPDF Laravel 包装器。

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

      【讨论】:

        猜你喜欢
        • 2014-10-16
        • 2015-12-19
        • 1970-01-01
        • 2022-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多