html转pdf问题记录,中文不显示,中文不换行,css样式无效 您所在的位置:网站首页 网页生成pdf不显示 html转pdf问题记录,中文不显示,中文不换行,css样式无效

html转pdf问题记录,中文不显示,中文不换行,css样式无效

2024-04-11 04:44| 来源: 网络整理| 查看: 265

代码示例:

public static void htmlToPDF(String pdfPath, String htmlPath) throws Exception { try { ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); OutputStream os = new FileOutputStream(pdfPath); String url = new File(htmlPath).toURI().toURL().toString(); renderer.setDocument(url); //设置字体库路径,获取文件再磁盘上的绝对路径 String simsunpath=""; String arialpath=""; fontResolver.addFont(simsunpath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); fontResolver.addFont(arialpath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); renderer.layout(); renderer.createPDF(os, false); renderer.finishPDF(); os.close(); } catch (Exception e) { //处理异常 //_log.error("htmlToPDF出现异常!", e); } }

常见问题:

一.中文不显示:

1.引用字体文件,simsun.ttc和arial.ttf,百度下载

2.html格式符合要求,必须有html标签,且所有标签必须闭合,且body必须设置字体,示例如下:

中文 test

二.中文不换行

原来的jar不支持中文换行,需要自己增加语言环境判断和中文兼容

不管使用core-renderer还是flying-saucer-pdf,都可以修改org.xhtmlrenderer.layout.Breaker

从jar包中反编译出代码,修改换行代码:

示例:

package org.xhtmlrenderer.layout; /** * @author 12550 * @program: 02src * @description: 解决中文不换行 * @date 2021-04-20 14:46:40 */ import org.xhtmlrenderer.css.constants.IdentValue; import org.xhtmlrenderer.css.style.CalculatedStyle; import org.xhtmlrenderer.extend.TextRenderer; import org.xhtmlrenderer.render.FSFont; public class Breaker { public static void breakFirstLetter(LayoutContext c, LineBreakContext context, int avail, CalculatedStyle style) { FSFont font = style.getFSFont(c); context.setEnd(getFirstLetterEnd(context.getMaster(), context.getStart())); context.setWidth(c.getTextRenderer().getWidth(c.getFontContext(), font, context.getCalculatedSubstring())); if (context.getWidth() > avail) { context.setNeedsNewLine(true); context.setUnbreakable(true); } } private static int getFirstLetterEnd(String text, int start) { int i = start; while (i < text.length()) { char c = text.charAt(i); int type = Character.getType(c); if ((type != 21) && (type != 22) && (type != 29) && (type != 30) && (type != 24)) { break; } i++; } if (i < text.length()) { i++; } return i; } public static void breakText(LayoutContext c, LineBreakContext context, int avail, CalculatedStyle style) { FSFont font = style.getFSFont(c); IdentValue whitespace = style.getWhitespace(); if (whitespace == IdentValue.NOWRAP) { context.setEnd(context.getLast()); context.setWidth(c.getTextRenderer().getWidth(c.getFontContext(), font, context.getCalculatedSubstring())); return; } if ((whitespace == IdentValue.PRE) || (whitespace == IdentValue.PRE_WRAP) || (whitespace == IdentValue.PRE_LINE)) { int n = context.getStartSubstring().indexOf("\n"); if (n > -1) { context.setEnd(context.getStart() + n + 1); context.setWidth(c.getTextRenderer().getWidth(c.getFontContext(), font, context.getCalculatedSubstring())); context.setNeedsNewLine(true); context.setEndsOnNL(true); } else if (whitespace == IdentValue.PRE) { context.setEnd(context.getLast()); context.setWidth(c.getTextRenderer().getWidth(c.getFontContext(), font, context.getCalculatedSubstring())); } } if ((whitespace == IdentValue.PRE) || ((context.isNeedsNewLine()) && (context.getWidth() 0) && (graphicsLength


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有