日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php stream_select w3c,使用flying-saucer 实现 html转pdf实现input框select,textarea checkbox等的显示...

發(fā)布時間:2024/1/1 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php stream_select w3c,使用flying-saucer 实现 html转pdf实现input框select,textarea checkbox等的显示... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

com.itextpdf

itextpdf

${itextpdf.version}

com.itextpdf

itext-asian

${itext.asian.version}

中文顯示問題解決了,當(dāng)時碰到了打印表單中input框等不顯示的問題。

今天換了個方案使用flying-saucer 實現(xiàn)html轉(zhuǎn)pdf

org.xhtmlrenderer

flying-saucer-pdf-itext5

9.1.1

實現(xiàn)代碼如下:

public void html2pdf(String html, File file, String fontDir) throws I18NIllegalArgumentException {

try (OutputStream os = new FileOutputStream(file); ){

ITextRenderer renderer = new ITextRenderer();

ITextFontResolver fontResolver = (ITextFontResolver) renderer.getSharedContext().getFontResolver();

//添加字體庫 begin

File f = new File(fontDir);

if (f.isDirectory()) {

File[] files = f.listFiles(new FilenameFilter() {

@Override

public boolean accept(File dir, String name) {

String lower = name.toLowerCase();

return lower.endsWith(".otf") || lower.endsWith(".ttf") || lower.endsWith(".ttc");

}

});

for (int i = 0; i < files.length; i++) {

fontResolver.addFont(files[i].getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

}

}

//添加字體庫end

renderer.setDocumentFromString(html);

renderer.layout();

renderer.createPDF(os);

} catch (Exception e) {

throw new I18NIllegalArgumentException(e);

}

}

網(wǎng)上查的都是添加字體的方法都是這樣實現(xiàn)的:

ITextFontResolver fontResolver = renderer.getFontResolver();

fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

fontResolver.addFont("C:/Windows/Fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

這樣實現(xiàn)有個缺陷:字體的路徑都是系統(tǒng)的目錄,不同系統(tǒng)(windows,Linux)字體目錄不同,對系統(tǒng)依賴太高。

為了解決這個問題:找到字體庫文件,復(fù)制到項目的目錄中,在代碼中直接遍歷這個目錄,添加所有的字體。

注意:頁面中字體不能使用中文,需要使用英文名稱,而且是大小寫敏感的!例如宋體的英文名稱是 SimSun(注意不是simsun!,首字母都是大寫的)

錯誤寫法:font-family:'宋體' 或者 ?font-family:'simsun' ?或者 ?font-family:?'微軟雅黑'

正確寫法:font-family:'SimSun' 或者?font-family:'SimHei'或者 ?font-family:? 'Microsoft YaHei'

另外: html的格式要求符合xml格式(即必須有結(jié)束標(biāo)簽)

測試:

測試用html代碼:

我是標(biāo)題

*{font-family: 'SimSun';}

我是內(nèi)容

asdfsdf今晚打老虎

運(yùn)動睡覺

html瀏覽器顯示結(jié)果:

生成的pdf:

總結(jié)

以上是生活随笔為你收集整理的php stream_select w3c,使用flying-saucer 实现 html转pdf实现input框select,textarea checkbox等的显示...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。