zoukankan      html  css  js  c++  java
  • html导出pdf

    <!-- html转PDF -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>2.0.1</version>
        </dependency>


    public class ItextPDFUtil {

    public static void main(String[] args) {
    String htmlStr = null;
    InputStream inputStream = null;
    PdfDocument pd = null;
    try {
    // 读取html的流
    inputStream = new FileInputStream(new File("F:/协议.html"));

    // 流转换成字符串
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = inputStream.read(b)) != -1;) {
    out.append(new String(b, 0, n));
    }

    htmlStr = out.toString();
    String pdffile = "F:/test.pdf";

    pd = new PdfDocument(new PdfWriter(new FileOutputStream(new File(pdffile))));
    // 设置文件标题为fileName,web上展示的标题为此标题
    pd.getDocumentInfo().setTitle(pdffile);
    }
    catch (Exception e) {
    e.printStackTrace();
    }

    Document document = new Document(pd, PageSize.A4);
    try {
    // 导入字体
    FontProvider font = new FontProvider();
    font.addFont("F:/SimSun.ttf");

    ConverterProperties c = new ConverterProperties();
    c.setFontProvider(font);
    c.setCharset("utf-8");

    // 设置页面边距 必须先设置边距,再添加内容,否则页边距无效
    document.setMargins(50, 0, 50, 0);
    List<IElement> list = HtmlConverter.convertToElements(htmlStr, c);
    for (IElement ie : list) {
    document.add((IBlockElement) ie);
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    document.close();
    }

    }

    public static void htmlToPdf(String html, OutputStream outStream, String fontPath) {
    PdfDocument pd = null;
    Document document = null;
    try {
    pd = new PdfDocument(new PdfWriter(outStream));

    // 导入字体
    FontProvider font = new FontProvider();
    font.addStandardPdfFonts();
    font.addFont(fontPath);
    ConverterProperties c = new ConverterProperties();
    c.setFontProvider(font);
    c.setCharset("utf-8");

    // 设置页面边距 必须先设置边距,再添加内容,否则页边距无效
    document = new Document(pd, PageSize.A4, true);
    document.setMargins(50, 0, 40, 0);
    List<IElement> list = HtmlConverter.convertToElements(html, c);
    for (IElement ie : list) {
    document.add((IBlockElement) ie);
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    document.close();
    }

    }
  • 相关阅读:
    [读书笔记]-技术学习-微服务架构与实践
    [文章转载]-Java后端,应该日常翻看的中文技术网站 -江南白衣
    [文章转载]-我的Java后端书架-江南白衣
    正则表达式有多强大一看便知!
    微信小程序支付功能完整流程
    判断字符串是否合法(1)
    ES6新增常用方法
    JS求一个字符串在另一个字符串中出现的次数
    根据对象的某个属性排序
    数组去除重复值的四种超简便方法
  • 原文地址:https://www.cnblogs.com/shenggege5240/p/10063090.html
Copyright © 2011-2022 走看看