Commit ef24d374 authored by 徐俊's avatar 徐俊

xujun

parent 82a96eb9
...@@ -248,6 +248,9 @@ public class ComProjectDTO extends BaseDTO { ...@@ -248,6 +248,9 @@ public class ComProjectDTO extends BaseDTO {
/** URL */ /** URL */
@ApiModelProperty(value = "URL", position = 7) @ApiModelProperty(value = "URL", position = 7)
private String downloadUrl; private String downloadUrl;
/** 转换文件URL */
@ApiModelProperty(value = "转换文件URL", position = 7)
private String convertUrl;
/** 单位 */ /** 单位 */
@ApiModelProperty(value = "单位", position = 41) @ApiModelProperty(value = "单位", position = 41)
......
...@@ -13,6 +13,8 @@ import java.util.Objects; ...@@ -13,6 +13,8 @@ import java.util.Objects;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ProjectInfoToPDF { public class ProjectInfoToPDF {
...@@ -75,6 +77,11 @@ public class ProjectInfoToPDF { ...@@ -75,6 +77,11 @@ public class ProjectInfoToPDF {
// 项目经费预算表 // 项目经费预算表
addProjectBudgetTable(document, project, bfChinese, fsChinese); addProjectBudgetTable(document, project, bfChinese, fsChinese);
// 插入申请书正文PDF
if (project.getConvertUrl() != null && !project.getConvertUrl().trim().isEmpty()) {
insertPdfContent(document, System.getProperty("user.dir") + project.getConvertUrl(), writer);
}
// 添加新页面 // 添加新页面
document.newPage(); document.newPage();
...@@ -936,4 +943,67 @@ public class ProjectInfoToPDF { ...@@ -936,4 +943,67 @@ public class ProjectInfoToPDF {
} }
} }
} }
/**
* 插入PDF文件到指定位置
* @param document 目标文档
* @param pdfPath 要插入的PDF文件路径
* @throws DocumentException
* @throws IOException
*/
private static void insertPdfContent(Document document, String pdfPath, PdfWriter writer) throws DocumentException, IOException {
PdfReader reader = null;
try {
// 添加标题
Font titleFont = new Font(BaseFont.createFont(), 12, Font.BOLD);
Paragraph title = new Paragraph("申请书正文", titleFont);
title.setAlignment(Element.ALIGN_CENTER);
title.setSpacingAfter(20f);
document.add(title);
// 读取PDF文件内容
byte[] pdfBytes = readPdfToBytes(pdfPath);
// 创建PdfReader
reader = new PdfReader(pdfBytes);
// 获取PDF页数
int totalPages = reader.getNumberOfPages();
// 复制每一页
for (int i = 1; i <= totalPages; i++) {
document.newPage();
PdfImportedPage page = writer.getImportedPage(reader, i);
PdfContentByte cb = writer.getDirectContent();
cb.addTemplate(page, 0, 0);
}
} catch (Exception e) {
System.out.println("插入PDF文件失败: " + e.getMessage());
e.printStackTrace();
throw e; // 重新抛出异常以确保错误被正确处理
}
// 不在这里关闭reader,让它在document关闭时自动关闭
}
/**
* 读取PDF文件并返回字节数组
* @param pdfPath PDF文件路径
* @return 字节数组
*/
private static byte[] readPdfToBytes(String pdfPath) throws IOException {
File file = new File(pdfPath);
if (!file.exists()) {
throw new FileNotFoundException("PDF文件不存在: " + pdfPath);
}
try (FileInputStream fis = new FileInputStream(file)) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
return bos.toByteArray();
}
}
} }
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
ic.total_funding,ic.gov_funding,ic.unit_funding,ic.self_funding,ic.other_funding, ic.total_funding,ic.gov_funding,ic.unit_funding,ic.self_funding,ic.other_funding,
ic.research_content,ic.technology_target, ic.economy_target, ic.achievement_target, ic.technology_reports_target, ic.other_target, ic.research_content,ic.technology_target, ic.economy_target, ic.achievement_target, ic.technology_reports_target, ic.other_target,
ic.proj_attribute,ic.remark,ic.year_target,ic.year1_goal,ic.year2_goal,ic.year3_goal, ic.proj_attribute,ic.remark,ic.year_target,ic.year1_goal,ic.year2_goal,ic.year3_goal,
c.id download_id,c.download_url,c.file_name c.id download_id,c.download_url,c.convert_url,c.file_name
from com_project a from com_project a
left join com_project_basic ic on a.id=ic.proj_id left join com_project_basic ic on a.id=ic.proj_id
left join system_parameter b on a.knowledge_id=b.id and b.type_id=68 left join system_parameter b on a.knowledge_id=b.id and b.type_id=68
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment