How to submit it to php via ajax, generate pdf and download it through tcpdf?

1. The front-end ajax submits data to php
_ html as the content to generate pdf (html code)
_ name as the file name to generate psdf

$.ajax({
              type: "POST",
              dataType: "json",
              url: "{:url("api/pdf/create")}",
              data: { "html": _html,"name":_name},
              success:function(res){

              }
            });

2. Backend php (url:api/pdf/create)
Controller file code is as follows (using tcpdf):

$tcpdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, "UTF-8", false);
        $tcpdf->SetFont("msyh", "", 10, "", true);
        $tcpdf->SetTitle($name);
        $tcpdf->AddPage();
        $tcpdf->writeHTML($html);
        $tcpdf->lastPage();
        $tcpdf->Output($name.".pdf","D");
        exit();

description: if you submit it to php through ajax to generate, F12, click network, click api/pdf/create, click response to get the string of pdf, as shown in figure

question: how to click the button, trigger ajax, submit the information to php, and pop up the download box of pdf?

Jun.30,2022

Jump directly to the controller to download

Menu