调用工具:PDF生成

This commit is contained in:
huangge1199 2025-05-28 16:44:26 +08:00
parent 74a610733f
commit 4856d11590
5 changed files with 90 additions and 1 deletions

15
pom.xml
View File

@ -126,6 +126,21 @@
<artifactId>jsoup</artifactId>
<version>1.20.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-core</artifactId>
<version>9.1.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/font-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>9.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>

View File

@ -24,4 +24,6 @@ public interface ToolsService {
String terminalTool(String command);
void downloadTool(String url, String name);
void pdfTool(String name, String context);
}

View File

@ -9,7 +9,6 @@ import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
@ -80,4 +79,10 @@ public class ToolsServiceImpl implements ToolsService {
DownloadTool downloadTool = new DownloadTool();
downloadTool.downloadResource(url, name);
}
@Override
public void pdfTool(String name, String context) {
PDFGenerationTool pdfTool = new PDFGenerationTool();
pdfTool.generatePDF(name, context);
}
}

View File

@ -97,4 +97,15 @@ public class ToolController {
toolsService.downloadTool(url, name);
return R.ok();
}
@PostMapping("/pdfTool")
@Operation(summary = "PDF生成")
public R<?> pdfTool(@RequestBody JSONObject params) {
String context = params.getStr("context");
String name = params.getStr("name");
CheckUtils.checkEmpty(context, "内容");
CheckUtils.checkEmpty(name, "文件名");
toolsService.pdfTool(name, context);
return R.ok();
}
}

View File

@ -0,0 +1,56 @@
package com.huangge1199.aiagent.tools;
import cn.hutool.core.io.FileUtil;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.io.IOException;
/**
* PDFTool
*
* @author huangge1199
* @since 2025/5/28 16:23:46
*/
public class PDFGenerationTool {
@Tool(description = "Generate a PDF file with given content")
public String generatePDF(
@ToolParam(description = "Name of the file to save the generated PDF") String fileName,
@ToolParam(description = "Content to be included in the PDF") String content) {
String fileDir = FileConstant.FILE_SAVE_DIR + "/pdf";
String filePath = fileDir + "/" + fileName;
try {
// 创建目录
FileUtil.mkdir(fileDir);
// 创建 PdfWriter PdfDocument 对象
try (PdfWriter writer = new PdfWriter(filePath);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf)) {
// 自定义字体需要人工下载字体文件到特定目录
// String fontPath = Paths.get("src/main/resources/static/fonts/simsun.ttf")
// .toAbsolutePath().toString();
// PdfFont font = PdfFontFactory.createFont(fontPath,
// PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
// 使用内置中文字体
// PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H");
// document.setFont(font);
// 创建段落
Paragraph paragraph = new Paragraph(content);
// 添加段落并关闭文档
document.add(paragraph);
}
return "PDF generated successfully to: " + filePath;
} catch (IOException e) {
return "Error generating PDF: " + e.getMessage();
}
}
}