解決html2pdf中文不顯示問題

2021-09-20 10:20:34 字數 1212 閱讀 8142

本地不能顯示中文解決辦法如下

// ftl檔案中body加上任意中文字型,如宋體:simsun

bytearrayoutputstream outstream = null;

inputstream is = null;

writer out = null;

// 1、得template物件

configuration cfg = new configuration(configuration.version_2_3_23);

cfg.setclassfortemplateloading(this.getclass(), ftlpath);

template temp = cfg.gettemplate(ftlfilename);

// 2、植入資料(data鍵值對資料都可以,比如json、map)

outstream = new bytearrayoutputstream();

out = new outputstreamwriter(outstream, "utf-8");

temp.process(data, out);

// 3、轉pdf

byte u = outstream.tobytearray();

is = new bytearrayinputstream(u);

// 設定字型(得在資源檔案resources/fonts中加入字型檔案,微軟雅黑、宋體等)

string fonts = this.getclass().getresource("/fonts").getpath()

fontprovider fontprovider = new defaultfontprovider();

fontprovider.adddirectory(fonts);

converterproperties p = new converterproperties();

p.setfontprovider(fontprovider);

p.setcharset("utf-8");

file pdffile = new file(pdffilepathandname);

htmlconverter.converttopdf(is, new pdfwriter(pdffile), p);

// pdffile 即我們拿到的pdf檔案

完美解決centos7下pdf中文不顯示問題

最近的 開發中,有乙個需要word轉pdf的需求,本地發現word轉pdf後中文正常展示,但是centos裡面中文部分是空白的。針對此問題,解決方案如下 將本地windows裡面的字型複製到linux上 windows10 字型路徑為 c windows fonts,在裡面找到需要的字型,其中宋體 ...

解決Sublime Text 2中文顯示亂碼問題

欲解決問題,關鍵在於讓sublime text 2支援gb2312和gbk。步驟如下 1.安裝sublime package control。在sublime text 2上用ctrl 開啟控制台並在裡面輸入以下 sublime text 2就會自動安裝package control。import ...

解決Sublime Text 2中文顯示亂碼問題

欲解決問題,關鍵在於讓sublime text 2支援gb2312和gbk。步驟如下 1.安裝sublime package control。在sublime text 2上用ctrl 開啟控制台並在裡面輸入以下 sublime text 2就會自動安裝package control。html vi...