首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ajax和mpdf将html转换为pdf

用ajax和mpdf将html转换为pdf
EN

Stack Overflow用户
提问于 2018-12-05 05:14:39
回答 1查看 1.5K关注 0票数 1

我已经将html列表存储在数据库中,我希望检索数据并将其转换为pdf,并通过ajax请求转换下载。

我试过使用它,但是当pdf下载时,下载的文件是空的。拜托,我需要做什么?谢谢。

JS码

代码语言:javascript
复制
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代码

代码语言:javascript
复制
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;
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-05 08:19:05

检查从MPDF返回的响应数据,我发现输出数据是blob,因为数据已经是blob数据。我有一条线索,要把XMLHttpRequest.responseType的responseType改为blob

现在起作用了。

修改后的联合材料:

代码语言:javascript
复制
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附加到浏览器;

代码语言:javascript
复制
//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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53625565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档