java html转pdf手动分页

方法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版本,其他版本没有getCharBBox(char)方法,只有getCharBBox(int)方法)

代码:

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 voidmain(String[] 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.parse(newByteArrayInputStream(htmlString.toString().getBytes()));

        ITextRenderer renderer =new ITextRenderer();

        renderer.setDocument(doc,null);

        renderer.layout();

        OutputStream out =new FileOutputStream("D://test.pdf");

        renderer.createPDF(out);

        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 main(String[] 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.getInstance(document,
newFileOutputStream("D://test.pdf"));
        document.open();
        InputStream htmlInput =
new ByteArrayInputStream(htmlString.toString().getBytes("UTF-8"));
       
// 使用自定义的字体提供器,并将其设置为unicode字体样式
       
FontsProvider fontProvider = new FontsProvider();
        fontProvider.addFontSubstitute(
"lowagie","garamond");
        fontProvider.setUseUnicode(
true);
        CssAppliers cssAppliers =
new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext =
newHtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        XMLWorkerHelper.getInstance().getDefaultCssResolver(
true);

       
//解析html文件
  
XMLWorkerHelper.getInstance().parseXHtml(writer,document, htmlInput,null, Charset.forName("UTF-8"), fontProvider);

       
//关闭流
       
document.close();
        writer.close();
    }

   
/**
     *
内部类,用于解决中文显示乱码问题
     */
   
private static class FontsProviderextendsXMLWorkerFontProvider {

       
/**
         *
构造函数
         */
       
public FontsProvider(){
           
super(null,null);
        }

       
/**
         *
重载,获取自定义的字体
         *
         * @param
fontName字体名称
         * @param
encoding编码信息
         * @param
size字体大小
         * @param
style字体风格
         * @return Font
自定义的字体
         */
       
@Override
       
public FontgetFont(finalString fontName, String encoding, float size,finalint style) {
            String fntname = fontName;
           
if (fntname==null) {
                fntname =
"宋体";
            }
           
if (size ==0) {
                size =
4;
            }
           
return super.getFont(fntname,encoding, size, style);
        }
    }
}

 


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值