1.获取html模板
java
public static void main(String[] args) {
String htmlContent = getHtmlFileContent(templateName,dataMap);
String exportType = "pdf";
if (exportType.equals("pdf")){
convertToPdf(htmlContent,filePath);
}else {
exportWord(htmlContent, filePath);
}
}
public static String getHtmlFileContent(String templateName, Map<String, Object> contentMap) {
String htmlText = null;
try {
Configuration configuration = new Configuration();
TemplateLoader templateLoader = new ClassTemplateLoader(PdfUtil.class, "/static/html/");
configuration.setTemplateLoader(templateLoader);
Template tpl = configuration.getTemplate(templateName);
//解析模板并替换动态数据,最终content将替换模板文件中的${content}标签。
htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, contentMap);
} catch (Exception e) {
e.printStackTrace();
}
return htmlText;
}
保存文件为word
java
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.text.pdf.BaseFont;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* 保存文件为word
*/
public static void exportWord(String content, String filePath) throws Exception {
byte[] b = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //该步骤不可省略,否则会出现乱码。
//输出文件
File file = new File(filePath);
FileOutputStream ostream = new FileOutputStream(file);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
poifs.close();
}
保存为pdf文档
java
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.text.pdf.BaseFont;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public static void convertToPdf(String html,String pdfPath){
// 中文和英文的格式
//String pdfPath = "D:\\cumentPdf.pdf";
File file = new File(pdfPath);
try(OutputStream outputStream = new FileOutputStream(file)) {
// 转换 HTML to PDF
PdfWriter writer = new PdfWriter(outputStream);
PdfDocument pdfDocument = new PdfDocument(writer);
// 设置PDF大小
pdfDocument.setDefaultPageSize(PageSize.A3);
// 设置中文
ConverterProperties converterProperties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider(true, true, true);
converterProperties.setFontProvider(fontProvider);
// html转换PDF
HtmlConverter.convertToPdf(html, pdfDocument, converterProperties);
// 关闭
pdfDocument.close();
} catch (Exception e){
e.fillInStackTrace();
}
}
2. pdf 转 excel
下载jar地址:https://mvnrepository.com/artifact/e-iceblue/spire.pdf/9.7.0
java
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>9.7.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/spire.pdf-9.7.0.jar</systemPath>
</dependency>
java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
public static void pdfToExcel(String pdfPath,String excelPath){
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.loadFromFile(pdfPath);
//保存为Excel
pdf.saveToFile(excelPath, FileFormat.XLSX);
}
使用excel生成html模板
java
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/spire.xls.free.5.1.0.jar</systemPath>
</dependency>
package com.suninfo.analyse.test;
import org.springframework.web.bind.annotation.*;
import com.spire.xls.*;
public class ExcelToPdfOrHtml {
public static void main(String[] args) {
//读取excel文件地址
String fileUrl = "D:\\CA_HOME\\upload\\杭州-2023-06-28 16-38-45.xlsx";
//要转换的sheet页名称
String sheetName = "Sheet1";
Workbook wb = new Workbook();
wb.loadFromFile(fileUrl);
if(wb==null){
throw new RuntimeException("文件不存在,请检查文件路径");
}
Worksheet worksheet = wb.getWorksheets().get(sheetName);
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPaperSize(PaperSizeType.PaperA4);
pageSetup.setPrintArea("A1:J21");
pageSetup.setZoom(80);
if(worksheet==null){
throw new RuntimeException("工作簿中不存在"+sheetName+"工作表,请检查");
}
//pdf存放路径
String url = "D:\\CA_HOME\\upload\\excelToPdf1.pdf";
//saveToPdf 如果该路径原本存在,直接生成文件,如果路径不存在,自动创建文件夹生成该路径
// worksheet.saveToPdf(url);
worksheet.saveToHtml( "D:\\CA_HOME\\upload\\orgHtmlTemplate.html");
}
}