| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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.PageSize;
- import com.itextpdf.text.pdf.PdfWriter;
- import com.itextpdf.tool.xml.XMLWorkerHelper;
-
- import java.io.*;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
-
- /**
- * @author wangqiong
- * @description pdf转换工具类
- * @date 2023-11-28 15:20
- */
- 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 {
- if(!new File(pdfFilePath).exists()){
- new File(pdfFilePath).createNewFile();
- }
- writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
- // 设置底部距离60,解决重叠问题
- document.setPageSize(PageSize.A4);
- document.setMargins(50, 45, 50, 60);
- document.setMarginMirroring(false);
- document.open();
- XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), null, Charset.forName("UTF-8"));
- } catch (Exception e) {
- 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;
- }
-
- public static void main(String[] args) {
- try {
-
- String pdfFile = "D:\\test2.pdf";
- String htmlContent1 = "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2>";
- String htmlContent = "<html><body style=\"font-size:12.0pt; font-family:宋体\">" + "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
- String htmlContent2 = "<html><body >" + "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
-
- htmlStringConvertToPDF(pdfFile,htmlContent);
- // parseHtml2PdfByFilePath(pdfFile,htmlFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|