wordpress的用戶名密碼網(wǎng)站優(yōu)化seo培
前言
最近公司讓寫一個(gè)數(shù)據(jù)頁面生成pdf的功能,找了一些市面代碼感覺都太麻煩,就自己綜合性整合了一個(gè)便捷的工具類,開發(fā)只需簡單組裝數(shù)據(jù)直接調(diào)用即可快速生成pdf文件。望大家一起學(xué)習(xí)!!!
代碼獲取方式:
- 資源下載
- 后臺(tái)私信(一鍵三連哦!!!)
二、前期準(zhǔn)備
1、html模版(放置接口所在項(xiàng)目的resourcess/templates/
)
需要準(zhǔn)備一個(gè)要看到的pdf模版,利用html代碼形式簡單輸出,其中需要?jiǎng)討B(tài)填充的地方需要用變量進(jìn)行填充
比如頁面顯示:
姓名:韓云中
性別:男
<div>姓名:${name}性別:${sex}
</div>
2、數(shù)據(jù)實(shí)體
AbstractDocumentVo
必須繼承 會(huì)有個(gè)findPrimaryKey
方法需要實(shí)現(xiàn),return一個(gè)你這條數(shù)據(jù)的標(biāo)識(shí)即可
實(shí)體字段名稱必須與html
的${}
內(nèi)容一致
public class User extends AbstractDocumentVo {private String name;private String sex;@Overridepublic String findPrimaryKey() {// 數(shù)據(jù)標(biāo)識(shí) id或則其它均可return this.name;}
}
三、代碼開發(fā)
實(shí)現(xiàn)接口
@GetMapping("/testCreatePdf")
public void testCreatePdf(HttpServletResponse response) {// 方式一:前端直接給你傳遞這個(gè)對象// 方式二:通過前端傳遞的標(biāo)識(shí),自行去庫中進(jìn)行數(shù)據(jù)獲取// ** 兩種方式都需要保證html用到的字段不能存在null 不然報(bào)錯(cuò)User user = new User();user.setName("");user.setSex("");// 生成pdf路徑PdfDocumentGenerator pdfGenerator = new PdfDocumentGenerator();// 生成pdf // 參數(shù)一:classpath中templates下對應(yīng)要用的模版名稱 // 參數(shù)二:模板數(shù)據(jù) // 參數(shù)三:生成pdf名稱// 參數(shù)四:responsepdfGenerator.generate("overseaAssistance.html", overseaVo, "2.pdf", response);
}
四、結(jié)果
得到自己想要的pdf文件
測試數(shù)據(jù)
java實(shí)體
package com.yxy.aob.controller;import com.yxy.common.core.utils.file.pdf.AbstractDocumentVo;
import lombok.Data;
import lombok.EqualsAndHashCode;/*** @Description:* @Author: Hyz* @Date: 2024/10/11 11:22* @Version:1.0*/
@EqualsAndHashCode(callSuper = true)
@Data
public class OverseaVo extends AbstractDocumentVo {/*** 標(biāo)識(shí)*/private String policyNo;/*** 投保人姓名*/private String holderName;/*** 投被保人關(guān)系*/private String relation;/*** 投保人聯(lián)絡(luò)地址*/private String holderAdress;/*** 投保人郵編*/private String holderPostCode;/*** 被保險(xiǎn)人姓名*/private String insuredName;/*** 被保險(xiǎn)人姓名拼音*/private String insuredPingyinName;/*** 被保險(xiǎn)人護(hù)照號(hào)碼*/private String insuredPassportNo;/*** 被保險(xiǎn)人性別*/private String insuredSex;/*** 被保險(xiǎn)人出生日期*/private String insuredBirthday;/*** 被保險(xiǎn)人電話*/private String insuredPhone;/*** 被保險(xiǎn)人證件號(hào)碼*/private String insuredIDNo;/*** 前往國家或地區(qū)*/private String destination;/*** 受益人姓名*/private String beneficiaryName;/*** 備注*/private String remarks;/*** 保險(xiǎn)期間*/private String period;/*** 境外意外傷害或殘疾保額*/private String accidentalSumInsured;/*** 緊急救援醫(yī)療保額*/private String emergencySumInsured;/*** 附加境外緊急救援醫(yī)保額*/private String medicalSumInsured;/*** 總保費(fèi)*/private String premium;/*** 簽發(fā)日期*/private String issueDate;/*** 省份*/private String branchName;/*** 合作公司名稱*/private String companyName;@Overridepublic String findPrimaryKey() {return this.policyNo;}
}
html模版
<html>
<head><title></title><style type="text/css">body {margin-left: 45px;margin-right: 45px;font-family: Arial Unicode MS;font-size: 10px;}table {margin: auto;width: 100%;border-collapse: collapse;border: 1px solid #444444;}th,td {border: 1px solid #444444;font-size: 10px;margin-left: 5px;}.mcContent {line-height: 180%;padding: 20px;}.logo {text-align: center;}.title {text-align: center;font-weight: bold;font-size: 20px;}.notes {font-weight: normal;margin-left: 5px;margin-right: 5px;line-height: 18px;}.text_content {margin-left: 5px;margin-right: 5px;line-height: 18px;}.sum_insured_first_row {width: 20%;}.sum_insured_span {font-size: 10px;}.special_agreements_div {page-break-before: always;font-size: 14px;margin-top: 20px;}.special_agreements_div .special_agreements {font-size: 18px;font-weight: bold;}.title_right {width: 100%;margin: 0 auto;}.title_right p {text-align: left;margin: 0;margin-left: 50%;padding: 0;}@page {size: 8.5in 11in;@bottom-center{content:"page "counter(page)" of "counter(pages);}.signature {margin: 0 auto;clear: both;font-size: 16px;font-weight: bold;}.signature_table {/* font-size: 16px; */font-weight: bold;}</style>
</head>
<body>
<div><div class="title">測試PDF生成--<p>測試單號(hào):${policyNo}</p></div><div class="insurance_info"><table class="insurance_info_table" cellpadding="0" cellspacing="0"width="100%"><tr><td width="20%" colspan="3">投保人<br /> Policyholder</td><td width="43%" colspan="3">${holderName}<br /></td><td width="15%">與被保險(xiǎn)人關(guān)系<br /> Relationship with the Insured</td><td >${relation}</td></tr><tr><td width="20%" colspan="3">聯(lián)絡(luò)地址<br /> Correspondence Address</td><td width="43%" colspan="3">${holderAdress}</td><td width="15%">郵編<br /> Postal Code</td><td >${holderPostCode}</td></tr><tr class="td_width1"><td width="20%" colspan="3">被保險(xiǎn)人姓名<br /> Name of the Insured</td><td width="13%">${holderName}</td><td width="10%">(拼音)<br /> (Pinyin)</td><td width="18%">${insuredPingyinName}</td><td width="15%">護(hù)照號(hào)碼<br /> Passport No</td><td>${insuredPassportNo}</td></tr><tr><td width="5%">性別<br /> Sex</td><td width="5%">${insuredSex}</td><td width="10%">出生日期<br /> Date of Birth</td><td width="13%">${insuredBirthday}</td><td width="10%">電話<br /> Telephone No.</td><td width="18%">${insuredPhone}</td><td width="15%">證件號(hào)碼 <br />ID No.</td><td >${insuredIDNo}</td></tr><tr><td colspan="3">請?jiān)敿?xì)列明前往國家或地區(qū)<br /> Destination</td><td colspan="3">${destination}</td><td>受益人姓名<br /> Beneficiary</td><td>${beneficiaryName}</td></tr><tr><td class="address_class" colspan="3">備注 <br /> Remarks</td><td colspan="5">${remarks}</td></tr><tr><td class="address_class" colspan="3">保險(xiǎn)期間 <br /> Insuranceperiod</td><td colspan="5">${period}</td></tr></table></div><div class="signature"><br /> <br /><table class="signature_table" style="border: 0; width: 100%;"><tr><thstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;">總經(jīng)理簽名:<br /> <span style="font-size:10px">Authorized Signature</span></th><tdstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;"></td><thstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;">公司簽章:<br /><span style="font-size:10px">Company Stamp</span></th><tdstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;"></td><thstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 16%;">簽發(fā)日期:<br /><span style="font-size:10px">Issue Date</span></th><tdstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 16%;">${issueDate}</td></tr></table></div><div class="text_content"><br /> 我從來不是那樣的人,不能耐心地拾起一地碎片,把它們湊合在一起,然后對自己說,這個(gè)修補(bǔ)好了的東西,跟新的完全一樣。一樣?xùn)|西破碎了就是破碎了,我寧愿記住它最好時(shí)的模樣,而不想把它修補(bǔ)好,然后終生看著那些碎了的地方。</div></div>
</body>
</html>