今天做了一个spring boot 导出pdf的功能 记录一下方便自己和他人
1.pom引入jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
2.写一个pdf导出服务Service
package com.example.demo.service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;public class PDFService {
public void getPDFbyte() throws IOException {
createPdf();
}
private void createPdf() throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("D://test.pdf"));
Document document = new Document(pdfDoc,new PageSize(PageSize.A4));
document.setMargins(24, 20, 0, 20);
Resource resource = new ClassPathResource("generator.properties");
Resource fontResource = new ClassPathResource("static/MSYH.ttf");
InputStream inputStream = fontResource.getInputStream();
PdfFont font = PdfFontFactory.createFont(IOUtils.toByteArray(inputStream),PdfEncodings.IDENTITY_H,false);
Resource fontResurce2 = new ClassPathResource("static/MSYHBold.ttf");
InputStream inputStream2 = fontResurce2.getInputStream();
PdfFont fontBold = PdfFontFactory.createFont(IOUtils.toByteArray(inputStream2),PdfEncodings.IDENTITY_H,false);
Cell cell;
Paragraph p;
Image image;
p = new Paragraph("麦兜响当当");
p.setTextAlignment(TextAlignment.CENTER);
p.setFont(fontBold);
p.setFontSize(12f);
document.add(p);
Table table = new Table(1);
table.setWidth(new UnitValue(UnitValue.PERCENT,100f));
//加载头像图片
try {
Resource imageResource = new ClassPathResource("static/maidou.jpg");
InputStream imageInputStream = imageResource.getInputStream();
ImageData imageData = ImageDataFactory.create(IOUtils.toByteArray(imageInputStream));
image = new Image(imageData);
image.setHorizontalAlignment(HorizontalAlignment.CENTER);
image.setMaxWidth(120f);
image.setMaxHeight(120f);
cell = new Cell();
cell.setBorder(null);
cell.add(image);
cell.setPaddingTop(16f);
cell.setPaddingBottom(16f);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("姓名:麦兜");
p.setTextAlignment(TextAlignment.LEFT);
p.setFont(fontBold);
p.setFontSize(12f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("经典台词:");
p.setTextAlignment(TextAlignment.LEFT);
p.setFont(fontBold);
p.setFontSize(12f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("1.我最喜欢吃鸡,我妈妈最喜欢吃鸡,我最喜欢和我最喜欢的妈妈一起吃妈妈跟我最喜欢的快快鸡。");
p.setTextAlignment(TextAlignment.LEFT);
p.setFont(font);
p.setFontSize(10f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("2.麦兜:啊,阿MAY呀,你看,偶默写拿了个A啊~~\r\n" +
" 阿MAY:A你个头啊,你看看,是H啊,A和H差一点点的!!\r\n" +
" 麦兜:啊,原来我差一点点就拿A!!");
p.setTextAlignment(TextAlignment.LEFT);
p.setFont(font);
p.setFontSize(10f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("3.我希望可以拉屎\r\n" +
" 老师继续唱歌\r\n" +
" 我希望我可以很快\r\n" +
" 写对那些生字\r\n" +
" 不用那块橡皮\r\n" +
" 我希望我争气\r\n" +
" 妈妈不用挤眼泪\r\n" +
" 不用有更年期\r\n" +
" 我希望得到A\r\n" +
"不会差一丁点\r\n" +
" 如果我争气\r\n" +
" 比屁股还争气\r\n" +
" 如果妈妈开心\r\n" +
" 我愿意...\r\n" +
"唔...\r\n" +
"我愿意以后再也不吃我最喜欢吃的鸡!");
p.setFont(font);
p.setFontSize(10f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("评论:");
p.setFont(fontBold);
p.setFontSize(12f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
cell = new Cell();
p = new Paragraph("麦兜他不是低能,他不过是善良");
p.setTextAlignment(TextAlignment.LEFT);
p.setFont(font);
p.setFontSize(10f);
cell.setBorder(null);
cell.add(p);
table.addCell(cell);
document.add(table);
}catch(Exception e) {
e.printStackTrace();
}
document.close();
}
}
测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExportpdfdemoApplicationTests {@Test
public void contextLoads() {
PDFService pdfServices = new PDFService();
try {
pdfServices.getPDFbyte();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
导出效果