生活随笔
收集整理的這篇文章主要介紹了
java 模板 word转pdf 可分页 带图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java 模板 word轉pdf 可分頁 帶圖片
之前寫過一個簡單的案例,但是在項目中完全不能滿足客戶的需求,所以重新用啦一種方式來寫,采用了word轉換pdf的方式,這種經過不斷研究,滿足了可分頁,列表循環數據可帶圖片,可以說很完善了。
第一步:先來編寫word自定義模板以及轉換模板,轉換模板可以直接是客戶發過來的樣式模板,自定義模板呢,就是帶java中賦值的占位符字段。
下面舉例幾個:
單個:
列表:無邊框
列表:有邊框
列表:帶圖片 單個圖片 日期格式
@ApiOperation(value
= "導出pdf報告")@GetMapping("/exportPdf/{id}")public void exportPdf(HttpServletResponse response
, @PathVariable Integer id
) {mLegalCompanyService
.exportPdf(response
, id
);}
@Value("${upload.dir}")String ulr
;@Value("${imgUrl.url}")String path
;@Overridepublic void exportPdf(HttpServletResponse response
, Integer id
) {long old
= System.currentTimeMillis();LegalCompany legalCompany
= this.getById(id
);if (ObjectUtils.isEmpty(legalCompany
)) {return;}HashMap<String, Object> map
= new HashMap<>();map
.put("title", legalCompany
.getCompanyName());map
.put("date1", "第一次檢查");addBasis(map
, legalCompany
);addRectifyList(map
, legalCompany
);map
.put("date", new Date());if (StringUtils.isNotEmpty(legalCompany
.getSignaturePicture())) {ImageEntity image
= new ImageEntity();image
.setHeight(842);image
.setWidth(595);image
.setUrl(path
+ legalCompany
.getSignaturePicture());image
.setType(ImageEntity.URL);map
.put("SignaturePicture", image
);}try {XWPFDocument doc
= WordExportUtil.exportWord07(ulr
+ "/寫模板.docx", map
);FileOutputStream fos
= new FileOutputStream(ulr
+ "/轉換模板.docx");doc
.write(fos
);fos
.close();InputStream inputStream
= new FileInputStream(ulr
+ "/轉換模板.docx");OutputStream outputStream
= response
.getOutputStream();response
.setCharacterEncoding("utf-8");response
.setHeader("content-type", "application/octet-stream");String fileName
= URLEncoder.encode(legalCompany
.getCompanyName() + "-" + TimeUtils.formatDate(inspectData
, TimeUtils.DATE_FORMAT_DATEONLY) + "-" + "檢查報告" + ".pdf", "UTF-8");response
.setHeader("Content-Disposition", "attachment;filename=" + fileName
);response
.setHeader("Access-Control-Expose-Headers", "Content-Disposition");if (!AsposeWordsUtils.getLicense()) {return;}Document doct
= new Document(inputStream
);doct
.save(outputStream
, SaveFormat.PDF);long now
= System.currentTimeMillis();log
.info("共耗時:" + ((now
- old
) / 1000.0) + "秒");outputStream
.flush();outputStream
.close();} catch (IOException e
) {log
.info("IOException==>異常:{}==>{}", e
);} catch (Exception e
) {log
.info("Exception==>異常:{}==>{}", e
);}}private void addBasis(HashMap<String, Object> map
, LegalCompany legalCompany
) {List<String> list
= Lists.newArrayList();LegalCheckTheRecord checkTheRecord
= legalCheckTheRecordService
.getByCompanyId(legalCompany
.getCompanyId());List<BasisInfo> basisInfoList
= basisInfoService
.findListByBasisId(checkTheRecord
.getBasisId());if (CollectionUtils.isNotEmpty(basisInfoList
)) {for (int i
= 0; i
< basisInfoList
.size(); i
++) {list
.add(basisInfoList
.get(i
).getBasisInfoName());}map
.put("basis", list
);}}private void addRectifyList(HashMap<String, Object> map
, LegalCompany legalCompany
) {List<HashMap<String, Object>> list
= new ArrayList<>();List<LegalInspect> legalInspects
= legalInspectService
.selectList(legalCompany
.getCompanyId
);if (CollectionUtils.isNotEmpty(legalInspects
)) {for (int i
= 1; i
<= legalInspects
.size(); i
++) {LegalInspect legalInspect
= legalInspects
.get(i
- 1);HashMap<String, Object> mapData
= new HashMap<>();mapData
.put("id", isNull(i
));mapData
.put("problem", legalInspect
.getProblem());mapData
.put("levelId", legalInspect
.getLevelId().equals(1) ? "一般隱患" : "重大隱患");if (StringUtils.isNotEmpty(legalInspect
.getLivePhotos())) {ImageEntity image
= new ImageEntity(path
+ legalInspect
.getLivePhotos(), 100, 100);mapData
.put("livePhotos", image
);}mapData
.put("isRectify", legalInspect
.getIsRectify().equals(1) ? "未整改" : "已整改");if (StringUtils.isNotEmpty(legalInspect
.getFinalPhotos())) {ImageEntity image
= new ImageEntity(path
+ legalInspect
.getFinalPhotos(), 100, 100);mapData
.put("finalPhotos", image
);}mapData
.put("measure", legalInspect
.getMeasure());mapData
.put("safetyPrincipal", legalCompany
.getSafetyPrincipal());if (null != periodMap
&& periodMap
.containsKey(legalInspect
.getDeadlineId())) {mapData
.put("deadlineNum", periodMap
.get(legalInspect
.getDeadlineId()).getDeadlineNum() + "天");}Long between
= DateUtiles.between(legalInspect
.getSubmitTime().getTime(), System.currentTimeMillis());int num
= 1;if (between
> legalCompany
.getCycleDate()) {num
+= between
/ legalCompany
.getCycleDate();}mapData
.put("num", "第" + num
+ "次檢查");list
.add(mapData
);}}map
.put("inspect", list
);}
public class AsposeWordsUtils {
public static boolean getLicense() {boolean result
= false;try {InputStream is
= AsposeWordsUtils.class.getClassLoader().getResourceAsStream("license.xml");License aposeLic
= new License();aposeLic
.setLicense(is
);result
= true;} catch (Exception e
) {e
.printStackTrace();}return result
;}
}
pdf下載后:
這個報告文件頁碼很長,我只截取了一小部分,代碼很多,但是只截取了一些,可能沒辦法提供全,有些涉及企業機密,要是有哪不懂,可以評論留言,如果有需要可以提供其他的給你們參考。
這個是不限頁面的,單圖片 列表圖片都可以下載下來。
總結
以上是生活随笔為你收集整理的java 模板 word转pdf 可分页 带图片的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。