亲测代码没有问题,需要注意细节已经标注;需要jar包:iText-2.0.8.jar;core-renderer-R8.jar;
core-renderer-R8.jar下载地址:http://central.maven.org/maven2/org/xhtmlrenderer/core-renderer/R8/core-renderer-R8.jar
iText-2.0.8.jar下载地址:http://central.maven.org/maven2/com/lowagie/itext/2.0.8/itext-2.0.8.jar
import com.lowagie.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class HtmlToPDF {
public static void main(String[] args) throws Exception {
htmlToPdf("C://Users//Administrator//Desktop//20181001.pdf","C://Users//Administrator//Desktop//xse//newFile.html");
}
/**
* 把URL转换为PDF
* @param outputFile
* , 示例:/data/fs/inspector/BJ20150522001.pdf
* @param url
* ,示例:http :xxxx
* @return
* @throws Exception
*/
public static void htmlToPdf(String outputFile, String url)throws Exception {
try{
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
// 如果携带图片则加上以下代码,将图片标签转换为Itext自己的图片对象
renderer.getSharedContext().setReplacedElementFactory(new Base64ImgReplacedElementFactory());
renderer.getSharedContext().getTextRenderer().setSmoothingThreshold(0);
//String fontPath = HtmlToPDF.class.getClassLoader().getResource("").toString().replaceAll("file:/", "C:\\Users\\Administrator\\Downloads\\simsunttc\\") + "simsun.ttc";
// 解决中文支持问题 字体必须名称必须和前台页面的大小写一直(simsun 区分大小写)
String fontPath="C:\\Users\\Administrator\\Downloads\\simsunttc\\simsun.ttc";
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
url=new File(url).toURI().toURL().toString();
renderer.setDocument(url);
//解决图片相对路径问题,window 上必须用file:/; 后面路径只写到img标签中src的上级目录
renderer.getSharedContext().setBaseURL("file:/C:/Users/Administrator/Desktop/xse/");
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
}catch(Exception e ){
e.printStackTrace();
}
}
}
import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.codec.Base64;
import org.w3c.dom.Element;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.extend.ReplacedElementFactory;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.pdf.ITextFSImage;
import org.xhtmlrenderer.pdf.ITextImageElement;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.simple.extend.FormSubmissionListener;
import java.io.IOException;
/**
* * 图片base64支持,把图片转换为itext自己的图片对象
* * @author Administrator
* *
*
*/
public class Base64ImgReplacedElementFactory implements ReplacedElementFactory {
/**
* * 实现createReplacedElement 替换html中的Img标签
* *
* * @param c 上下文
* * @param box 盒子
* * @param uac 回调
* * @param cssWidth css宽
* * @param cssHeight css高
* * @return ReplacedElement
*
*/
public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box, UserAgentCallback uac,int cssWidth, int cssHeight) {
Element e = box.getElement();
if (e == null) {
return null;
}
String nodeName = e.getNodeName();
// 找到img标签
if (nodeName.equals("img")) {
String attribute = e.getAttribute("src");
FSImage fsImage;
try {
// 生成itext图像
fsImage = buildImage(attribute, uac);
} catch (BadElementException e1) {
fsImage = null;
} catch (IOException e1) {
fsImage = null;
}
if (fsImage != null) {
// 对图像进行缩放
if (cssWidth != -1 || cssHeight != -1) {
fsImage.scale(cssWidth, cssHeight);
}
return new ITextImageElement(fsImage);
}
}
return null;
}
/**
* * 编解码base64并生成itext图像
*
*/
protected FSImage buildImage(String srcAttr, UserAgentCallback uac) throws IOException,
BadElementException {
FSImage fiImg = null;
if (srcAttr.toLowerCase().startsWith("data:image/")) {
String base64Code = srcAttr.substring(srcAttr.indexOf("base64,") + "base64,".length(), srcAttr.length());
// 解码
byte[] decodedBytes = Base64.decode(base64Code);
fiImg = new ITextFSImage(Image.getInstance(decodedBytes));
} else {
fiImg = uac.getImageResource(srcAttr).getImage();
}
return fiImg;
}
public void reset() {
}
@Override
public void remove(Element arg0) {
}
@Override
public void setFormSubmissionListener(FormSubmissionListener arg0) {
}
}