Javafreemarker⽣成word模板⽂件(如合同⽂件)及转pdf⽂件⽅法Java freemarker⽣成word模板⽂件(如合同⽂件)及转pdf⽂件⽅法
创建模板⽂件
ContractTemplate.docx
导⼊的Jar包
compile("junit:junit")
compile("org.springframework:spring-test")
compile("org.springframework.boot:spring-boot-test")
testCompile 'org.springframework.boot:spring-boot-starter-test'
compile 'org.freemarker:freemarker:2.3.28'
compile 'fakepath:aspose-words:19.5jdk'
compile 'fakepath:aspose-cells:8.5.2'
Java⼯具类 xml⽂档转换 Word XmlToDocx.java
st.docxml.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* xml⽂档转换 Word
*/
public class XmlToDocx {
/
**
*
* @param documentFile 动态⽣成数据的l⽂件
* @param docxTemplate docx的模板
* @param toFilePath 需要导出的⽂件路径
* @throws Exception
*/
public static void outDocx(File documentFile, String docxTemplate, String toFilePath,String key) throws Exception { try {
File docxFile = new File(docxTemplate);
ZipFile zipFile = new ZipFile(docxFile);
Enumeration<? extends ZipEntry> zipEntrys = ies();
FileOutputStream fileOutputStream = new FileOutputStream(toFilePath);
ZipOutputStream zipout = new ZipOutputStream(fileOutputStream);
int len = -1;
byte[] buffer = new byte[1024];
while (zipEntrys.hasMoreElements()) {
ZipEntry next = Element();
InputStream is = InputStream(next);
// 把输⼊流的⽂件传到输出流中如果是l由我们输⼊
zipout.putNextEntry(new String()));
if ("l".String())) {
InputStream in = new FileInputStream(documentFile);
while ((len = in.read(buffer)) != -1) {
zipout.write(buffer, 0, len);
}
in.close();
} else {
while ((len = is.read(buffer)) != -1) {
zipout.write(buffer, 0, len);
}
is.close();
}
}
zipout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java⼯具类 word⽂档转换 PDF WordToPdf.java
st.docxml.utils;
import lls.*;
import lls.License;
import com.aspose.words.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* word⽂档转换 PDF
*/
public class WordToPdf {
/**
* 获取license许可凭证
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
String licenseStr = "<License>\n"
+ " <Data>\n"
+ " <Products>\n"
+ " <Product>Aspose.Total for Java</Product>\n"
+ " <Product>Aspose.Words for Java</Product>\n"
+ " </Products>\n"
+ " <EditionType>Enterprise</EditionType>\n"
+ " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
+ " <LicenseExpiry>20991231</LicenseExpiry>\n"
+ " <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n"
+ " </Data>\n"
+ " <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuin + "</License>";
InputStream license = new Bytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* word⽂档转换为 PDF
* @param inPath 源⽂件
* @param outPath ⽬标⽂件
*/
public static File doc2pdf(String inPath, String outPath) {
//验证License,获取许可凭证
if (!getLicense()) {
return null;
}
//新建⼀个PDF⽂档
File file = new File(outPath);
try {
//新建⼀个IO输出流
FileOutputStream os = new FileOutputStream(file);
//获取将要被转化的word⽂档
Document doc = new Document(inPath);
// 全⾯⽀持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换
doc.save(os, com.aspose.words.SaveFormat.PDF);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
public static void main(String[] args) {
doc2pdf("D:/1.doc", "D:/1.pdf");
}
}
Java单元测试类 XmlDocTest.java
st.docxml;
st.docxml.utils.WordToPdf;
st.docxml.utils.XmlToDocx;
plate.Configuration;
plate.Template;svg怎么转为pdf
import org.junit.Test;
import org.junit.runner.RunWith;
import org.st.context.SpringBootTest;
import io.ClassPathResource;
import io.Resource;
import st.context.junit4.SpringJUnit4ClassRunner;
import st.context.web.WebAppConfiguration;
import java.io.File;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* 本地单元测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
//@RunWith(SpringRunner.class)
@SpringBootTest(classes= TemplateApplication.class)
@WebAppConfiguration
public class XmlDocTest {
//短租
@Test
public void testContract() throws Exception{
String contractNo = "1255445544";
String contractCorp = "银河宇宙⽆敌测试soft";
String contractDate = "2022-01-27";
String contractItem = "房地产交易中⼼";
String contractContent = "稳定发展中的⽂案1万字";
//doc xml模板⽂件
String docXml = "l"; //使⽤替换内容
//xml中间临时⽂件
String xmlTemp = "l";
//⽣成⽂件的doc⽂件
String toFilePath = contractNo + ".docx";
//模板⽂档
String docx = "ContractTemplate.docx";
/
/⽣成pdf⽂件
String toPdfFilePath = contractNo + ".pdf";;
String CONTRACT_ROOT_URL = "/template";
Resource contractNormalPath = new ClassPathResource(CONTRACT_ROOT_URL + File.separator + docXml);
String docTemplate = URI().getPath().replace(docXml, docx);
//设置⽂件编码(注意点1)
Writer writer = new PrintWriter(new File(xmlTemp),"UTF-8");
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
configuration.setEncoding(Locale.CHINESE, Charset.forName("UTF-8").name());
//设置配置(注意点3)
configuration.setDefaultEncoding("UTF-8");
String filenametest = URI().getPath().replace(docXml, "");
System.out.println("filenametest=" + filenametest);
configuration.setDirectoryForTemplateLoading(new File(filenametest));
// Template template = Template(ContractConstants.CONTRACT_NORMAL_URL+orderType+type+".xml"); //设置模板编码(注意点2)
Template template = Template(docXml,"UTF-8"); //绝对地址
Map paramsMap = new HashMap();
paramsMap.put("contractCorp",contractCorp);
paramsMap.put("contractDate",contractDate);
paramsMap.put("contractNo",contractNo);
paramsMap.put("contractItem",contractItem);
paramsMap.put("contractContent",contractContent);
template.process(paramsMap, writer);
XmlToDocx.outDocx(new File(xmlTemp), docTemplate, toFilePath, null);
System.out.println("do finish");
//转成pdf
WordToPdf.doc2pdf(toFilePath,toPdfFilePath);
}
}
创建成功之后的⽂件如下:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论