• html 字符串 生成 pdf 完美解决中文不显示


    //maven

    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.4.2</version>
    </dependency>
    <dependency>
      <groupId>com.itextpdf.tool</groupId>
      <artifactId>xmlworker</artifactId>
      <version>5.4.1</version>
    </dependency>
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itext-asian</artifactId>
      <version>5.2.0</version>
    </dependency>

    //java

    package com.leoodata.utils;
    
    import com.itextpdf.text.Document;
    import com.itextpdf.text.Font;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfWriter;
    import com.itextpdf.tool.xml.XMLWorkerHelper;
    import com.leoodata.entity.Report;
    import org.jsoup.Jsoup;
    import java.io.*;
    import java.nio.charset.Charset;
    import static java.awt.SystemColor.info;
    
    /**
     * User: 杨永生
     * Date: 15:45 2018/4/10
     * Email: kevin@hiibook.com
     */
    
    public class PDFReport {
    public static void htmlToPDF(Report report,String fileNameAndURL) { try { //创建html String reportData = " <tr style='border-bottom: 1px solid black;'> " + " <td>"+report.getName()+"</td> " + " <td>"+report.getPhone()+"</td> " + " <td>"+report.getIdentificationNuber()+"</td> " + " </tr> " ;
    String body
    = "<h1 style='text-align: center'>报告</h1> " + "<div style='100%;margin:0 auto;'> " + "<table border='1px' style='border: 1px solid black;100%;text-align: center;border-collapse:collapse;'> " + " <thead> " + " <tr style='border: 1px solid black;'> " + " <th>姓名</th> " + " <th>电话</th> " + " <th>身份证号</th> " + " </tr> " + " </thead> " + " <tbody> " + // reportData+ " </tbody> " + "</table> " + "</div>";
    StringBuilder html
    = new StringBuilder(); html.append("<html>"); html.append("<body style='font-size:20px;font-family:SimSun;'>"); html.append(body); html.append("</body>"); html.append("</html>");
    InputStream is
    = new ByteArrayInputStream(html.toString().getBytes("utf-8"));
    File files
    =new File(fileNameAndURL); //打印查看上传路径 System.out.println(fileNameAndURL); if(!files.getParentFile().exists()){ files.getParentFile().mkdirs(); }
    OutputStream os
    = new FileOutputStream(fileNameAndURL); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, os); document.open(); // 将html转pdf XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, Charset.forName("UTF-8")); document.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Report report=new Report(); String url= "E:/HiibookIntellijProject/svn2/leoodata/target/leoodata/static/upload/report/余*157**446732018_04_11_17_37_21_891.pdf"; htmlToPDF(report,url); } }
  • 相关阅读:
    3.for in循环
    2.break与continue
    1.XHTML框架结构
    lamda表达式在EF中的应用
    View数据呈现相关技术
    ASP.NET MVC 4 技术讲解
    ASP.NET MVC 相关的社群与讨论区
    C# 随机红包算法
    圆圈里带 小写字母,大写字母
    使用SQL语句 检测 MSSQL死锁
  • 原文地址:https://www.cnblogs.com/yysbolg/p/8806216.html
Copyright © 2020-2023  润新知