后端使用TCPDF插件生成PDF:
- 首先,安装Composer,PHP包管理工具
参考:
下载网址:https://getcomposer.org/download/
安装完毕,控制台输入composer -v,显示下图,说明安装成功
2.使用compser安装tcpdf,在指定的文件夹下,打开控制台输入composer require tecnickcom/tcpdf
出现composer.json、composer.lock、vendor,并且vendo文件夹下出现,说明插件安装成功
参考:
tcpdf官方文档:https://tcpdf.org/examples/
3.后端生成PDF二进制文件流
<?php
require_once "./vendor/tecnickcom/tcpdf/tcpdf.php";
$DB_HOST = 'localhost';
// 下面是数据库配置
$DB_NAME = '***';
$DB_USER = '***';
$DB_PWD = '***';
$pdo = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME",$DB_USER,$DB_PWD);
// 设置编码,否则中文显示?
$pdo->exec("set names utf8");
if (!$pdo)
{
die('Could not connect: ' . mysql_error());
}
if(empty($_GET['num']))
{
echo 'error:null';
exit();
}
$num = $_GET['num'];
// 其中table表示查询的数据表
$sql = "select * from table where worknum=".$num;
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('droidsansfallback', '', 18);
$pdf->AddPage();
$result = $pdo->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)){
if($row) {
$html = '
<div style="text-align:center;margin-bottom:20px;vertical-align:middle;">
<img src="./images/logo.png" width="64" height="64"/>
<h1 style="font-size:20pt;">标题</h1>
</div>
<table>
<tr>
<td width="8%"></td>
<td>
<table width="380">
<tr height="25">
<td width="150" align="right" >准考证号:</td>
<td align="left" style="border-bottom-color:#000;">'.$row['examnum'].'</td>
</tr>
<tr height="25">
<td width="150" align="right">考生姓名:</td>
<td align="left" style="border-bottom-color:#000;">'.$row['name'].'</td>
</tr>
<tr height="25">
<td width="150" align="right">考生学号:</td>
<td align="left" style="border-bottom-color:#000;">'.$row['worknum'].'</td>
</tr>
<tr height="25">
<td width="150" align="right">竞赛地点:</td>
<td align="left" style="border-bottom-color:#000;">'.$row['examroom'].'</td>
</tr>
<tr height="25">
<td width="150" align="right">座位号:</td>
<td align="left" style="border-bottom-color:#000;">'.$row['seatnum'].'</td>
</tr>
<tr height="25">
<td width="150" align="right">竞赛类别:</td>
<td align="left" style="border-bottom-color:#000;">'.$row['cate'].'</td>
</tr>
<tr height="25">
<td width="150" align="right">竞赛时间:</td>
<td align="left" style="border-bottom-color:#000;">2021.04.24 9:00--11:00</td>
</tr>
</table>
</td>
</tr>
</table>
';
$pdf->writeHTML($html, false, false, false, false, '');
//Close and output PDF document
$pdf->Output(__DIR__.'/'.iconv("utf-8","gb2312",'准考证.pdf'), 'FD');
}
}
备注: 出现中文编码,输出会出乱码,需要自己配置
参考:
- https://cloud.tencent.com/developer/article/1488576
- https://blog.csdn.net/ghjhot/article/details/7930386
微信小程序下载二进制流,并预览
tcpdf() {
let num = this.data.num,
name = this.data.name;
this.setData({
loadingHidden: false,
})
wx.request({
url: 'https://www.sxmda.net/api/download_tcpdf.php?num='+num,
header: {
"content-type": "application/json",
},
responseType: "arraybuffer", //注意这里的responseType
success: (result) => {
console.log("下载成功!", result);
var fileManager = wx.getFileSystemManager();
var FilePath = wx.env.USER_DATA_PATH + "/" + num +'-'+name+"-英语竞赛准考证.pdf";
fileManager.writeFile({
data: result.data,
filePath: FilePath,
encoding: "binary", //编码方式
success: res => {
console.log('编码格式');
wx.openDocument({ //我这里成功之后直接打开
filePath: FilePath,
showMenu:true,
fileType: "pdf",
success: result => {
this.setData({
loadingHidden: true,
})
console.log("打开文档成功");
},
fail: err => {
this.setData({
loadingHidden: true,
})
console.log("打开文档失败", err);
}
});
},
fail: res => {
this.setData({
loadingHidden: true,
})
wx.showToast({
title: '导出失败!',
icon: 'none', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
duration: 2000, //停留时间
})
console.log(res);
}
})
},
fail(err) {
this.setData({
loadingHidden: true,
})
console.log(err)
}
})
}