//maven
com.itextpdf itextpdf 5.4.2 com.itextpdf.tool xmlworker 5.4.1 com.itextpdf itext-asian 5.2.0
//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 = " \n" + " "+report.getName()+"\n" + " "+report.getPhone()+"\n" + " "+report.getIdentificationNuber()+"\n" + " \n" ; String body = "报告
\n" + "\n" + ""; StringBuilder html = new StringBuilder(); html.append(""); html.append(""); html.append(body); html.append(""); html.append(""); 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); }}\n" + " \n" + "
\n" + "\n" + " \n" + " \n" + " \n" +// reportData+ " \n" + "姓名 \n" + "电话 \n" + "身份证号 \n" + "