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;'>\n" +
" <td>"+report.getName()+"</td>\n" +
" <td>"+report.getPhone()+"</td>\n" +
" <td>"+report.getIdentificationNuber()+"</td>\n" +
" </tr>\n" ;
String body = "<h1 style='text-align: center'>报告</h1>\n" +
"<div style='width:100%;margin:0 auto;'>\n" +
"<table border='1px' style='border: 1px solid black;width:100%;text-align: center;border-collapse:collapse;'>\n" +
" <thead>\n" +
" <tr style='border: 1px solid black;'>\n" +
" <th>姓名</th>\n" +
" <th>电话</th>\n" +
" <th>身份证号</th>\n" +
" </tr>\n" +
" </thead>\n" +
" <tbody>\n" +
// reportData+
" </tbody>\n" +
"</table>\n" +
"</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);
}
}
html 字符串 生成 pdf 完美解决中文不显示的更多相关文章
- PHP生成PDF完美支持中文,解决TCPDF乱码
PHP生成PDF完美支持中文,解决TCPDF乱码 2011-09-26 09:04 418人阅读 评论(0) 收藏 举报 phpfontsheaderttfxhtml文档 PHP生成PDF完美支持中文 ...
- Python之将Python字符串生成PDF
笔者在今天的工作中,遇到了一个需求,那就是如何将Python字符串生成PDF.比如,需要把Python字符串'这是测试文件'生成为PDF, 该PDF中含有文字'这是测试文件'. 经过一番检索, ...
- jquery插件导出excel和pdf(解决中文乱码问题)
参考文件:http://jackyrong.iteye.com/blog/2169683 https://my.oschina.net/aruan/blog/418980 https://segmen ...
- 完美解决pycharm 不显示代码提示问题
pycharm 不显示代码提示 1.检查IDE省电模式是否关闭状态!!! file → power save mode 取消掉 2.检查代码提示是否成功开启. setting → Inspection ...
- asp.net MVC设计模式中使用iTextSharp实现html字符串生成PDF文件
因个人需求,需要将html格式转换成PDF并加上水印图片.于是乎第一次接触这种需求的小菜鸟博主我,在某度搜索引擎上不断的查阅关键字资料.踩坑,终于有了一个相应的解决方案.以下是解决步骤,记录下来方便以 ...
- qt 利用 HTML 生成PDF文档,不能显示jpg图片
利用 QPrinter 和html 生成 pdf文档 其中用html语句有显示图片的语句 但只能显示png格式的图片,不能显示jpg格式图片. 经过排查:语法,文件路径等都正确,最终在stack ov ...
- 使用Python爬取微信公众号文章并保存为PDF文件(解决图片不显示的问题)
前言 第一次写博客,主要内容是爬取微信公众号的文章,将文章以PDF格式保存在本地. 爬取微信公众号文章(使用wechatsogou) 1.安装 pip install wechatsogou --up ...
- Visual Studio Code 1.44 解决中文代码显示乱码问题(小白图文教程)
现今主流的计算机中文字符编码方案是:GBK和UTF-8. 不同编码方案使用不同的字符集,GBK字符集在中文字符长度和字符数量上存在绝对优势,但对国外字符并不支持.所以,完全面向国内的程序/网页使用的是 ...
- java转pdf(html转为pdf),解决中文乱码,标签不规范等问题
第一步,下载jar包以及建对应的文件夹.注意pd4ml的jar要选择pro版本.然后建一个pd4fonts.properties 里面对应的字体. SimSun = simsun.ttf 前面为变量名 ...
随机推荐
- css3的动画效果
全新的css3加入的动画效果: [ animation-name ]:检索或设置对象所应用的动画名称 [ animation-duration ]: 检索或设置对象动画的持续时间 [ animatio ...
- EF生成实体自动添加数据库字段注释
我们在用EF从数据库生成模型的时候,默认实体类是没有注释的,但是我们已经在数据库字段添加说明了,能不能自动把注释也拿过来? 答案是:能. 那么我们开始 首先随便开一个ASP.NET MVC项目,我 ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- OCP 12c考试题,062题库出现大量新题-第20道
choose three Your database is configured for ARCHIVELOG mode, and a daily full database backup is ta ...
- 五花八门的CSS
一.颜色 rgba(0, 0, 0, 0.5) rgba括号中前3个数字代表着 red green blue三种颜色的rgb值,0-255,最后一个是设定这个颜色的透明度即alpha值.范围从0到1, ...
- JavaScript实现HTML页面集成QQ空间分享功能
<!DOCTYPE HTML> <html> <head> <title>QQ空间分享</title> <meta http-equi ...
- 使用 Navicate 连接 Oracle9i 数据库
Navicat Premium 是一个可多重连接的数据库管理工具,它可让你以单一程序同時连接到 MySQL.SQLite.Oracle 及 PostgreSQL 数据库,让管理不同类型的数据库更加方便 ...
- C#6.0语言规范(二) 词法结构
程式 AC#程序由一个或多个源文件组成,正式称为编译单元(编译单元).源文件是Unicode字符的有序序列.源文件通常与文件系统中的文件一一对应,但不需要此对应关系.为了获得最大的可移植性,建议使用U ...
- MySQL-Cluster 和主从(Master,Slave)搭建总结
双主互备,主从 什么是双主 MultiSource 多源复制 原理及流程图 主要步骤 1,在 Master Server 上开启 bin log 日志 和 设置 server-id :(在my.cn ...
- 跨浏览器的javascript事件的封装
一,跨浏览器的事件处理程序 1,DOM0级处理事件 将一个函数赋值给一个事件处理程序属性. 事件流:冒泡阶段. 使用: 为元素增加事件: var btn = document.getElementBy ...