springboot html转pdf
时间: 2023-10-13 07:18:03 浏览: 200
可以使用第三方库iText来实现将HTML转为PDF的功能。具体实现步骤如下:
1.在pom.xml文件中引入iText库依赖:
```xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
```
2.编写一个HTML转PDF的工具类HtmlToPdfUtil,实现将HTML文件转为PDF文件的方法:
```java
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class HtmlToPdfUtil {
//将HTML文件转为PDF文件
public static void htmlToPdf(String htmlPath, String pdfPath) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
document.open();
//将HTML文件中的内容读取出来
org.jsoup.nodes.Document doc = Jsoup.parse(new File(htmlPath), "UTF-8");
Elements elements = doc.getAllElements();
//遍历所有的元素,并将其添加到PDF文件中
for (Element element : elements) {
if (element.tagName().equalsIgnoreCase("p")) {
document.add(new Paragraph(element.text()));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
```
3.在Spring Boot的Controller中编写接口,调用HtmlToPdfUtil中的方法将HTML文件转为PDF文件后返回给前端:
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
@RestController
public class HtmlToPdfController {
@GetMapping("/html-to-pdf")
public void htmlToPdf(HttpServletResponse response) {
//获取HTML文件路径
String htmlPath = "src/main/resources/templates/index.html";
//生成PDF文件路径
String pdfPath = "src/main/resources/static/index.pdf";
//调用工具类方法将HTML文件转为PDF文件
HtmlToPdfUtil.htmlToPdf(htmlPath, pdfPath);
try {
//设置响应头
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=" + "index.pdf");
//将PDF文件传输到前端
FileInputStream fis = new FileInputStream(pdfPath);
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
fis.close();
//删除生成的PDF文件
new File(pdfPath).delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这样就完成了将HTML文件转为PDF文件并返回给前端的功能。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)