import cn.ecut.file.pdf.entity.AdmissionCard;
import cn.ecut.file.pdf.service.PdfCustomService;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* @since 2021/7/11 16:12
*/
@Service
@Slf4j
public class PdfCustomServiceImpl implements PdfCustomService {
@Override
public void generatorAdmissionCard(AdmissionCard admissionCard, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {
// Template name
String templateName = "Admission Ticket-Template.pdf";
String path = "";
// Get the operating system name and determine the path to store the template based on the system name
String systemName = System.getProperty("");
if(systemName.toUpperCase().startsWith("WIN")){
path = "D:/pdf/";
}else {
path = "/usr/local/pdf/";
}
// Generate the file name of the export PDF
String fileName = admissionCard.getName() + "-Master's Admission Ticket.pdf";
fileName = URLEncoder.encode(fileName, "UTF-8");
// Set response header
response.setContentType("application/force-download");
response.setHeader("Content-Disposition",
"attachment;fileName=" + fileName);
OutputStream out = null;
ByteArrayOutputStream bos = null;
PdfStamper stamper = null;
PdfReader reader = null;
try {
// Save to local
// out = new FileOutputStream(fileName);
// Output to the browser
out = response.getOutputStream();
// Read PDF template form
reader = new PdfReader(path + templateName);
// Byte array stream, used to cache file streams
bos = new ByteArrayOutputStream();
// Generate a new PDF based on the template form
stamper = new PdfStamper(reader, bos);
// Get the newly generated PDF form
AcroFields form = stamper.getAcroFields();
// Generate Chinese fonts for the form. The system font is used here. If it is not set, there will be problems with the Chinese display.
BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(font);
// Assembly data
Map<String, Object> data = new HashMap<>(15);
data.put("no", admissionCard.getNo());
data.put("name", admissionCard.getName());
data.put("sex", admissionCard.getSex());
data.put("idCard", admissionCard.getIdCard());
data.put("school", admissionCard.getSchool());
data.put("enterSchool", admissionCard.getEnterSchool());
data.put("examAddress", admissionCard.getExamAddress());
data.put("major", admissionCard.getMajor());
data.put("enterName", admissionCard.getEnterName());
data.put("studentImg", admissionCard.getStudentImg());
// traverse data and assign values to the pdf form
for(String key : data.keySet()){
// The pictures must be processed separately
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 = data.get(key).toString();
//Read the picture according to the path or URL
Image image = Image.getInstance(studentImage);
//Get picture page
PdfContentByte under = stamper.getOverContent(pageNo);
//The image size is adaptive
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
//Add pictures
image.setAbsolutePosition(x, y);
under.addImage(image);
}
// Set normal text data
else {
form.setField(key, data.get(key).toString());
}
}
// Indicates that the PDF cannot be modified
stamper.setFormFlattening(true);
// Close the resource
stamper.close();
// Output the stream in ByteArray byte array into out (i.e. output to the browser)
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();
log.info("*********************************************************************);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (reader != null) {
reader.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}