1.基于itexpdf 插件
pom.xml文件添加
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>
/**
* 下载接口文档pdf
* @param req
* @param response
* @param apiId
*/
@RequestMapping(value = "downloadPdf", method = RequestMethod.GET)
@ResponseBody
public void downloadPdf(HttpServletRequest req, HttpServletResponse response) {
String newsContent = "<div id='pano_camera' class='servie_info1'><h3>111</h3>222</div>";
newsContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
+ newsContent;
// newsContent = newsContent.replaceAll("\240$", "");
// newsContent = "<html><body>123</body></html>";
String path = req.getRealPath("/") + "pdf";
File pathFile = new File(path);
//如果文件夹不存在则创建
if(!pathFile.exists() && !pathFile.isDirectory()) {
pathFile.mkdir();
}
String filename = "test";
FileOutputStream fos;
try {
fos = new FileOutputStream(path + File.separator + filename + ".html");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(newsContent);
osw.flush();
fos.close();
osw.close();
* 下载接口文档pdf
* @param req
* @param response
* @param apiId
*/
@RequestMapping(value = "downloadPdf", method = RequestMethod.GET)
@ResponseBody
public void downloadPdf(HttpServletRequest req, HttpServletResponse response) {
String newsContent = "<div id='pano_camera' class='servie_info1'><h3>111</h3>222</div>";
newsContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
+ newsContent;
// newsContent = newsContent.replaceAll("\240$", "");
// newsContent = "<html><body>123</body></html>";
String path = req.getRealPath("/") + "pdf";
File pathFile = new File(path);
//如果文件夹不存在则创建
if(!pathFile.exists() && !pathFile.isDirectory()) {
pathFile.mkdir();
}
String filename = "test";
FileOutputStream fos;
try {
fos = new FileOutputStream(path + File.separator + filename + ".html");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(newsContent);
osw.flush();
fos.close();
osw.close();
//通过这个 方法来进行转换
ParseHtmlAsian.createPdf(path, filename);
ParseHtmlAsian.createPdf(path, filename);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
File file = new File(path + File.separator + filename + ".pdf");
byte[] buf = new byte[1024 * 1024 * 10];
int len = 0;
ServletOutputStream ut = null;
BufferedInputStream br = null;
response.reset();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "must-revalidate, no-transform");
response.setDateHeader("Expires", 0L);
String userAgent = req.getHeader("User-Agent");
boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);
String displayFilename = file.getName();
response.setContentType("application/x-download");
if (isIE) {
try {
displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
} else {
try {
displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
}
try {
br = new BufferedInputStream(new FileInputStream(file));
ut = response.getOutputStream();
while ((len = br.read(buf)) != -1)
ut.write(buf, 0, len);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}