|
|
@@ -13,9 +13,7 @@ import com.goafanti.common.utils.word.WordUtils;
|
|
|
import com.goafanti.core.mybatis.BaseMybatisDao;
|
|
|
import com.goafanti.core.mybatis.page.Pagination;
|
|
|
import com.goafanti.core.shiro.token.TokenManager;
|
|
|
-import com.itextpdf.text.Document;
|
|
|
-import com.itextpdf.text.Image;
|
|
|
-import com.itextpdf.text.Rectangle;
|
|
|
+import com.itextpdf.text.*;
|
|
|
import com.itextpdf.text.pdf.*;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -25,10 +23,9 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.util.*;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
public class RDServiceImpl extends BaseMybatisDao<RdDetailsMapper> implements RDService {
|
|
|
@@ -170,94 +167,29 @@ public class RDServiceImpl extends BaseMybatisDao<RdDetailsMapper> implements RD
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void downloadDetailsPDF(Long id, HttpServletResponse response) {
|
|
|
+ public void pushDownloadDetailsPDF(Long id, HttpServletResponse response) {
|
|
|
OutRdDetails bo = rdDetailsMapper.details(id);
|
|
|
- rdDetailsMapper.updateBrowseOrDownload(id,1);
|
|
|
- OutWordRdDetails data =new OutWordRdDetails();
|
|
|
- BeanUtils.copyProperties(bo,data);
|
|
|
- data.setStartEndTime(bo.getRdStartStr()+"~"+bo.getRdEndStr());
|
|
|
+ rdDetailsMapper.updateBrowseOrDownload(id, 1);
|
|
|
+ OutWordRdDetails data = new OutWordRdDetails();
|
|
|
+ BeanUtils.copyProperties(bo, data);
|
|
|
+ data.setStartEndTime(bo.getRdStartStr() + "~" + bo.getRdEndStr());
|
|
|
|
|
|
String tableName = data.getRdName();
|
|
|
- String url=uploadPath+"/tmp/RD_details_word_template.pdf";
|
|
|
-
|
|
|
- String realFileName = tableName+ DateUtils.parseDateToStr(AFTConstants.YYYYMMDDHHMMSS, new Date())+".docx";
|
|
|
- response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
- OutputStream out = null;
|
|
|
- ByteArrayOutputStream bos = null;
|
|
|
- PdfStamper stamper = null;
|
|
|
- PdfReader reader = null;
|
|
|
+ String url = uploadPath + "/tmp/RD_details_word_template.pdf";
|
|
|
+
|
|
|
+ String realFileName = uploadPath+"/tmp/"+DateUtils.parseDateToStr(AFTConstants.YYYYMMDDHHMMSS_OUT, new Date()) + ".pdf";
|
|
|
+// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
+
|
|
|
+ Document document=new Document();
|
|
|
try {
|
|
|
- FileUtils.setAttachmentResponseHeader(response, tableName,realFileName);
|
|
|
- // 保存到本地
|
|
|
- // out = new FileOutputStream(fileName);
|
|
|
- // 输出到浏览器端
|
|
|
- out = response.getOutputStream();
|
|
|
- // 读取PDF模板表单
|
|
|
- reader = new PdfReader(url);
|
|
|
- // 字节数组流,用来缓存文件流
|
|
|
- bos = new ByteArrayOutputStream();
|
|
|
- // 根据模板表单生成一个新的PDF
|
|
|
- stamper = new PdfStamper(reader, bos);
|
|
|
- // 获取新生成的PDF表单
|
|
|
- AcroFields form = stamper.getAcroFields();
|
|
|
- // 给表单生成中文字体,这里采用系统字体,不设置的话,中文显示会有问题
|
|
|
- BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
|
|
|
- form.addSubstitutionFont(font);
|
|
|
- // 装配数据
|
|
|
- Map<String, Object> params = new HashMap<>(15);
|
|
|
- params.put("RdName", data.getRdName());
|
|
|
- params.put("userName", data.getUserName());
|
|
|
-
|
|
|
- // 遍历data,给pdf表单赋值
|
|
|
- for(String key : params.keySet()){
|
|
|
- // 图片要单独处理
|
|
|
- if("studentImg".equals(key)){
|
|
|
- int pageNo = form.getFieldPositions(key).get(0).page;
|
|
|
- Rectangle signRect = form.getFieldPositions(key).get(0).position;
|
|
|
- float x = signRect.getLeft();
|
|
|
- float y = signRect.getBottom();
|
|
|
- String studentImage = params.get(key).toString();
|
|
|
- //根据路径或Url读取图片
|
|
|
- Image image = Image.getInstance(studentImage);
|
|
|
- //获取图片页面
|
|
|
- PdfContentByte under = stamper.getOverContent(pageNo);
|
|
|
- //图片大小自适应
|
|
|
- image.scaleToFit(signRect.getWidth(), signRect.getHeight());
|
|
|
- //添加图片
|
|
|
- image.setAbsolutePosition(x, y);
|
|
|
- under.addImage(image);
|
|
|
- }
|
|
|
- // 设置普通文本数据
|
|
|
- else {
|
|
|
- form.setField(key, params.get(key).toString());
|
|
|
- }
|
|
|
- }
|
|
|
- // 表明该PDF不可修改
|
|
|
- stamper.setFormFlattening(true);
|
|
|
- // 关闭资源
|
|
|
- stamper.close();
|
|
|
- // 将ByteArray字节数组中的流输出到out中(即输出到浏览器)
|
|
|
- Document doc = new Document();
|
|
|
- PdfCopy copy = new PdfCopy(doc, out);
|
|
|
- doc.open();
|
|
|
- PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
|
|
|
- copy.addPage(importPage);
|
|
|
- doc.close();
|
|
|
- }catch (Exception e){
|
|
|
+ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(realFileName));
|
|
|
+ document.open();
|
|
|
+ document.add(new Paragraph("Hello Word"));
|
|
|
+ document.close();
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
- try {
|
|
|
- if (out != null) {
|
|
|
- out.flush();
|
|
|
- out.close();
|
|
|
- }
|
|
|
- if (reader != null) {
|
|
|
- reader.close();
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|