下载 pdf vue

Lau*_*ich 3 vue.js

当我在 PostMan 上调用此功能时,它确实下载了 pdf,但是当我从页面调用时,它不会下载但请求到达。我要下载

,generateFarmerPDF:function (id) {
            this.farmerId = id
          var data = new FormData()
          data.append('function','generateFarmerPDF')
            data.append('farmerId',this.farmerId )
          axios.post(this.url,data)
              .then( function (response ) {
          }.bind(this)).catch(function (error) {

          })

        }
Run Code Online (Sandbox Code Playgroud)

这是api代码

if ($function == "generateFarmerPDF") {
    $farmerId = $_POST['farmerId'];

    $result = DB::instance()->executeSQL("SELECT * FROM `milk_production`  WHERE `farmerId` ='$farmerId'");
    $header = DB::instance()->executeSQL("SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='blog_samples' 
AND `TABLE_NAME`='milk_production'");


    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 5);
    foreach ($header as $heading) {
        foreach ($heading as $column_heading)
            $pdf->Cell(20, 6, $column_heading, 1);
    }
    foreach ($result as $row) {
        $pdf->SetFont('Arial', '', 5);
        $pdf->Ln();
        foreach ($row as $column)
            $pdf->Cell(20, 6, $column, 1);
    }
    $pdf->Output();
}
Run Code Online (Sandbox Code Playgroud)

我是这样称呼它的

<button v-on:click="generateFarmerPDF(milkvolume.farmerId)">Report</button></td>
Run Code Online (Sandbox Code Playgroud)

Ist*_*tei 8

例如,您可以使用它:

axios('/urltopdfgeneration', {
    method: 'GET',
    responseType: 'blob'
})
.then(response => {
    //Create a Blob from the PDF Stream
    const file = new Blob(
    [response.data], 
    {type: 'application/pdf'});
    //Build a URL from the file
    const fileURL = URL.createObjectURL(file);
    //Open the URL on new Window
    window.open(fileURL);
})
.catch(error => {
    console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

也许弹出窗口阻止程序会因为window.open而捕获它,但它可以通过这种方式使用axios下载pdf。