package appapi.controller.pdfExport;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.kafka.common.config.ConfigDef;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
@RestController
@RequestMapping(“/api”)
@Api(tags = “导出pdf”)
public class DownLoadPDF {
@GetMapping(“/downloadPdf”)
@ApiOperation(value = “获取PDF”)
public ResponseEntity downloadPdf() {
ByteArrayInputStream byteArrayInputStream = createPdf();
HttpHeaders headers = new HttpHeaders();
headers.add(“Content-Disposition”, “inline; filename=test.pdf”);
return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_PDF)
.body(new InputStreamResource(byteArrayInputStream));
}
private ByteArrayInputStream createPdf() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Document document = new Document();
try {
PdfWriter.getInstance(document, out);
BaseFont bfChinese = BaseFont.createFont("E:\\zzw\\zj\\src\\main\\resources/仿宋_GB2312.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font title = new Font(bfChinese, 16, Font.BOLD);
Font titles = new Font(bfChinese, 30, Font.BOLD, BaseColor.RED);
Font title2 = new Font(bfChinese, 12, Font.BOLD);
Font textFont = new Font(bfChinese, 12, Font.NORMAL);
Font dateFont = new Font(bfChinese, 12, Font.ITALIC);
document.open();
Paragraph imageParagraph = new Paragraph();
imageParagraph.setAlignment(Element.ALIGN_CENTER);
Image image = Image.getInstance("E:\\zzw\\zj\\src\\main\\resources/logo.png");
image.scaleToFit(200, 200);
imageParagraph.add(image);
document.add(imageParagraph);
Paragraph dateTitles = new Paragraph("生产性装备管理月报", titles);
dateTitles.setAlignment(Element.ALIGN_CENTER);
document.add(dateTitles);
Paragraph dateTitle3 = new Paragraph(" ", dateFont);
dateTitle3.setAlignment(Element.ALIGN_CENTER);
document.add(dateTitle3);
Paragraph dateTitle = new Paragraph("(2023年 月)", dateFont);
dateTitle.setAlignment(Element.ALIGN_CENTER);
document.add(dateTitle);
Paragraph department = new Paragraph("生产运营管理部 2023年1月31 日", textFont);
department.setAlignment(Element.ALIGN_CENTER);
document.add(department);
Paragraph xhxParagraph = new Paragraph();
xhxParagraph.setAlignment(Element.ALIGN_CENTER);
Image xhxImage = Image.getInstance("E:\\zzw\\zj\\src\\main\\resources/xhx.png");
xhxImage.scaleToFit(450, 600);
xhxParagraph.add(xhxImage);
document.add(xhxParagraph);
Paragraph chapterOneTitle = new Paragraph("一、生产性装备保有情况", title);
chapterOneTitle.setAlignment(Element.ALIGN_LEFT);
document.add(chapterOneTitle);
Paragraph sectionOneTitle = new Paragraph("1.总体情况", title2);
sectionOneTitle.setSpacingBefore(10);
sectionOneTitle.setIndentationLeft(20);
document.add(sectionOneTitle);
Paragraph sectionOneContent = new Paragraph(" 截至 月,公司(项目)保有生产性装备共计 艘 (台套) ,进场" +
"\n 台,退场 台,验收 台,验收率 %;其中:\n" + "自有装备合计 台/套;\n" + "外租装备合计 台/套;" +
"\n" + "队伍自带装备合计 台/套;\n" + "公司各单位生产性装备保有情况详见设备台账。", textFont);
sectionOneContent.setIndentationLeft(40);
document.add(sectionOneContent);
Paragraph sectionTwoTitle = new Paragraph("2.自有装备情况", title2);
sectionTwoTitle.setSpacingBefore(10);
sectionTwoTitle.setIndentationLeft(20);
document.add(sectionTwoTitle);
// 添加自有装备情况内容,根据实际情况填写
Paragraph sectionFourContent = new Paragraph(" 截至 月,公司自有装备共计 台套 ,启用 台,停用 台," +
"\n" + "其中:\n" + "塔式起重机:共计 台,在用 台,闲置 台,利用率 %;\n" + "施工升降机:共计 台,在用 台,闲置 台,利用率 %;" +
"\n" + "门式起重机:共计 台,在用 台,闲置 台,利用率 %;\n" + "架 桥 机:共计 台,在用 台,闲置 台,利用率 %;" +
"\n" + "挂 篮:共计 台,在用 台,闲置 台,利用率 %;\n" + "其 它装 备:共计 台,在用 台,闲置 台,利用率 %;" +
"\n" + "公司各单位自有装备保有情况详见设备台账。", textFont);
sectionFourContent.setIndentationLeft(40);
document.add(sectionFourContent);
Paragraph sectionThreeTitle = new Paragraph("3.外租装备情况", title2);
sectionThreeTitle.setSpacingBefore(10);
sectionThreeTitle.setIndentationLeft(20);
document.add(sectionThreeTitle);
// 添加外租装备情况内容,根据实际情况填写
Paragraph sectionThreeContent = new Paragraph(" 截至 月,公司外租装备共计 台套 ,进场 台,退场 台\n" + ",验收 台,验收率 %;其中:\n" + "塔式起重机:共计 台,在用 台,闲置 台,利用率 %;\n" + "施工升降机:共计 台,在用 台,闲置 台,利用率 %;\n" + "门式起重机:共计 台,在用 台,闲置 台,利用率 %;\n" + "架 桥 机:共计 台,在用 台,闲置 台,利用率 %;\n" + "挂 篮:共计 台,在用 台,闲置 台,利用率 %;\n" + "其 它装 备:共计 台,在用 台,闲置 台,利用率 %;", textFont);
sectionThreeContent.setIndentationLeft(40);
document.add(sectionThreeContent);
Paragraph sectionFourTitle = new Paragraph("4.队伍自带装备情况", title2);
sectionFourTitle.setSpacingBefore(10);
sectionFourTitle.setIndentationLeft(20);
document.add(sectionFourTitle);
// 添加队伍自带装备情况内容,根据实际情况填写
Paragraph sectionFourContent1 = new Paragraph(" 截至 月,队伍自带装备共计 台套 ,进场 台,退场 台,\n" + "验收 台,验收率 %;其中:\n" + "塔式起重机:共计 台,在用 台,闲置 台,利用率 %;\n" + "施工升降机:共计 台,在用 台,闲置 台,利用率 %;\n" + "门式起重机:共计 台,在用 台,闲置 台,利用率 %;\n" + "架 桥 机:共计 台,在用 台,闲置 台,利用率 %;\n" + "挂 篮:共计 台,在用 台,闲置 台,利用率 %;\n" + "其 它装 备:共计 台,在用 台,闲置 台,利用率 %;", textFont);
sectionFourContent1.setIndentationLeft(40);
document.add(sectionFourContent1);
Paragraph chapterTwoTitle = new Paragraph("二、监控系统应用情况", title);
chapterTwoTitle.setAlignment(Element.ALIGN_LEFT);
chapterTwoTitle.setSpacingBefore(20);
document.add(chapterTwoTitle);
// 添加监控系统应用情况内容,根据实际情况填写
Paragraph chapterTwoContent = new Paragraph(" 截止 月,监控系统共安装 套,本月安装 套," +
"\n 本月退场 套,本月一般告警 次,严重告警 次,处理\n告警 次,处理率 %,具体情况如下:\n" + "(公司或项目名称),共安装 套,本月一般告警 次,严重\n告警 次,处理告警 次,处理率 %;\n" + "(公司或项目名称),共安装 套,本月一般告警 次,严重\n告警 次,处理告警 次,处理率 %;", textFont);
chapterTwoContent.setIndentationLeft(20);
document.add(chapterTwoContent);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new ByteArrayInputStream(out.toByteArray());
}
}
该代码示例展示了如何在JavaSpringBoot应用中创建一个RESTAPI,该API用于下载由iTextPDF库生成的PDF文件。内容包括设置页面布局,添加图片,设置文本样式以及展示不同类型的装备统计信息,如保有量、利用率等。
634

被折叠的 条评论
为什么被折叠?



