使用 ajax 和 mpdf 将 html 转换为 pdf

Convert html to pdf with ajax and mpdf

我有 html 的列表存储在数据库中,我想检索数据并将其转换为 pdf,转换后通过 ajax 请求下载。

我尝试了它,但是当下载 pdf 时,下载的文件是空的。 请问我需要做什么? 谢谢。

JS代码

SMSLiTe('click','.export-to-pdf',function(event){
  event.preventDefault();
  ///export note to pdf via ajax
  if (typeof notes[0].note.nid !== typeof undefined && notes.length) {
  var nid = notes[0].note.nid;
  var fd = new FormData();
  fd.append('export_to_pdf',true);
  fd.append('note_id',nid);
  request.open(bigPipe.formMethod.a, 'pdf/pdf');
  request.send(fd);
  request.onload = function(e){
    if (request.readyState == request.DONE && request.status ==200) {
        //code here ....download
        var data = request.responseText,
        blob = new Blob([data]),
        a = document.createElement('a'), d = new Date();
        a.href = window.URL.createObjectURL(blob);
        a.download = d.getTime()+'.pdf'; a.click();

    }
  }
  }else{
  sustainScroll(bigPipe.c_w,bigPipe.class.fixed);
  var msg = 'Sorry! something gone wrong, please refresh this page and try again.',title = 'Export Failed';
  $(bigPipe.document).append(flexibleDialog({content:templateHTML.flexDialog(msg,title,'_2d0',null,'Close',null,0),isClass:'export_to_pdf_failed _2d0',heigt:140,width:600}));jkg();  
  }
});

PHP代码

if (isset($_POST['note_id'],$_POST['export_to_pdf'])) {
$nid = (int)$_POST['note_id'];
$sql = "SELECT content  FROM $notes_table WHERE note_id='{$nid}' LIMIT 1";
$query = mysqli_query($conn,$sql);
if (mysqli_num_rows($query)) {
$row = mysqli_fetch_assoc($query);
$content = $row['content'];
//create PDF file
$mpdf = new mPDF();
$mpdf->WriteHTML($content);
$mpdf->Output(time().'.pdf','D');exit;
}
}

检查从 MPDF 返回的响应数据,我意识到输出数据是一个 blob,因为数据已经是 blob 数据。我有将 XMLHttpRequest.responseType 的 responseType 更改为 blob

的线索

目前有效。

修改后的JS:

SMSLiTe('click','.export-to-pdf',function(event){
event.preventDefault();

//set request method
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
var request = new XMLHttpRequest();
} else {  // code for IE6, IE5
var request = new ActiveXObject("Microsoft.XMLHTTP");
}


  ///export note to pdf via ajax
  if (typeof notes[0].note.nid !== typeof undefined && notes.length) {
  var nid = notes[0].note.nid;
  var fd = new FormData();
  fd.append('export_to_pdf',true);
  fd.append('note_id',nid);
  request.open(bigPipe.formMethod.a, 'pdf/pdf');
  request.send(fd);

  request.responseType = 'blob';//change reponseType before onload

  request.onload = function(e){
    if (request.readyState == request.DONE && request.status ==200) {
        //code here ....download
        var data = request.response,
        blob = new Blob([data],{type: 'application/pdf'}),
        a = document.createElement('a'), d = new Date();
        a.href = window.URL.createObjectURL(blob);
        a.download = d.getTime()+'.pdf'; a.click();
        //console.log(data);
    }
  }
  }else{
  sustainScroll(bigPipe.c_w,bigPipe.class.fixed);
  var msg = 'Sorry! something gone wrong, please refresh this page and try again.',title = 'Export Failed';
  $(bigPipe.document).append(flexibleDialog({content:templateHTML.flexDialog(msg,title,'_2d0',null,'Close',null,0),isClass:'export_to_pdf_failed _2d0',heigt:140,width:600}));jkg();  
  }
});

PHP代码: 我们必须将 MPDF 输出为字符串,因为我们没有将 MPDF 附加到浏览器;

//create PDF file
$mpdf = new mPDF();
$mpdf->WriteHTML($content);
$output  = $mpdf->Output(time().'.pdf','S');//output as string
echo($output); //final result to JS as blob data