php - 是否可以通过 ajax 传递具有外部 css 样式的 html?
问题描述
经过长时间的寻找解决方案,我不得不写这篇文章。我希望你能解释我做错了什么。
我有文件 index.php、preview.php、saveToPdf.php 和 doc.html。
索引.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>
保存到PDF.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.
文档.html
// it is an html document with bootstrap classes
那么我应该怎么做才能获得格式正确的保存文件呢?当我使用内联 css 样式时它正在工作,但我想为此使用引导程序。
解决方案
如他们的文档中所述,您可以设置样式表数据:
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet,\Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($html,\Mpdf\HTMLParserMode::HTML_BODY);
更改style.css
为 CSS 文件的路径。
推荐阅读
- git - 如何在不丢失更改的情况下恢复提交?
- git - Maven 发布插件中的 git-commit 问题
- python - 在 dict() 上使用 `bisect_left()` 会引发 `dict is not a sequence` 错误。如何遍历 dict() 以改进搜索过程时间
- javascript - FileReader onload 并在循环中赋值?
- pine-script - Pine 脚本:如果 x -> 百分比停止 else -> ATR 停止
- java - 通过 java swing 实现快速排序可视化
- aspnetboilerplate - DI 中的 Masstransit unti 测试问题和 ISendEndpointProvider
- python - Qt 样式表破坏了 PyQt5 应用程序的功能
- python - 使用日期时间填写列表中缺失月份的最佳方法是什么?
- docker - 使用 docker 构建 Jekyll 并使用 nginx 服务