日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java导出pdf集合_java实现导出pdf-Go语言中文社区

發布時間:2025/1/21 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java导出pdf集合_java实现导出pdf-Go语言中文社区 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.添加Maven依賴

org.xhtmlrenderer

flying-saucer-pdf

9.0.3

com.itextpdf

itextpdf

5.5.10

2.工具類

package com.dzqc.dz.common.util;

import com.itextpdf.text.pdf.BaseFont;

import org.xhtmlrenderer.pdf.ITextFontResolver;

import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

public class PdfUtils {

/**

* 把URL轉換為PDF

* @return

* @throws Exception

*/

public static boolean htmlToPdf2(String outputFile, String url)

throws Exception {

File outFile = new File(outputFile);

if (!outFile.exists()) {

outFile.getParentFile().mkdirs();

}

OutputStream os = new FileOutputStream(outputFile);

ITextRenderer renderer = new ITextRenderer();

renderer.setDocument(url);

String fontPath="/simsun.ttc";

// 解決中文支持問題

ITextFontResolver fontResolver = renderer.getFontResolver();

fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

renderer.layout();

renderer.createPDF(os);

os.flush();

os.close();

System.out.println("文件轉換成功");

return true;

}

}

3.Controller層

/**

* 批量導出座次表

*

* @param roomId

* @return

*/

@RequestMapping("/exportSeatNumPlanPDF")

@ResponseBody

public MyAjaxResult exportSeatNumPlanPDF(String roomId,String subjectId, HttpServletRequest request, HttpServletResponse response) {

String path1 = System.getProperty("root") + File.separator + "resources" + File.separator + "PDF" + File.separator;

File file = new File(path1);

if (!file.exists() && !file.isDirectory()) {

System.out.println("//不存在");

file.mkdir();

}

if (subjectId==null||"".equals(subjectId)) {

return MyAjaxResult.fail_300("考試科目id不能為空");

}

BufferedOutputStream out = null;

BufferedInputStream in = null;

String[] split = null;

if (null != roomId && !"".equals(roomId)) {

split = roomId.split(",");

}

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//設置日期格式

String format = df.format(new Date());

String ip = SystemConstants.get("ip");

SystemConstants.get("ip");

String port = SystemConstants.get("port");

try {

System.out.println(roomId);

boolean b = PdfUtils.htmlToPdf2(path1 + format+"-座次表.pdf", "http://"+ip+":" + port + request.getContextPath() + "/stuexamroom/selectRoomDoorPDF?path=zdbpzcb&roomId=" + roomId+"&subjectId="+subjectId);

System.err.println(b);

if (b) {

File f = new File(path1 + format+"-座次表.pdf");

out = new BufferedOutputStream(response.getOutputStream());

in = new BufferedInputStream(new FileInputStream(f));

response.setContentType(new MimetypesFileTypeMap().getContentType(f));// 設置response內容的類型

String filename = format+"-座次表";

if (isMSBrowsers.isMSBrowser(request)) {

filename = URLEncoder.encode(filename, "UTF-8");

} else {

filename = new String(filename.getBytes("utf-8"), "ISO8859-1");

}

response.setHeader("Content-disposition", "attachment;filename=" + filename + "." + "pdf");// 設置頭部信息

byte[] buffer = new byte[1024];

int length = 0;

while ((length = in.read(buffer)) > 0) {

out.write(buffer, 0, length);

}

out.flush();

}

} catch (Exception e) {

e.printStackTrace();

return MyAjaxResult.fail_500("網絡異常");

} finally {

try {

out.close();

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return MyAjaxResult.success("下載成功");

}

@RequestMapping("/selectRoomDoorPDF")

public ModelAndView selectRoomDoorPDF(String path, String roomId,String subjectId) {

String[] split = null;

if (!"".equals(roomId)&& !"null".equals(roomId)) {

split = roomId.split(",");

}

List stuExamRoom = null;

try {

stuExamRoom = stuExamRoomService.selectRoomDoorPDF(split,subjectId);

System.out.println(stuExamRoom);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Map ma = new HashMap<>(16);

ma.put("stuExamRooms", stuExamRoom);

return new ModelAndView("pages/dzBackEnd/" + path, ma);

}

4.頁面

pageEncoding="UTF-8" isELIgnored="false" %>

座次表

@page {

size: A4;

margin: 0;

}

html, body {

margin: 0;

padding: 0;

}

h2, h4 {

text-align: center;

}

.wrap {

width: 794px;

/* height: 1123px; */

height: auto;

margin: 0 auto;

}

ul {

width: 100%;

margin: 0;

padding: 0;

overflow: hidden;

}

li {

list-style: none;

width: 20%;

height: auto;

float: left;

margin-bottom: 3px;

text-align: center;

}

li img {

width: 120px;

}

li p {

margin: 0;

text-align: center;

}

第${room.roomCode}考場 (個數:${roompage})

  • ${var.seatNum} ${var.name}

第${rooCode}考場 (個數:${fn:length(list)})

  • ${var.sear} ${var.name}

--%>

5.效果

6.注意點

1.div的文字換行

在生成的pdf中,默認不會換行;實現換行在div的style中加入?word-wrap:break-word;

2.在轉換pdf的時候對html文件的格式要求比較嚴格,標簽必需有結束標簽,否則將會報一系列錯誤

例如:換行只有一個
標簽

則會報異常:

ERROR: '元素類型 "br" 必須由匹配的結束標記 "" 終止。'

如果是
則不會報異常。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的java导出pdf集合_java实现导出pdf-Go语言中文社区的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。