IText转PDF秒会
最近在开发过程中,碰到了这样的需求:将图片及相关文字信息,按视觉标准,排版后直接转成PDF。因为之前没接触过,乍一听很懵,感觉会很难。但经过一番网上搜索学习后,发现其实很简单!
(一)Maven引入依赖
注意:一定要按此版本号引入,不然会出现很多错误,包括“中文乱码、中文不输出或直接报错”等异常!
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.4.1</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.10.1</version> </dependency>
(二)快速入门 —— Hello Word!
package com.netease.test; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class ITextTest { public static void main(String[] args) throws Exception { //第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); //第二步,为该Document创建一个Writer实例: PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\HelloWorld.pdf")); //第三步,打开当前Document document.open(); //第四步,为当前Document添加内容; document.add(new Paragraph("Hello World")); //第五步,关闭Document document.close(); System.out.println( "OK!" ); } }去C盘下找找,打开你的第一个用ITextTest生成的PDF吧,就这么简单,有没有爱上它?
(三)HTML转PDF之快速入门 —— Hello Word!
1.先写一个简单的HelloWorld.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> <h1>Hello Word!</h1> <h2>Hello Word!</h2> <h3>Hello Word!</h3> <h4>Hello Word!</h4> <h5>Hello Word!</h5> <h6>Hello Word!</h6> <h7>Hello Word!</h7> </body> </html>
注:此处一定要注意,所有标签最终必须全部封闭!比如<meta charset="UTF-8"/>没有封闭,写成了<meta charset="UTF-8">。转换时,会如下错误:
Exception in thread "main" com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag meta.
2.Java代码
import java.io.FileInputStream; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class ITextTest { public static void main(String[] args) throws Exception { //第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); //第二步,为该Document创建一个Writer实例: PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\HelloWorld2.pdf")); //第三步,打开当前Document document.open(); //第四步,为当前Document添加内容: //document.add(new Paragraph("Hello World")); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("C:\\HelloWorld.html")); //第五步,关闭Document document.close(); System.out.println( "OK!" ); } }
去C盘下找找,打开你的第二个用ITextTest生成的PDF,看看效果吧:
(四)直接向PDF中写入html代码
package com.netease.test; import java.io.ByteArrayInputStream; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.Font; import com.itextpdf.text.FontProvider; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class PdfTest { public static void main(String[] args) { try { Document document = new Document(); PdfWriter mPdfWriter = PdfWriter.getInstance(document, new FileOutputStream("C:\\test.pdf")); document.open(); String s = getHtml(); ByteArrayInputStream bin = new ByteArrayInputStream(s.getBytes()); XMLWorkerHelper.getInstance().parseXHtml(mPdfWriter, document, bin, null, new ChinaFontProvide()); System.out.println("OK"); document.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 拼写html字符串代码 * <p>Title: getHtml</p> * @author Liyan * @date 2017年4月1日 下午6:30:30 */ public static String getHtml() { StringBuffer html = new StringBuffer(); html.append("<div>咆哮的黄河</div>"); html.append("<div><img src='http://www.photo0086.com/member/5758/pic/2013081217201520156.JPG'/></div>"); return html.toString(); } /** * 解决中文字体 * <p>Title: ChinaFontProvide</p> * @author Liyan * @date 2017年4月1日 下午6:30:48 */ public static final class ChinaFontProvide implements FontProvider { @Override public Font getFont(String arg0, String arg1, boolean arg2, float arg3, int arg4, BaseColor arg5) { BaseFont bfChinese = null; try { bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //也可以使用Windows系统字体(TrueType) //bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); } catch (Exception e) { e.printStackTrace(); } Font FontChinese = new Font(bfChinese, 20, Font.NORMAL); return FontChinese; } @Override public boolean isRegistered(String arg0) { return false; } } }
注意:IText并不是支持所有前端的标签!
去C盘下找找,打开你的第三个用ITextTest生成的PDF,看看效果吧:
如果你还有进一步需求,想详细了解IText的强大功能,可以下载下面的两个文档,进行参考!
点击下载: