parent
f3e3f48422
commit
8b25e112e8
@ -0,0 +1,43 @@
|
||||
package cn.teammodel.utils;
|
||||
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.pdf.AcroFields;
|
||||
import com.itextpdf.text.pdf.PdfReader;
|
||||
import com.itextpdf.text.pdf.PdfStamper;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author winter
|
||||
* @create 2024-01-24 16:02
|
||||
*/
|
||||
public class PdfUtil {
|
||||
|
||||
public static void fillStudentPdfForm(Map<String, String> data, HttpServletResponse response) throws IOException, DocumentException {
|
||||
response.reset();
|
||||
response.setContentType("application/pdf");
|
||||
response.setHeader("Content-disposition",
|
||||
"attachment;filename=report_student_" + System.currentTimeMillis() + ".pdf");
|
||||
|
||||
ClassPathResource resource = new ClassPathResource("templates/pdf_templates/template.pdf");
|
||||
InputStream in = resource.getInputStream();
|
||||
ServletOutputStream os = response.getOutputStream();
|
||||
fillPdfForm(in, os, data);
|
||||
}
|
||||
public static void fillPdfForm(InputStream in, OutputStream os, Map<String, String> data) throws IOException, DocumentException {
|
||||
PdfReader pdfReader = new PdfReader(in);
|
||||
PdfStamper stamper = new PdfStamper(pdfReader, os);
|
||||
AcroFields fields = stamper.getAcroFields();
|
||||
for (Map.Entry<String, String> entry : data.entrySet()) {
|
||||
fields.setField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
stamper.close();
|
||||
os.close();
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue