1.背景

在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等。方便用户查看,下载,打印。目前常用的解决方案是,把相关数据信息,生成对应的pdf文件返回给用户。

本文源码:http://git.oschina.net/lujianing/java_pdf_demo

2.iText

iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

iText 官网:http://itextpdf.com/

iText 开发文档: http://developers.itextpdf.com/developers-home

iText目前有两套版本iText5和iText7。iText5应该是网上用的比较多的一个版本。iText5因为是很多开发者参与贡献代码, 因此在一些规范和设计上存在不合理的地方。iText7是后来官方针对iText5的重构,两个版本差别还是挺大的。不过在实际使用中,一般用到的都比较 简单,所以不用特别拘泥于使用哪个版本。比如我们在http://mvnrepository.com/中搜索iText,出来的都是iText5的依赖。

来个最简单的例子:

添加依赖:


  1. <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
  2. <dependency>
  3. <groupId>com.itextpdf</groupId>
  4. <artifactId>itextpdf</artifactId>
  5. <version>5.5.11</version>
  6. </dependency>

测试代码:JavaToPdf

  1. package com.lujianing.test;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Paragraph;
  5. import com.itextpdf.text.pdf.PdfWriter;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. /**
  9. * Created by lujianing on 2017/5/7.
  10. */
  11. public class JavaToPdf {
  12. private static final String DEST = "target/HelloWorld.pdf";
  13. public static void main(String[] args) throws FileNotFoundException, DocumentException {
  14. Document document = new Document();
  15. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
  16. document.open();
  17. document.add(new Paragraph("hello world"));
  18. document.close();
  19. writer.close();
  20. }
  21. }

运行结果:

3.iText-中文支持

iText默认是不支持中文的,因此需要添加对应的中文字体,比如黑体simhei.ttf

可参考文档:http://developers.itextpdf.com/examples/font-examples/using-fonts#1227-tengwarquenya1.java

测试代码:JavaToPdfCN

  1. package com.lujianing.test;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Font;
  5. import com.itextpdf.text.FontFactory;
  6. import com.itextpdf.text.Paragraph;
  7. import com.itextpdf.text.pdf.BaseFont;
  8. import com.itextpdf.text.pdf.PdfWriter;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. /**
  12. * Created by lujianing on 2017/5/7.
  13. */
  14. public class JavaToPdfCN {
  15. private static final String DEST = "target/HelloWorld_CN.pdf";
  16. private static final String FONT = "simhei.ttf";
  17. public static void main(String[] args) throws FileNotFoundException, DocumentException {
  18. Document document = new Document();
  19. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
  20. document.open();
  21. Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  22. document.add(new Paragraph("hello world,我是鲁家宁", f1));
  23. document.close();
  24. writer.close();
  25. }
  26. }

输出结果:

4.iText-Html渲染

在一些比较复杂的pdf布局中,我们可以通过html去生成pdf

可参考文档:http://developers.itextpdf.com/examples/xml-worker-itext5/xml-worker-examples

添加依赖:

  1. <!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
  2. <dependency>
  3. <groupId>com.itextpdf.tool</groupId>
  4. <artifactId>xmlworker</artifactId>
  5. <version>5.5.11</version>
  6. </dependency>

添加模板:template.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>Title</title>
  6. <style>
  7. body{
  8. font-family:SimHei;
  9. }
  10. .red{
  11. color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="red">
  17. 你好,鲁家宁
  18. </div>
  19. </body>
  20. </html>

测试代码:JavaToPdfHtml

  1. package com.lujianing.test;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.pdf.PdfWriter;
  5. import com.itextpdf.tool.xml.XMLWorkerFontProvider;
  6. import com.itextpdf.tool.xml.XMLWorkerHelper;
  7. import com.lujianing.test.util.PathUtil;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.nio.charset.Charset;
  12. /**
  13. * Created by lujianing on 2017/5/7.
  14. */
  15. public class JavaToPdfHtml {
  16. private static final String DEST = "target/HelloWorld_CN_HTML.pdf";
  17. private static final String HTML = PathUtil.getCurrentPath()+"/template.html";
  18. private static final String FONT = "simhei.ttf";
  19. public static void main(String[] args) throws IOException, DocumentException {
  20. // step 1
  21. Document document = new Document();
  22. // step 2
  23. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
  24. // step 3
  25. document.open();
  26. // step 4
  27. XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
  28. fontImp.register(FONT);
  29. XMLWorkerHelper.getInstance().parseXHtml(writer, document,
  30. new FileInputStream(HTML), null, Charset.forName("UTF-8"), fontImp);
  31. // step 5
  32. document.close();
  33. }
  34. }

输出结果:

需要注意:

1.html中必须使用标准的语法,标签一定需要闭合

2.html中如果有中文,需要在样式中添加对应字体的样式

5.iText-Html-Freemarker渲染

在实际使用中,html内容都是动态渲染的,因此我们需要加入模板引擎支持,可以使用FreeMarker/Velocity,这里使用FreeMarker举例

添加FreeMarke依赖:

  1. <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
  2. <dependency>
  3. <groupId>org.freemarker</groupId>
  4. <artifactId>freemarker</artifactId>
  5. <version>2.3.19</version>
  6. </dependency>

添加模板:template_freemarker.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>Title</title>
  6. <style>
  7. body{
  8. font-family:SimHei;
  9. }
  10. .blue{
  11. color: blue;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="blue">
  17. 你好,${name}
  18. </div>
  19. </body>
  20. </html>

测试代码:JavaToPdfHtmlFreeMarker

  1. package com.lujianing.test;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.pdf.PdfWriter;
  5. import com.itextpdf.tool.xml.XMLWorkerFontProvider;
  6. import com.itextpdf.tool.xml.XMLWorkerHelper;
  7. import com.lujianing.test.util.PathUtil;
  8. import freemarker.template.Configuration;
  9. import freemarker.template.Template;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.StringWriter;
  15. import java.io.Writer;
  16. import java.nio.charset.Charset;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. /**
  20. * Created by lujianing on 2017/5/7.
  21. */
  22. public class JavaToPdfHtmlFreeMarker {
  23. private static final String DEST = "target/HelloWorld_CN_HTML_FREEMARKER.pdf";
  24. private static final String HTML = "template_freemarker.html";
  25. private static final String FONT = "simhei.ttf";
  26. private static Configuration freemarkerCfg = null;
  27. static {
  28. freemarkerCfg =new Configuration();
  29. //freemarker的模板目录
  30. try {
  31. freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. public static void main(String[] args) throws IOException, DocumentException {
  37. Map<String,Object> data = new HashMap();
  38. data.put("name","鲁家宁");
  39. String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(data,HTML);
  40. JavaToPdfHtmlFreeMarker.createPdf(content,DEST);
  41. }
  42. public static void createPdf(String content,String dest) throws IOException, DocumentException {
  43. // step 1
  44. Document document = new Document();
  45. // step 2
  46. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  47. // step 3
  48. document.open();
  49. // step 4
  50. XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
  51. fontImp.register(FONT);
  52. XMLWorkerHelper.getInstance().parseXHtml(writer, document,
  53. new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"), fontImp);
  54. // step 5
  55. document.close();
  56. }
  57. /**
  58. * freemarker渲染html
  59. */
  60. public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
  61. Writer out = new StringWriter();
  62. try {
  63. // 获取模板,并设置编码方式
  64. Template template = freemarkerCfg.getTemplate(htmlTmp);
  65. template.setEncoding("UTF-8");
  66. // 合并数据模型与模板
  67. template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
  68. out.flush();
  69. return out.toString();
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. } finally {
  73. try {
  74. out.close();
  75. } catch (IOException ex) {
  76. ex.printStackTrace();
  77. }
  78. }
  79. return null;
  80. }
  81. }

输出结果:

目前为止,我们已经实现了iText通过Html模板生成Pdf的功能,但是实际应用中,我们发现iText并不能对高级的CSS样式进行解析,比如CSS中的position属性等,因此我们要引入新的组件

若中文变量还是不显示,则在pom.xml里添加如下:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  <!--增加的配置,过滤ttf文件的匹配-->
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.7</version>
      <configuration>
      <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
      </configuration>
    </plugin>
  </plugins>
</build>

备注:工具类的方法 PathUtil.getCurrentPath() = "src/main/resources/",(这个对新手有用)

6.Flying Saucer-CSS高级特性支持

Flying Saucer is a pure-Java library for rendering arbitrary
well-formed XML (or XHTML) using CSS 2.1 for layout and formatting,
output to Swing panels, PDF, and images.

Flying Saucer是基于iText的,支持对CSS高级特性的解析。

添加依赖:

  1. <!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf -->
  2. <dependency>
  3. <groupId>org.xhtmlrenderer</groupId>
  4. <artifactId>flying-saucer-pdf</artifactId>
  5. <version>9.1.5</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->
  8. <dependency>
  9. <groupId>org.xhtmlrenderer</groupId>
  10. <artifactId>flying-saucer-pdf-itext5</artifactId>
  11. <version>9.1.5</version>
  12. </dependency>

添加模板:template_freemarker_fs.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>Title</title>
  6. <style>
  7. body{
  8. font-family:SimHei;
  9. }
  10. .color{
  11. color: green;
  12. }
  13. .pos{
  14. position:absolute;
  15. left:200px;
  16. top:5px;
  17. width: 200px;
  18. font-size: 10px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <img src="logo.png" width="600px"/>
  24. <div class="color pos">
  25. 你好,${name}
  26. </div>
  27. </body>
  28. </html>

测试代码:JavaToPdfHtmlFreeMarker

  1. package com.lujianing.test.flyingsaucer;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.pdf.BaseFont;
  4. import com.lujianing.test.util.PathUtil;
  5. import freemarker.template.Configuration;
  6. import freemarker.template.Template;
  7. import org.xhtmlrenderer.pdf.ITextFontResolver;
  8. import org.xhtmlrenderer.pdf.ITextRenderer;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.StringWriter;
  13. import java.io.Writer;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * Created by lujianing on 2017/5/7.
  18. */
  19. public class JavaToPdfHtmlFreeMarker {
  20. private static final String DEST = "target/HelloWorld_CN_HTML_FREEMARKER_FS.pdf";
  21. private static final String HTML = "template_freemarker_fs.html";
  22. private static final String FONT = "simhei.ttf";
  23. private static final String LOGO_PATH = "file://"+PathUtil.getCurrentPath()+"/logo.png";
  24. private static Configuration freemarkerCfg = null;
  25. static {
  26. freemarkerCfg =new Configuration();
  27. //freemarker的模板目录
  28. try {
  29. freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
  35. Map<String,Object> data = new HashMap();
  36. data.put("name","鲁家宁");
  37. String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(data,HTML);
  38. JavaToPdfHtmlFreeMarker.createPdf(content,DEST);
  39. }
  40. /**
  41. * freemarker渲染html
  42. */
  43. public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
  44. Writer out = new StringWriter();
  45. try {
  46. // 获取模板,并设置编码方式
  47. Template template = freemarkerCfg.getTemplate(htmlTmp);
  48. template.setEncoding("UTF-8");
  49. // 合并数据模型与模板
  50. template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
  51. out.flush();
  52. return out.toString();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. } finally {
  56. try {
  57. out.close();
  58. } catch (IOException ex) {
  59. ex.printStackTrace();
  60. }
  61. }
  62. return null;
  63. }
  64. public static void createPdf(String content,String dest) throws IOException, DocumentException, com.lowagie.text.DocumentException {
  65. ITextRenderer render = new ITextRenderer();
  66. ITextFontResolver fontResolver = render.getFontResolver();
  67. fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  68. // 解析html生成pdf
  69. render.setDocumentFromString(content);
  70. //解决图片相对路径的问题
  71. render.getSharedContext().setBaseURL(LOGO_PATH);
  72. render.layout();
  73. render.createPDF(new FileOutputStream(dest));
  74. }
  75. }

输出结果:

在某些场景下,html中的静态资源是在本地,我们可以使用render.getSharedContext().setBaseURL()加载文件资源,注意资源URL需要使用文件协议 "file://"。

对于生成的pdf页面大小,可以用css的@page属性设置。

7.PDF转图片

在某些场景中,我们可能只需要返回图片格式的电子凭证,我们可以使用Jpedal组件,把pdf转成图片

添加依赖:

  1. <!-- https://mvnrepository.com/artifact/org.jpedal/jpedal-lgpl -->
  2. <dependency>
  3. <groupId>org.jpedal</groupId>
  4. <artifactId>jpedal-lgpl</artifactId>
  5. <version>4.74b27</version>
  6. </dependency>

测试代码:JavaToPdfImgHtmlFreeMarker

  1. package com.lujianing.test.flyingsaucer;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.pdf.BaseFont;
  4. import com.lujianing.test.util.PathUtil;
  5. import freemarker.template.Configuration;
  6. import freemarker.template.Template;
  7. import org.jpedal.PdfDecoder;
  8. import org.jpedal.exception.PdfException;
  9. import org.jpedal.fonts.FontMappings;
  10. import org.xhtmlrenderer.pdf.ITextFontResolver;
  11. import org.xhtmlrenderer.pdf.ITextRenderer;
  12. import java.awt.image.BufferedImage;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.StringWriter;
  18. import java.io.Writer;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import javax.imageio.ImageIO;
  22. /**
  23. * Created by lujianing on 2017/5/7.
  24. */
  25. public class JavaToPdfImgHtmlFreeMarker {
  26. private static final String DEST = "target/HelloWorld_CN_HTML_FREEMARKER_FS_IMG.png";
  27. private static final String HTML = "template_freemarker_fs.html";
  28. private static final String FONT = "simhei.ttf";
  29. private static final String LOGO_PATH = "file://"+PathUtil.getCurrentPath()+"/logo.png";
  30. private static final String IMG_EXT = "png";
  31. private static Configuration freemarkerCfg = null;
  32. static {
  33. freemarkerCfg =new Configuration();
  34. //freemarker的模板目录
  35. try {
  36. freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
  42. Map<String,Object> data = new HashMap();
  43. data.put("name","鲁家宁");
  44. String content = JavaToPdfImgHtmlFreeMarker.freeMarkerRender(data,HTML);
  45. ByteArrayOutputStream pdfStream = JavaToPdfImgHtmlFreeMarker.createPdf(content);
  46. ByteArrayOutputStream imgSteam = JavaToPdfImgHtmlFreeMarker.pdfToImg(pdfStream.toByteArray(),2,1,IMG_EXT);
  47. FileOutputStream fileStream = new FileOutputStream(new File(DEST));
  48. fileStream.write(imgSteam.toByteArray());
  49. fileStream.close();
  50. }
  51. /**
  52. * freemarker渲染html
  53. */
  54. public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
  55. Writer out = new StringWriter();
  56. try {
  57. // 获取模板,并设置编码方式
  58. Template template = freemarkerCfg.getTemplate(htmlTmp);
  59. template.setEncoding("UTF-8");
  60. // 合并数据模型与模板
  61. template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
  62. out.flush();
  63. return out.toString();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. } finally {
  67. try {
  68. out.close();
  69. } catch (IOException ex) {
  70. ex.printStackTrace();
  71. }
  72. }
  73. return null;
  74. }
  75. /**
  76. * 根据模板生成pdf文件流
  77. */
  78. public static ByteArrayOutputStream createPdf(String content) {
  79. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  80. ITextRenderer render = new ITextRenderer();
  81. ITextFontResolver fontResolver = render.getFontResolver();
  82. try {
  83. fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  84. } catch (com.lowagie.text.DocumentException e) {
  85. e.printStackTrace();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. // 解析html生成pdf
  90. render.setDocumentFromString(content);
  91. //解决图片相对路径的问题
  92. render.getSharedContext().setBaseURL(LOGO_PATH);
  93. render.layout();
  94. try {
  95. render.createPDF(outStream);
  96. return outStream;
  97. } catch (com.lowagie.text.DocumentException e) {
  98. e.printStackTrace();
  99. } finally {
  100. try {
  101. outStream.close();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. return null;
  107. }
  108. /**
  109. * 根据pdf二进制文件 生成图片文件
  110. *
  111. * @param bytes pdf二进制
  112. * @param scaling 清晰度
  113. * @param pageNum 页数
  114. */
  115. public static ByteArrayOutputStream pdfToImg(byte[] bytes, float scaling, int pageNum,String formatName) {
  116. //推荐的方法打开PdfDecoder
  117. PdfDecoder pdfDecoder = new PdfDecoder(true);
  118. FontMappings.setFontReplacements();
  119. //修改图片的清晰度
  120. pdfDecoder.scaling = scaling;
  121. ByteArrayOutputStream out = new ByteArrayOutputStream();
  122. try {
  123. //打开pdf文件,生成PdfDecoder对象
  124. pdfDecoder.openPdfArray(bytes); //bytes is byte[] array with PDF
  125. //获取第pageNum页的pdf
  126. BufferedImage img = pdfDecoder.getPageAsImage(pageNum);
  127. ImageIO.write(img, formatName, out);
  128. } catch (PdfException e) {
  129. e.printStackTrace();
  130. } catch (IOException e){
  131. e.printStackTrace();
  132. }
  133. return out;
  134. }
  135. }

输出结果:


Jpedal支持将指定页Pdf生成图片,pdfDecoder.scaling设置图片的分辨率(不同分辨率下文件大小不同) ,支持多种图片格式,具体更多可自行研究

8.总结

对于电子凭证的技术方案,总结如下:

1.html模板+model数据,通过freemarker进行渲染,便于维护和修改

2.渲染后的html流,可通过Flying Saucer组件生成pdf文件流,或者生成pdf后再转成jpg文件流

3.在Web项目中,对应的文件流,可以通过ContentType设置,在线查看/下载,不需通过附件服务

9.纯前端解决方案

还有一种解决方案是使用PhantomJS

git地址: https://github.com/ariya/phantomjs

PhantomJS 是一个基于 WebKit 的服务器端 JavaScript
API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和
SVG。 PhantomJS 可以用于 页面自动化 , 网络监测 , 网页截屏 ,以及 无界面测试 等。

具体方法可自行查询。

以上内容转载自:https://my.oschina.net/lujianing/blog/894365,感谢蛙牛的分享,红色部分是自己做的时候踩到的坑,所以补上

Java iText+FreeMarker生成PDF(HTML转PDF)的更多相关文章

  1. [Web Pdf] flying-saucer + iText + Freemarker生成pdf 跨页问题

    转载于: https://blog.csdn.net/qq_31980421/article/details/79662988 flying-saucer + iText +  Freemarker实 ...

  2. java使用freemarker生成静态html页面

    1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  3. java使用freemarker 生成word文档

      java 生成word文档     最近需要做一个导出word的功能, 在网上搜了下, 有用POI,JXL,iText等jar生成一个word文件然后将数据写到该文件中,API非常繁琐而且拼出来的 ...

  4. JAVA用freemarker生成复杂Excel。(freemarker)

    在生成Excel的时候,大多时候都是使用poi,jxl等进行的,但是对于复杂的Excel来说,这个工作量是非常的大的,而且,对于我这么懒的人来说,这是相当痛苦的一件事情,所以,我不得不找找有没有简单一 ...

  5. java使用freemarker生成word文档

    1.原料 开源jar包freemarker.eclipse.一份模板word文档 2.首先设计模板word文档 一般,通过程序输出的word文档的格式是固定的,例如建立一个表格,将表格的标题写好,表格 ...

  6. JAVA使用Freemarker生成静态文件中文乱码

    1.指定Configuration编码 Configuration freemarkerCfg = new Configuration(); freemarkerCfg.setEncoding(Loc ...

  7. 使用freemarker生成word、html时图片显示问题

    使用freemarker生成word.html时图片显示问题 博客分类: Java 使用freemarker生成word时图片显示问题使用freemarker生成html时图片显示问题使用iText生 ...

  8. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

  9. flying-saucer + iText + Freemarker实现pdf的导出, 支持中文、css以及图片

    前言 项目中有个需求,需要将合同内容导出成pdf.上网查阅到了 iText , iText 是一个生成PDF文档的开源Java库,能够动态的从XML或者数据库生成PDF,同时还可以对文档进行加密,权限 ...

随机推荐

  1. 用< 100行代码向EPUB或Web服务器添加视频回放

    下载source - 32.3 KB 下载latest version from GituHub 介绍 在我 在关于CodeProject的前一篇文章中,我展示了一个简单的EPUB查看器 Androi ...

  2. Windows7 提示“无法访问 xxxx,您没有权限访问,请与网络管理员联系请求访问权限”的解决办法

    Windows7 客户端访问提示"无法访问 xxxx,您没有权限访问,请与网络管理员联系请求访问权限"的解决办法

  3. JMeter实战(一) 体系结构

    此为开篇,介绍JMeter的组成结构,阅读后对JMeter形成整体认知和初步印象. 为了便于后续讲解,先明确下2个术语. 元件:如HTTP请求.事务控制器.响应断言,就是一个元件. 组件:如逻辑控制器 ...

  4. Redis安装使用以及可能的算法实践

    layout: post title: 2017-12-05-Redis 入门 tags: - Redis - 实时特征 - UCB, Tompson sampling Redis 是什么? Redi ...

  5. 【C语言程序设计】—最近超火的小游戏—【数字炸弹】!

    ✍  准备工作和建议 一.程序的原理 在动手编程之前,得先跟大家说一下这个程序是干什么的. 我们可以称呼这个游戏为<数字炸弹>. 游戏的原理是这样: 每一轮电脑从 1 到 100 中随机抽 ...

  6. centos8平台:用fontconfig安装及管理字体(fc-list/fc-match/fc-cache)

    一,fc-list所属的rpm包 [root@blog ~]$ whereis fc-list fc-list: /usr/bin/fc-list /usr/share/man/man1/fc-lis ...

  7. LNOI 2020 退役记

    不会爆零了吧嘤嘤嘤 \(Day -7\) 周五正在上化学珂,突然被老师叫出去说省选还有名额,问我报不报名.啊嘞嘞还有一周了告诉我还有名额?经过了激烈的思想斗争,还是决定停课搞一搞,学一回OI好歹看看省 ...

  8. CentOS 8 Yum安装ansible

    ansible的安装 直接yum安装ansible会报错,需要先安装epel源. ansible的安装来源于epel仓库,因此在安装前需确保安装了正确的epel源. yum install -y ep ...

  9. Linux命令之命令别名

    对于经常执行的较长的命令,可以将其定义成较短的别名,以方便执行 显示当前shell进程所有可用的命令别名 [04:33:43 root@C8[ ~]#alias alias cp='cp -i' al ...

  10. javaSE、javaEE、Android知识点总结

    曾今上学时候的一些学习总结,如有错误请大家指出,共同学习. 1. 什么是WebView? WebView是一个使用WebKit引擎的浏览器组件,用来加载网页. 2. WebView中加载网页的两种方式 ...