QT生成PDF

使用第三方工具wkhtmltopdf

//p:自定义html缓存和PDF保存文件夹;content:html;name:pdf文件名称
	bool BuildPDF(QString& p,QString content,QString name)//生成pdf
	{
		QString softpath = QApplication::applicationDirPath() + "/wkhtmltopdf/wkhtmltopdf.exe";
		QString filePath = QString("\"").append(p).append("/").append(name).append(".pdf\"");
		QString htmlpath = p + QString("/").append("DEMO").append(".html");//临时文件
		p.append("/").append(name).append(".pdf");

		QFileInfo qfi(softpath);
		if (!qfi.isFile())
		{
			return false;
		}

		QFile fi(htmlpath);
		if (!fi.open(QFile::WriteOnly | QIODevice::Truncate))
		{
			return false;
		}

		fi.write(content.toLocal8Bit());//html数据
		fi.close();

		QString path = QString("\"") + softpath + QString("\" \"") + htmlpath + QString("\" ") + filePath;
		int ret = QProcess::execute(path);
		if (ret < 0)
		{
			FXC << "failed : " << filePath << ret;
			return false;
		}

		return true;
	}

下面是content:

DOCTYPE html>
<html>
	<head>
		<meta lang="zh-cn">
		<meta charset="UTF-8">
		<style>
			/* 创建通用样式类 */
			        .div-pandel {
			            width: 100%;
			            margin-top: 32px;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .title-large {
			            /* width: 618px;
			            height: 52px; */
			            font-size: 24px;
			            font-weight: normal;
			            font-stretch: normal;
			            margin-left: 45px;
			            line-height: 44px;
			            letter-spacing: 0px;
			            margin-top: 90px;
			            color: #000000;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .divier-full {
			            size: 1px;
			            color: #eaeaea;
			            margin-top: 22px;
			            margin-left: 45px;
			            margin-right: 45px;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        /* 二级标题 */
			        .title-info {
			            font-size: 26px;
			            font-weight: normal;
			            font-stretch: normal;
			            line-height: 44px;
			            letter-spacing: 0px;
			            color: #000000;
			            margin-top:10px;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .label-title {
			            height: 36px;
			            font-size: 20px;
			            font-weight: normal;
			            font-stretch: normal;
			            line-height: 44px;
			            letter-spacing: 0px;
			            color: #999999;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .label-title-ED5931 {
			            height: 36px;
			            font-size: 20px;
			            font-weight: normal;
			            font-stretch: normal;
			            line-height: 44px;
			            letter-spacing: 0px;
			            color: #FFFFFF;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .lable-value {
			            font-size: 20px;
			            height: 36px;
			            font-weight: normal;
			            letter-spacing: 0px;
			            color: #000000;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .label-layout-full {
			            width: 100%;
			            margin-top: 10px;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .label-layout {
			            float: left;
			            width: 50%;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			
			        .table-layout {
			            width: 100%;
						font-family:"MicroSoft YaHei", Gulim, "Malgun Gothic", sans-serif;
			        }
			/*表格行*/
			        .table-tr-ED5931 {
			            width: 100%;
			            height: 44px;
						word-wrap: break-word;
						word-break: break-all;
						overflow: hidden;
			            background-color: #ED5931;
			        }
					
			        .table-tr-f6 {
			            width: 100%;
			            height: 44px;
						word-wrap: break-word;
						word-break: break-all;
						overflow: hidden;
			            background-color: #f6f6f6;
			        }
					
					.table-tr-ff {
			            width: 100%;
			            height: 44px;
						word-wrap: break-word;
						word-break: break-all;
						overflow: hidden;
			            background-color: #FFFFFF;
			        }
			
			        .table-tr {
			            width: 100%;
			            height: 118px;
						word-wrap: break-word;
						word-break: break-all;
						overflow: hidden;
			            background-color: #FFFFFF;
			        }
			      
			        .table-td {
			            height: 44px;
			            font-size: 16px;
			            font-weight: normal;
			            font-stretch: normal;
			            line-height: 44px;
			            letter-spacing: 0px; 
						word-wrap: break-word;
						word-break: break-all;
						overflow: hidden;
			            color: #000000;
			        }
		style>
	head>

	<body>
		<h1 style="text-align:center;font-family:MicroSoft YaHei, Gulim, Malgun Gothic">
			<img src="E:/Software/Debug/company.png"
				 style="float:right;width:283px;height:49;" />Vehicle Diagnostic Reporth1>
		<hr />
		<div class="div-pandel">
			<div>
				<span class="title-info">Vehicle Informationspan>
			div>
			<table class="table-layout">
				<tr class="table-tr-ff">
					<td width=33%
						class="table-td">
						<span class="label-title">Brand : span>
						<span class="lable-value">DEMOspan>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">VIN : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Plate Number : span>
						<span class="lable-value">span>
					td>
				tr>
				<tr class="table-tr-ff">
					<td width=33%
						class="table-td">
						<span class="label-title">Model : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Mileage : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Color : span>
						<span class="lable-value">span>
					td>
				tr>
				<tr class="table-tr-ff">
					<td width=33%
						class="table-td">
						<span class="label-title">Year : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Capacity : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Status : span>
						<span class="lable-value">Unset statusspan>
					td>
				tr>
				<tr class="table-tr-ff">
					<td width=33%
						class="table-td">
						<span class="label-title">Engine : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Reference Number : span>
						<span class="lable-value">span>
					td>
					<td width=33%
						class="table-td">
						<span class="label-title">Date : span>
						<span class="lable-value">2023-04-28span>
					td>
				tr>
			table>
		div>
		<hr />
		<div class="div-pandel">
			<div>
				<span class="title-info"
					  style="font-size:30px; color:red;">故障码span>
			div>
			<table class="table-layout">
				<tr style="Background:#A0A0A0;">
					<td width=27%
						class="table-td">IDtd>
					<td width=20%
						class="table-td">Statustd>
					<td width=53%
						class="table-td">Descriptiontd>
				tr>
				<tr class="table-tr-ff">
					<td width=27%
						class="table-td">P0030td>
					<td width=20%
						class="table-td">激活td>
					<td width=53%
						class="table-td">HO2S加热器控制电路1排传感器1td>
				tr>
				<tr class="table-tr-f6">
					<td width=27%
						class="table-td">P2096td>
					<td width=20%
						class="table-td">历史td>
					<td width=53%
						class="table-td">催化转化器燃油修正过稀(1排)td>
				tr>
				<tr class="table-tr-ff">
					<td width=27%
						class="table-td">P0130td>
					<td width=20%
						class="table-td">激活td>
					<td width=53%
						class="table-td">O2传感器电路1排传感器1td>
				tr>
				<tr class="table-tr-f6">
					<td width=27%
						class="table-td">P0616td>
					<td width=20%
						class="table-td">历史td>
					<td width=53%
						class="table-td">起动机继电器电路电压低td>
				tr>
				<tr class="table-tr-ff">
					<td width=27%
						class="table-td">P0335td>
					<td width=20%
						class="table-td">激活td>
					<td width=53%
						class="table-td">曲轴位置传感器 ’A’ 电路td>
				tr>
			table>
		div>
		<div>
			<div style="background:#ED5931;text-align:center;">
				<span class="title-info">Contact Informationspan>
			div>
			<table class="table-layout">
				<tr class="table-tr-ff">
					<td span=2
						class="table-td">
						<span class="label-title">Name : span>
						<span class="lable-value">span>
					td>
				tr>
				<tr class="table-tr-ff">
					<td span=2
						class="table-td">
						<span class="label-title">Address : span>
						<span class="lable-value">00.12span>
					td>
				tr>
				<tr class="table-tr-ff">
					<td width=50%
						class="table-td">
						<span class="label-title">Telephone : span>
						<span class="lable-value">123456span>
					td>
					<td width=50%
						class="table-td">
						<span class="label-title">Email : span>
						<span class="lable-value">[email protected]span>
					td>
				tr>
				<tr class="table-tr-ff">
					<td span=2
						class="table-td">
						<span class="label-title">Website : span>
						<span class="lable-value">www.fxowelspan>
					td>
				tr>
			table>
		div>
		<hr />
		<div style="background:#F0F0F0;font-size:20px;text-align:center;color:#333333;font-family:MicroSoft YaHei, Gulim, Malgun Gothic">Software Version:V9.15.016div>
	body>
html>

你可能感兴趣的:(qt,pdf,开发语言)