|
|
@ -7,9 +7,12 @@ import com.itextpdf.text.Paragraph;
|
|
|
|
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
|
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
|
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
|
|
|
|
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
|
|
|
|
import com.itextpdf.tool.xml.XMLWorkerHelper;
|
|
|
|
import com.itextpdf.tool.xml.XMLWorkerHelper;
|
|
|
|
|
|
|
|
import com.ruoyi.common.utils.http.HttpUtils;
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
@ -29,6 +32,7 @@ import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
public class HtmlToPdfConverter {
|
|
|
|
public class HtmlToPdfConverter {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
|
|
|
|
|
|
|
|
|
|
|
public static MultipartFile convertHtmlToPdf(String htmlArticleTitle, String htmlContent) throws IOException {
|
|
|
|
public static MultipartFile convertHtmlToPdf(String htmlArticleTitle, String htmlContent) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
@ -41,8 +45,9 @@ public class HtmlToPdfConverter {
|
|
|
|
String[] split = format.split("-");
|
|
|
|
String[] split = format.split("-");
|
|
|
|
Path dirPath = Paths.get(path, "temporary", split[0], split[1], split[2]);
|
|
|
|
Path dirPath = Paths.get(path, "temporary", split[0], split[1], split[2]);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// clearTemporaryDirectory(Paths.get(path, "temporary")); // 清理目录
|
|
|
|
Files.createDirectories(dirPath);
|
|
|
|
Files.createDirectories(dirPath);
|
|
|
|
clearTemporaryDirectory(Paths.get(path, "temporary")); // 清理目录
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("创建文件夹失败: " + e.getMessage(), e);
|
|
|
|
throw new RuntimeException("创建文件夹失败: " + e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -111,8 +116,14 @@ public class HtmlToPdfConverter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MultipartFile multipartFile = getMultipartFile(pdfFile);
|
|
|
|
|
|
|
|
// 在此处删除创建的 PDF 文件
|
|
|
|
|
|
|
|
boolean deleted = pdfFile.delete();
|
|
|
|
|
|
|
|
if (!deleted) {
|
|
|
|
|
|
|
|
log.error("删除文件失败: " + pdfFile.getAbsolutePath());
|
|
|
|
|
|
|
|
|
|
|
|
return getMultipartFile(pdfFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return multipartFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static MultipartFile getMultipartFile(File file) throws IOException {
|
|
|
|
private static MultipartFile getMultipartFile(File file) throws IOException {
|
|
|
|