| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package com.ruoyi.common.utils;
-
- import com.documents4j.api.DocumentType;
- import com.documents4j.api.IConverter;
- import com.documents4j.job.LocalConverter;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.PageSize;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfWriter;
- import com.itextpdf.tool.xml.XMLWorkerFontProvider;
- import com.itextpdf.tool.xml.XMLWorkerHelper;
- import com.ruoyi.common.config.RuoYiConfig;
- import com.tencentcloudapi.teo.v20220901.models.CC;
- import lombok.extern.slf4j.Slf4j;
-
- import java.io.*;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
-
- import static cn.hutool.core.util.ClassLoaderUtil.getClassLoader;
-
- /**
- * @author wangqiong
- * @description pdf转换工具类
- * @date 2023-11-28 15:20
- */
- @Slf4j
- public class PdfUtils {
- /**
- * 将html字符串转为pdf
- * @param pdfFilePath 保存的路径
- * @param htmlcontent:必须是完整的html格式,比如<html><body>123</body></html>
- */
- public static boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
- Document document = new Document();
- PdfWriter writer = null;
- try {
- writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
- // 设置底部距离60,解决重叠问题
-
- document.setPageSize(PageSize.A4);
- document.setMargins(50, 45, 50, 60);
- document.setMarginMirroring(false);
- document.open();
- // 解决PDF中文不显示
- String fontPath = "/D:/simsun.ttf"; //字体文件路径
- XMLWorkerFontProvider provider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
- provider.register(fontPath);//注册字体
- log.error("注册字体");
- XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), Charset.forName("UTF-8"), provider);
- } catch (Exception e) {
- log.error("htmlStringConvertToPDF error", e.getMessage());
- return false;
- } finally {
- if (null != document) {
- document.close();
- }
- if (null != writer) {
- writer.close();
- }
- }
- return true;
- }
- /**
- * 根据html文件生成pdf
- * @param pdfFilePath pdf文件生成路径
- * @param htmlFilePath html文件路径
- */
- public static boolean htmlFileConvertToPDF(String pdfFilePath, String htmlFilePath) {
- Document document = new Document();
- PdfWriter writer = null;
- FileOutputStream fileOutputStream = null;
- FileInputStream fileInputStream = null;
- try {
- fileOutputStream = new FileOutputStream(pdfFilePath);
- writer = PdfWriter.getInstance(document, fileOutputStream);
- // 设置底部距离60,解决重叠问题
- document.setPageSize(PageSize.A4);
- document.setMargins(50, 45, 50, 60);
- document.setMarginMirroring(false);
- document.open();
- StringBuffer sb = new StringBuffer();
- fileInputStream = new FileInputStream(htmlFilePath);
- BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
- String readStr = "";
- while ((readStr = br.readLine()) != null) {
- sb.append(readStr);
- }
- XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(sb.toString().getBytes("Utf-8")), null, Charset.forName("UTF-8"));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != document) {
- document.close();
- }
- if (null != writer) {
- writer.close();
- }
- if (null != fileInputStream) {
- try {
- fileInputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != fileOutputStream) {
- try {
- fileOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return true;
- }
-
- /**
- * docx转pdf
- * @param pdfSaveDirectory:文件保存目录,例:D:\\pdf\\
- * @param fileName:保存的文件名称,例:test.pdf
- * @param docxFile:需要转换的docx文件
- * @return
- */
- private static boolean docxConvertToPDF(String pdfSaveDirectory,String fileName,File docxFile) {
- // 不存在则新建
- File directory = new File(pdfSaveDirectory);
- if (!directory.exists()) {
- directory.mkdirs();
- }
- String pdfFilePath = pdfSaveDirectory + fileName;
- File outputFile = new File(pdfFilePath);
- try {
- InputStream docxInputStream = Files.newInputStream(docxFile.toPath());
- OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
- IConverter converter = LocalConverter.builder().build();
- converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
- docxInputStream.close();
- outputStream.close();
-
- } catch (Exception e) {
- return false;
- }
- return true;
- }
-
- }
|