简体   繁体   English

OctoberCMS DynamicPDF通过AJAX打开或下载

[英]OctoberCMS DynamicPDF open or download via AJAX

I am using DynamicPDF and I am opening my file in new tab to generate my file which is working absolutely fine. 我正在使用DynamicPDF ,我在新标签中打开我的文件以生成我的文件,它工作得很好。 Here is what I have sone so far (in one of my plugin's update.htm file). 这是我到目前为止所做的事情(在我的一个插件的update.htm文件中)。

<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. 现在我试图以某种方式通过AJAX响应打开/下载相同的文件来做同样的事情。 Hence I have put below code inside my update.htm file. 因此我将下面的代码放在我的update.htm文件中。

<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 提示:通过Ajax响应下载PDF

OctoberCMS ajax framework cannot handle this type of response. OctoberCMS ajax框架无法处理这种类型的响应。

Recommended approach is to save PDF file locally and return redirect to PDF file. 建议的方法是在本地保存PDF文件并返回重定向到PDF文件。

And I tried to open / download by using return but its not working. 我尝试使用return打开/下载,但它不起作用。

Can someone guide me how can I solve this ? 有人可以指导我如何解决这个问题? How can I make my PDF file open / download using AJAX here ? 如何在这里使用AJAX打开/下载我的PDF文件?

Eventually, I have implemented above feature. 最后,我已经实现了上述功能。

Here is what I have done. 这就是我所做的。

update.htm 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 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)" . 正如您在update.htm文件中看到的,我使用了data-request="onPreviewDownload"data-load-indicator="Generating Brochure..."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); 然后在我的ControllerFile onPreviewDownload方法,我使用了save方法而不是download方法, 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. DynamicPDF的文档中提到,通过该文档 ,我将文件保存在特定位置,并且一旦我能够保存文件。

Then I am opening from my formSuccess method in update.htm file using window.open(data.result, '_blank'); 然后我使用window.open(data.result, '_blank');update.htm文件中的formSuccess方法打开window.open(data.result, '_blank'); .

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM