方法1:用com.lowagie
引用jar包:
compile group:'org.xhtmlrenderer',name:'core-renderer',version:'R8'
compile group:'com.lowagie',name:'itext',version:'2.0.8'
只能2.0.8版本,其他版本没有getCharBBoxchar)方法,只有getCharBBoxint)方法)
代码:
import com.lowagie.text.DocumentException;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public classTest {
public static voidmainString[] args)
throwsParserConfigurationException,IOException, SAXException, DocumentException {
StringBuffer htmlString =new StringBuffer);
htmlString.append"<html>");
htmlString.append"<head><style>@page{}.pageNext{page-break-after:always;}</style></head>");
htmlString.append"<body>");
htmlString.append"<div>Hello</div>");
htmlString.append"<divclass='pageNext'></div>");
htmlString.append"<div>World</div>");
htmlString.append"</body></html>");
DocumentBuilder builder =DocumentBuilderFactory.newInstance).newDocumentBuilder);
Document doc = builder.parsenewByteArrayInputStreamhtmlString.toString).getBytes)));
ITextRenderer renderer =new ITextRenderer);
renderer.setDocumentdoc,null);
renderer.layout);
OutputStream out =new FileOutputStream"D://test.pdf");
renderer.createPDFout);
out.close);
}
}
方法2:用com.itextpdf
引用jar包:
compile'com.itextpdf:itextpdf:5.4.2')
compile'com.itextpdf.tool:xmlworker:5.4.1')
代码:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
public class Test {
public static void mainString[] args)throws IOException,DocumentException {
StringBuffer htmlString = new StringBuffer);
htmlString.append"<html>");
htmlString.append"<body>");
htmlString.append"<div>Hello</div>");
htmlString.append"<div style='page-break-before:always'>World</div>");
htmlString.append"</body></html>");
Document document = new Document);
PdfWriter writer = PdfWriter.getInstancedocument,newFileOutputStream"D://test.pdf"));
document.open);
InputStream htmlInput = new ByteArrayInputStreamhtmlString.toString).getBytes"UTF-8"));
// 使用自定义的字体提供器,并将其设置为unicode字体样式
FontsProvider fontProvider = new FontsProvider);
fontProvider.addFontSubstitute"lowagie","garamond");
fontProvider.setUseUnicodetrue);
CssAppliers cssAppliers = new CssAppliersImplfontProvider);
HtmlPipelineContext htmlContext =newHtmlPipelineContextcssAppliers);
htmlContext.setTagFactoryTags.getHtmlTagProcessorFactory));
XMLWorkerHelper.getInstance).getDefaultCssResolvertrue);
//解析html文件
XMLWorkerHelper.getInstance).parseXHtmlwriter,document, htmlInput,null, Charset.forName"UTF-8"), fontProvider);
//关闭流
document.close);
writer.close);
}
/**
* 内部类,用于解决中文显示乱码问题
*/
private static class FontsProviderextendsXMLWorkerFontProvider {
/**
* 构造函数
*/
public FontsProvider){
supernull,null);
}
/**
* 重载,获取自定义的字体
*
* @param fontName字体名称
* @param encoding编码信息
* @param size字体大小
* @param style字体风格
* @return Font 自定义的字体
*/
@Override
public FontgetFontfinalString fontName, String encoding, float size,finalint style) {
String fntname = fontName;
if fntname==null) {
fntname = "宋体";
}
if size ==0) {
size = 4;
}
return super.getFontfntname,encoding, size, style);
}
}
}