-1

Hey i using HTML2PDF to generate PDF and auto download when someone click on save button.

Here is my code given below.

AJAX

<script>
function savePDf(){
   var urlss = 'http://sunno-svr/gen-projects/printpdf/index.html';
   $.ajax({
        type: "POST",
        url: 'http://sunno-svr/gen-projects/printpdf/htmltwo.php',
        data: {data: urlss},
        success: function(result) {
            window.console.log('Successful');
        }
    });
  }
</script>

PHP

<?php 

ob_start();
require __DIR__.'/vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
$htmldata = $_REQUEST['data'];
$html2pdf = new HTML2PDF('L', 'C4', 'en');
$html2pdf->setTestTdInOnePage(true);
$html = file_get_contents($htmldata);
$html2pdf->writeHTML($html); 
//$html2pdf->Output();
header("Content-type:application/pdf");
$html2pdf->Output('pdf_demo.pdf', 'D');
?>

I also tried this Solution but it is not working.

How can i force download in HTML2PDF, anybody can guide me which thing i missing in my code?

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
  • AJAX is a _background_ request, you can not directly trigger a download via HTTP headers in that scenario. – misorude Jul 05 '19 at 12:03
  • 1
    Possible duplicate of [Handle file download from ajax post](https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) – misorude Jul 05 '19 at 12:03

1 Answers1

0

Please try to use $html2pdf->Output('pdf_demo.pdf', 'D'); instead of the following code

$html2pdf->Output('pdf_demo.pdf', 'F');
Vintage Coders
  • 162
  • 1
  • 4