最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generate PDF with AJAX request - Stack Overflow

programmeradmin2浏览0评论

I'm using Laravel 5.4 and I'm trying to generate a PDF download using Snappy PDF.

The reason I am trying to do it this is way is so that I can submit the form data to the PDF without reloading the page.

Here is my blade template:

<div>
    Download a PDF: <button type="button" class=" btn-success btn-sm" id="js-download" >Download</button>
</div>

This is my route:

Route::get('/download', 'FormController@generatePDF');

This is my controller function:

public function generatePDF(Request $request)
{
    $pdf = PDF::loadView('pdf.download', $request);
    return $pdf->download('download.pdf');
}

And this is my .js file:

$(document).on("click", "#js-download", function(e) {

e.preventDefault();

$.ajax({
    url: '/download',
    data: $("#js-pdf-form").serialize(),
    success:function(data) {
        console.log(data)
    },
    error:function() {
    }
});

});

The PDF works if I call the route via the address bar in the browser, but if I click the button and attempt an AJAX request it fails, however it does console log what seems to be the PDF output...

Any help would be greatly appreciated.

I'm using Laravel 5.4 and I'm trying to generate a PDF download using Snappy PDF.

The reason I am trying to do it this is way is so that I can submit the form data to the PDF without reloading the page.

Here is my blade template:

<div>
    Download a PDF: <button type="button" class=" btn-success btn-sm" id="js-download" >Download</button>
</div>

This is my route:

Route::get('/download', 'FormController@generatePDF');

This is my controller function:

public function generatePDF(Request $request)
{
    $pdf = PDF::loadView('pdf.download', $request);
    return $pdf->download('download.pdf');
}

And this is my .js file:

$(document).on("click", "#js-download", function(e) {

e.preventDefault();

$.ajax({
    url: '/download',
    data: $("#js-pdf-form").serialize(),
    success:function(data) {
        console.log(data)
    },
    error:function() {
    }
});

});

The PDF works if I call the route via the address bar in the browser, but if I click the button and attempt an AJAX request it fails, however it does console log what seems to be the PDF output...

Any help would be greatly appreciated.

Share Improve this question asked Jul 26, 2018 at 15:21 ShaunShaun 5464 silver badges13 bronze badges 2
  • 2 are you saying the ajax returns a successful return code? You should also add a log in the error callback, it helps so much during development. – Andrew L Commented Jul 26, 2018 at 15:25
  • 1 I've not worked with downloads much, but from my own mon knowledge I would have to say that AJAX does not support downloads, it just retrieves the data. Now if it is actually being successful, and you get the pdf data, you can place that data in an element or something, (at this point I don't know how downloads work so that's all i can tell you) – Matt D Commented Jul 26, 2018 at 15:32
Add a ment  | 

1 Answer 1

Reset to default 5

replace it:

return $pdf->download('download.pdf');

to this:

$pdf_file = 'download.pdf';

$pdf_path = 'public/pdf/'.$pdf_file;

$pdf->save($pdf_path);

return asset($pdf_path);

Dont forget to create the public/pdf folder...

发布评论

评论列表(0)

  1. 暂无评论