dongzhe3573 2019-04-19 05:14
浏览 8
已采纳

OctoberCMS DynamicPDF通过AJAX打开或下载

I am using DynamicPDF and I am opening my file in new tab to generate my file which is working absolutely fine. Here is what I have sone so far (in one of my plugin's update.htm file).

<a href="<?= url('/'); ?>/regency-brochure" class="btn btn-primary" target="_blank">Preview Brochure</a>

Now I am trying to somehow do the same by opening/downloading the same file via AJAX response. Hence I have put below code inside my update.htm file.

<button
                        type="submit"

                        data-request="onPreview"
                        data-load-indicator="Loading Preview"
                        class="btn btn-primary">Preview Brochure Ajax
                    </button>

And Inside my controller I have done this.

public function onPreview()
    {        
     return PDF::loadTemplate('renatio::invoice')->download('download.pdf');
    }

Now as soon as I click on it, my browser getting hanged, but I am able to see some random binary long string in my response.

I have checked and read library's documentation and they are giving a tip saying...

Tip: Download PDF via Ajax response

OctoberCMS ajax framework cannot handle this type of response.

Recommended approach is to save PDF file locally and return redirect to PDF file.

And I tried to open / download by using return but its not working.

Can someone guide me how can I solve this ? How can I make my PDF file open / download using AJAX here ?

  • 写回答

1条回答 默认 最新

  • duanhuang4306 2019-04-19 11:49
    关注

    Eventually, I have implemented above feature.

    Here is what I have done.

    update.htm

     <button type="submit" data-request="onPreviewDownload" data-load-indicator="Generating Brochure..."
                                data-request-success="formSuccess( context, data, textStatus, jqXHR)" class="btn btn-primary">Preview Brochure
                            </button>
    
    <script>
        function formSuccess( context, data, textStatus, jqXHR){        
            window.open(data.result, '_blank');        
        }    
    </script>
    

    ControllerFile.php

    public function onPreviewDownload()
        {
    
    
            $templateCode = 'renatio::invoice'; // unique code of the template
            $storagePath =  storage_path('app/uploads/');
            $pdf_file_name =  'regency-brochure-test.pdf' ;
            $pdf_file_name_directory =  $storagePath . $pdf_file_name;
            PDF::loadTemplate($templateCode)->setPaper('a4', 'landscape')->save($pdf_file_name_directory);
            return $baseUrl = url(Config::get('cms.storage.uploads.path')) . '/' . $pdf_file_name;
    
        }
    

    As you can see in update.htm file, I have used data-request="onPreviewDownload" , data-load-indicator="Generating Brochure..." and data-request-success="formSuccess( context, data, textStatus, jqXHR)".

    Then onPreviewDownload method in my ControllerFile, I have used save method instead of download method, PDF::loadTemplate($templateCode)->setPaper('a4', 'landscape')->save($pdf_file_name_directory); mentioned in Documentation of DynamicPDF through which, I am saving the file in particular location and once I am able to save the file.

    Then I am opening from my formSuccess method in update.htm file using window.open(data.result, '_blank');.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配