|
|
@ -5,14 +5,13 @@ import lombok.Data; |
|
|
import lombok.NoArgsConstructor; |
|
|
import lombok.NoArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.poi.util.Units; |
|
|
import org.apache.poi.util.Units; |
|
|
import org.apache.poi.xwpf.converter.pdf.PdfConverter; |
|
|
|
|
|
import org.apache.poi.xwpf.converter.pdf.PdfOptions; |
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.ParagraphAlignment; |
|
|
import org.apache.poi.xwpf.usermodel.ParagraphAlignment; |
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
|
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
|
|
import org.apache.poi.xwpf.usermodel.XWPFRun; |
|
|
import org.apache.poi.xwpf.usermodel.XWPFRun; |
|
|
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.*; |
|
|
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.*; |
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; |
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; |
|
|
|
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect; |
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
import org.springframework.core.io.Resource; |
|
|
import org.springframework.core.io.Resource; |
|
|
import org.springframework.util.FileCopyUtils; |
|
|
import org.springframework.util.FileCopyUtils; |
|
|
@ -45,7 +44,8 @@ public class WordCertificateService { |
|
|
*/ |
|
|
*/ |
|
|
public byte[] generateCertificate(CertificateData data) throws Exception { |
|
|
public byte[] generateCertificate(CertificateData data) throws Exception { |
|
|
XWPFDocument document = prepareDocument(data); |
|
|
XWPFDocument document = prepareDocument(data); |
|
|
|
|
|
// 方法1:设置文档为只读推荐
|
|
|
|
|
|
document.enforceReadonlyProtection(); |
|
|
// 保存到字节数组
|
|
|
// 保存到字节数组
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
document.write(outputStream); |
|
|
document.write(outputStream); |
|
|
@ -54,20 +54,6 @@ public class WordCertificateService { |
|
|
return outputStream.toByteArray(); |
|
|
return outputStream.toByteArray(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 生成PDF格式的证书 |
|
|
|
|
|
*/ |
|
|
|
|
|
public byte[] generateCertificateAsPdf(CertificateData data) throws Exception { |
|
|
|
|
|
XWPFDocument document = prepareDocument(data); |
|
|
|
|
|
|
|
|
|
|
|
// 转换为PDF
|
|
|
|
|
|
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream(); |
|
|
|
|
|
PdfConverter.getInstance().convert(document, pdfOutputStream, PdfOptions.create()); |
|
|
|
|
|
document.close(); |
|
|
|
|
|
|
|
|
|
|
|
return pdfOutputStream.toByteArray(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 准备文档(用于Word或PDF生成) |
|
|
* 准备文档(用于Word或PDF生成) |
|
|
*/ |
|
|
*/ |
|
|
@ -118,8 +104,8 @@ public class WordCertificateService { |
|
|
// 添加新内容
|
|
|
// 添加新内容
|
|
|
XWPFRun run = paragraph.createRun(); |
|
|
XWPFRun run = paragraph.createRun(); |
|
|
run.setText(text); |
|
|
run.setText(text); |
|
|
run.setFontFamily("宋体"); |
|
|
run.setFontFamily("FangSong_GB2312"); |
|
|
run.setFontSize(12); |
|
|
run.setFontSize(16); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|