之前使用freemark模板渲染ftl頁面,轉(zhuǎn)出的pdf中,css2有些樣式好像不支持,比較常用的居中樣式都沒有效果,text-align:center 改造成使用html頁面來轉(zhuǎn)pdf,css2的樣式可以生效,itext是不支持css3的彈性布局的 ITextRenderer pdfRenderer = new ITextRenderer(); // 添加字體設(shè)置 ITextFontResolver fontResolver = pdfRenderer.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); */ try { fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); /*String path = ResourceUtil.getResourceObj("classpath:/fonts/SimSun.ttf").getUrl().getPath(); *//*fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*//* fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); */ } catch (Exception e) { log.error(e.getMessage(),e); } pdfRenderer.setDocumentFromString(createXHtml(rowsHtml)); /* File f= getFileFromResources(); pdfRenderer.setDocument(f);*/ pdfRenderer.layout(); pdfRenderer.createPDF(outputStream);
嘗試好多種方式,最后可能是跟字體有關(guān)系
字體設(shè)置為C:/Windows/Fonts/simhei.ttf? 黑體,同時html頁面上樣式要添加
body { font-family: SimHei; }
pdf生成方式參考項目:E:\myfiles\project\github\demo-html2pdf
字體問題參考文章:https://blog.51cto.com/u_15127651/4527950
文章來源:http://www.zghlxwxcb.cn/news/detail-828832.html
最后完美解決字體問題!!文章來源地址http://www.zghlxwxcb.cn/news/detail-828832.html
到了這里,關(guān)于Itext生成pdf文件,html轉(zhuǎn)pdf時中文一直顯示不出來的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!