SpringBoot(⼗五)_springboot实现预览pdf 最近,项⽬上要做个打印的东西,还要预览。我想就直接⽣成pdf预览,然后⽤户选择打印
于是,昨天了资料。⼀般⽤itext 进⾏转pdf。于是我就⽤springboot试了试,代码⽐较简单,现在只是简单的⼀个实现。
整体流程如下
1.获取数据(⽐如从数据库获取数据)
2.通过freemarker 渲染页⾯
3.将渲染的页⾯进⾏转换为pdf,放在本地
4.读取pdf,实现预览
主要代码
pom⽂件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- mvnrepository/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- mvnrepository/artifact/l/xmlworker -->
<dependency>
<groupId>l</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
PdfController代码
package com.kevin.ller;
import Document;
import DocumentException;
import pdf.PdfWriter;
import l.xml.XMLWorkerFontProvider;
import l.xml.XMLWorkerHelper;
plate.Configuration;
plate.Template;
import org.apachemons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
/**
* @author: kevin
* @Date: 2018/11/16
*/
@RestController
public class PdfController {
@Value("${DEST}")
private String dest;
@Value("${HTML}")
private String html;
@Value("${FONT}")
private String font;
private static Configuration freemarkerCfg = null;
@RequestMapping(value = "helloPdf")
public void showPdf(HttpServletResponse response) throws IOException, DocumentException {
//需要填充的数据
Map<String, Object> data = new HashMap<>(16);
data.put("name", "kevin");
String content = freeMarkerRender(data,html);
//创建pdf
createPdf(content, dest);
// 读取pdf并预览
readPdf(response);
}
public  void createPdf(String content,String dest) throws IOException, DocumentException {
/
/ step 1
Document document = new Document();
// step 2
PdfWriter writer = Instance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);        ister(font);
new Bytes("UTF-8")), null, Charset.forName("UTF-8"), fontImp);
// step 5
document.close();
}
/**
* freemarker渲染html
*/
public  String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码⽅式
setFreemarkerCfg();
Template template = Template(htmlTmp);
template.setEncoding("UTF-8");
//将合并后的数据和模板写⼊到流中,这⾥使⽤的字符流
template.process(data, out);
out.flush();
String();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* 设置freemarkerCfg
*/
private void setFreemarkerCfg() {
freemarkerCfg = new Configuration();
/
/freemarker的模板⽬录
try {
freemarkerCfg.setDirectoryForTemplateLoading(new ClassPathResource("template").getFile());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取本地pdf,这⾥设置的是预览
*/
private void readPdf(HttpServletResponse response) {
response.setContentType("application/pdf");
try {
File file = new File(dest);
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream outputStream = OutputStream();
IOUtils.ByteArray(fileInputStream), outputStream);
response.setHeader("Content-Disposition",
"inline; filename= file");
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
spring framework版本
}
}
运⾏效果
设置头信息使浏览器下载⽂件或预览
强制浏览器下载
response.setHeader("content-disposition", "attachment;filename=" + realName);浏览器尝试打开,⽀持office online或浏览器预览pdf功能response.setHeader("content-disposition", "inline;filename=" + realName);
完整代码 :
玩的开⼼!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。