在长期寻找解决方案后,我被迫写下这篇文章。我希望你能解释我做错了什么。
我有文件index.php,preview.php,saveToPdf.php和doc.html。
index.php
<head>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
</head>
//some other code included jquery link
预览.php
// this file contain form, buttons and some function for loading, filling and sending datas.
<?php
include 'index.php';
?>
// here is some html form
<button id="previewBtn">preview</button>
<button id="saveBtn">save</button>
<div id="content"></div>
<script type="text/javascript">
fillDocs(){
// filling the document
}
$('#previewBtn').click(function(){
$('#content').load('./docs/doc.html', function(){
fillDocs();
});
});
$('#saveBtn').click(function(){
request = $.ajax({
url: "./saveToPdf.php",
type: "POST",
data: {'data' : $("#content").html(),
//other datas
},
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
});
request.done(function(response, textStatus, jqXHR){
console.log(response);
});
request.fail(function (jqXHR, textStatus, errorThrown){
console.log(textStatus+' '+errorThrown);
});
});
</script>
saveToPdf.php
// in this file I receive the data and store html content to pdf by MPDF lib.
// the problem is that this html content isn't formatted. It is plain html without css styles.
doc.html
// it is an html document with bootstrap classes
那么,我应该怎么做才能使保存的文件具有正确的格式呢?当我使用内联css样式时,它是有效的,但我想使用引导程序。
正如他们的文档中所述,您可以设置样式表数据:
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet,MpdfHTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($html,MpdfHTMLParserMode::HTML_BODY);
将style.css
更改为CSS文件的路径。