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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?