HTML生成PDF不显示中文或报错,求解救。

陈小渣 2014-04-29 11:50:43
java 代码如下。
public class ToPdf {
public static void main(String[] args) throws DocumentException,
IOException {
String path=System.getProperty("user.dir")+"/src/";

String inputFile =path+"samples/fistdoc.html";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = path+"outputs/fistdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer render = new ITextRenderer();
render.getFontResolver().addFont("C:/Windows/Fonts/simsun.ttc",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
render.getFontResolver().addFont("C:/WINDOWS/Fonts/Arial.ttf",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//
render.setDocument(url);
render.layout();
render.createPDF(os);
os.close();
}

}

HTML代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>aa</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="overflow:hidden;">
<span class="strt-name">the HTML to PDF</span>
很坚决
</body>
</html>
...全文
181 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈小渣 2014-04-29
  • 打赏
  • 举报
回复
编码吗? 用什么编码能解决呢?
tony4geek 2014-04-29
  • 打赏
  • 举报
回复
英文显示? 那就是编码的问题。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧