如何使用pdfmake将html表格转换为pdf
How to convert html table to pdf using pdfmake
我试图使用 pdfmake 将我的 html 表转换为 pdf http://pdfmake.org/但我无法了解如何使用
<!DOCTYPE html>
<html>
<?php $pdffile = md5(time()).'.pdf'; ?>
<script src="jquery-1.7.2.min.js" type="text/ecmascript"></script>
<script src="pdfmake.min.js" type="text/javascript"></script>
<script src="vfs_fonts.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(e) {
var table = document.getElementById('table').outerHTML;
var table = document.getElementById('table').innerHTML; // Same response with outerHTML
var table = document.getElementById('table').value; // nothing is happening here
docDefinition = {
content: table
}
pdfMake.createPdf(docDefinition).download('<?php print $pdffile; ?>');
});
</script>
<?php
$con = mysqli_connect('localhost','root','','db');
if($con){
echo 'connected'.'<br/>';
$query = "SELECT * FROM user";
$query_db = mysqli_query($con,$query);
if(mysqli_num_rows($query_db) > 0){
?>
<table cellpadding="0" cellspacing="0" border="1" id="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php while($data = mysqli_fetch_array($query_db)): ?>
<tr>
<td><?php print $data['user_id']; ?></td>
<td><?php print $data['username']; ?></td>
<td><?php print $data['email']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php
}else echo 'not data';
}else echo 'error';
?>
</html>
从我发布的代码中,我的 pdf 文件上正在获取 html 代码而不是 html 表,对不起,我不擅长 javascript?
1.使用 PDFMAKE 时,不能将 HTML 表放入文档定义对象的内容中2.您需要做的是生成一个数组数组。主数组是文档定义对象中表对象中的主体数组。所以现在提取html表格的每一行,并将每个单元格(逐行)推到一个数组中。行完成后,特定行的所有单元格数据被推入 body 数组。需要对所有行执行此操作。就是这样!一个简单的例子是
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title id='title'>HTML Page setup Tutorial</title>
<script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/pdfmake.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/vfs_fonts.js'></script>
<script type="text/javascript">
function myFunction()
{
var docDefinition = {
content: [
{
table: {
body: [
[ 'First', 'Second', 'Third', 'The last one' ],
[ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],
[ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ]
]
}
}]}
pdfMake.createPdf(docDefinition).download('Report.pdf');
}
</script>
</head>
<body>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>
以下是传递表数据的正确语法:-
var docDefinition = {
content: [
{
table: {
body: [
[ 'First', 'Second', 'Third', 'The last one' ],
[ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],
[ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ]
]
}
}
]};
这将打印 HTML 表,而不是 pdf 文件上的 html 代码。您需要以这种格式传递表数据。
相关文章:
- 将画布转换为pdf:黑色背景
- 如何在不在本地下载的情况下将url中提供的文件(pdf/doc)转换为json/string/base64格式
- 将网页转换为具有样式的pdf
- 如何在使用jsPDF将html转换为pdf的同时,在pdf中的某个点进行分页
- 如何将带有大量svg图表的html转换为pdf
- PDF到JPG的转换器
- 如何使用javascript或jquery将整个html页面转换为pdf
- c# mvc nreco HtmlToPdfConverter 问题将 html 文档转换为 pdf
- 使用 php 将 pdf 文件转换为 html 文件
- HTML到PDF的转换仅在客户端使用JavaScript
- 我们如何将URL的PDF转换为convert.javascript中的ToBase64String
- 将数据库中的数据转换为可下载的PDF文件
- HTML和CSS转换为JavaScript中的PDF
- 将Markdown文件转换为PDF,同时剥离Jekyll Front Matter
- 谷歌应用程序脚本:将文本文档转换为PDF的代码
- 谷歌应用程序将电子邮件表单脚本转换为PDF格式,但希望清除一些单元格
- Java使用css+js将html转换为pdf
- 如何使用javascript将Html页面转换为PDF
- 如何使用pdfmake将html表格转换为pdf
- JSPDF添加黑色背景转换PDF