使用JasperReport 的项目部署到linux系统上,出现找不到中文字体

    xiaoxiao2022-07-02  108

    JasperReport 错误:net.sf.jasperreports.engine.util.JRFontNotFoundException: Font '宋体' is not available

    JasperReport 错误:net.sf.jasperreports.engine.util.JRFontNotFoundException: Font '黑体' is not available

    原因:

           1)inux在没有安装报表所需中文字体的情况

              1.安装方法——转到Linux环境下的/usr/share/fonts路径下,新建一个dir如zh_CN,把准备好的中文字体传到该目录下(中文字体的获取可以从windows环境的C:/Windows/Fonts下拷贝~),再执行如下命令:

    mkfontscale mkfontdir  fc-cache -fv 

    即可成功安装字体。可以执行命令:

    fc-list

    查看是否安装好字体。

            2..在项目的classes目录下加一个jasperreports.properties配置文件,添加内容如下:

    net.sf.jasperreports.awt.ignore.missing.font=true

    再重启项目      

     2.系统为英文版,无法识别中文

      方案一:

      将系统语言切换为中文

      方案二:(部署项目比较麻烦) 1、把需要用到的字体(可以直接拷贝windows系统的C:\WINDOWS\Fonts 下的相关字体)拷贝当前项目的classpath下,一般为classes目录下  2、在classpath里添加 jasperreports.properties 属性文件 

    net.sf.jasperreports.awt.ignore.missing.font=true 

     方案三:(我使用的是这个方法)

     修改jasperreports-6.8.0.jar的JRDesignTextElement类

     将中文字体名换成英文名

    package net.sf.jasperreports.engine.design; import java.awt.Color; import java.io.IOException; import java.io.ObjectInputStream; import net.sf.jasperreports.engine.JRDefaultStyleProvider; import net.sf.jasperreports.engine.JRFont; import net.sf.jasperreports.engine.JRLineBox; import net.sf.jasperreports.engine.JRParagraph; import net.sf.jasperreports.engine.JRTextElement; import net.sf.jasperreports.engine.base.JRBaseLineBox; import net.sf.jasperreports.engine.base.JRBaseParagraph; import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport; import net.sf.jasperreports.engine.type.HorizontalAlignEnum; import net.sf.jasperreports.engine.type.HorizontalTextAlignEnum; import net.sf.jasperreports.engine.type.LineSpacingEnum; import net.sf.jasperreports.engine.type.ModeEnum; import net.sf.jasperreports.engine.type.RotationEnum; import net.sf.jasperreports.engine.type.VerticalAlignEnum; import net.sf.jasperreports.engine.type.VerticalTextAlignEnum; import net.sf.jasperreports.engine.util.StyleResolver; public abstract class JRDesignTextElement extends JRDesignElement implements JRTextElement { private static final long serialVersionUID = 10200L; protected HorizontalTextAlignEnum horizontalTextAlign; protected VerticalTextAlignEnum verticalTextAlign; protected RotationEnum rotationValue; protected String markup; protected JRLineBox lineBox; protected JRParagraph paragraph; protected String fontName; protected Boolean isBold; protected Boolean isItalic; protected Boolean isUnderline; protected Boolean isStrikeThrough; protected Float fontsize; protected String pdfFontName; protected String pdfEncoding; protected Boolean isPdfEmbedded; private String switchEnglish(String fontName) { switch(fontName){ case "黑体" : return "SimHei"; case "宋体" : return "SimSun"; default : return fontName; } } protected JRDesignTextElement(JRDefaultStyleProvider defaultStyleProvider) { super(defaultStyleProvider); this.lineBox = new JRBaseLineBox(this); this.paragraph = new JRBaseParagraph(this); } public ModeEnum getModeValue() { return getStyleResolver().getMode(this, ModeEnum.TRANSPARENT); } public HorizontalTextAlignEnum getHorizontalTextAlign() { return getStyleResolver().getHorizontalTextAlign(this); } public HorizontalTextAlignEnum getOwnHorizontalTextAlign() { return this.horizontalTextAlign; } public void setHorizontalTextAlign(HorizontalTextAlignEnum horizontalTextAlign) { Object old = this.horizontalTextAlign; this.horizontalTextAlign = horizontalTextAlign; getEventSupport().firePropertyChange("horizontalTextAlignment", old, this.horizontalTextAlign); } public VerticalTextAlignEnum getVerticalTextAlign() { return getStyleResolver().getVerticalTextAlign(this); } public VerticalTextAlignEnum getOwnVerticalTextAlign() { return this.verticalTextAlign; } public void setVerticalTextAlign(VerticalTextAlignEnum verticalTextAlign) { Object old = this.verticalTextAlign; this.verticalTextAlign = verticalTextAlign; getEventSupport().firePropertyChange("verticalTextAlignment", old, this.verticalTextAlign); } public RotationEnum getRotationValue() { return getStyleResolver().getRotationValue(this); } public RotationEnum getOwnRotationValue() { return this.rotationValue; } public void setRotation(RotationEnum rotationValue) { Object old = this.rotationValue; this.rotationValue = rotationValue; getEventSupport().firePropertyChange("rotation", old, this.rotationValue); } public String getMarkup() { return getStyleResolver().getMarkup(this); } public String getOwnMarkup() { return this.markup; } public void setMarkup(String markup) { Object old = this.markup; this.markup = markup; getEventSupport().firePropertyChange("markup", old, this.markup); } public JRLineBox getLineBox() { return this.lineBox; } public JRParagraph getParagraph() { return this.paragraph; } /** * @deprecated */ public JRFont getFont() { return this; } /** * @deprecated */ public void setFont(JRFont font) { setFontName(this.switchEnglish(font.getOwnFontName())); setBold(font.isOwnBold()); setItalic(font.isOwnItalic()); setUnderline(font.isOwnUnderline()); setStrikeThrough(font.isOwnStrikeThrough()); setFontSize(font.getOwnFontsize()); setPdfFontName(font.getOwnPdfFontName()); setPdfEncoding(font.getOwnPdfEncoding()); setPdfEmbedded(font.isOwnPdfEmbedded()); } public String getFontName() { return getStyleResolver().getFontName(this); } public String getOwnFontName() { return this.fontName; } public void setFontName(String fontName) { Object old = this.fontName; this.fontName = this.switchEnglish(fontName); getEventSupport().firePropertyChange("fontName", old, this.fontName); } public boolean isBold() { return getStyleResolver().isBold(this); } public Boolean isOwnBold() { return this.isBold; } /** * @deprecated */ public void setBold(boolean isBold) { setBold(Boolean.valueOf(isBold)); } public void setBold(Boolean isBold) { Object old = this.isBold; this.isBold = isBold; getEventSupport().firePropertyChange("isBold", old, this.isBold); } public boolean isItalic() { return getStyleResolver().isItalic(this); } public Boolean isOwnItalic() { return this.isItalic; } /** * @deprecated */ public void setItalic(boolean isItalic) { setItalic(Boolean.valueOf(isItalic)); } public void setItalic(Boolean isItalic) { Object old = this.isItalic; this.isItalic = isItalic; getEventSupport().firePropertyChange("isItalic", old, this.isItalic); } public boolean isUnderline() { return getStyleResolver().isUnderline(this); } public Boolean isOwnUnderline() { return this.isUnderline; } /** * @deprecated */ public void setUnderline(boolean isUnderline) { setUnderline(Boolean.valueOf(isUnderline)); } public void setUnderline(Boolean isUnderline) { Object old = this.isUnderline; this.isUnderline = isUnderline; getEventSupport().firePropertyChange("isUnderline", old, this.isUnderline); } public boolean isStrikeThrough() { return getStyleResolver().isStrikeThrough(this); } public Boolean isOwnStrikeThrough() { return this.isStrikeThrough; } /** * @deprecated */ public void setStrikeThrough(boolean isStrikeThrough) { setStrikeThrough(Boolean.valueOf(isStrikeThrough)); } public void setStrikeThrough(Boolean isStrikeThrough) { Object old = this.isStrikeThrough; this.isStrikeThrough = isStrikeThrough; getEventSupport().firePropertyChange("isStrikeThrough", old, this.isStrikeThrough); } public float getFontsize() { return getStyleResolver().getFontsize(this); } public Float getOwnFontsize() { return this.fontsize; } public void setFontSize(Float fontSize) { Object old = this.fontsize; this.fontsize = fontSize; getEventSupport().firePropertyChange("fontSize", old, this.fontsize); } public String getPdfFontName() { return getStyleResolver().getPdfFontName(this); } public String getOwnPdfFontName() { return this.pdfFontName; } public void setPdfFontName(String pdfFontName) { Object old = this.pdfFontName; this.pdfFontName = pdfFontName; getEventSupport().firePropertyChange("pdfFontName", old, this.pdfFontName); } public String getPdfEncoding() { return getStyleResolver().getPdfEncoding(this); } public String getOwnPdfEncoding() { return this.pdfEncoding; } public void setPdfEncoding(String pdfEncoding) { Object old = this.pdfEncoding; this.pdfEncoding = pdfEncoding; getEventSupport().firePropertyChange("pdfEncoding", old, this.pdfEncoding); } public boolean isPdfEmbedded() { return getStyleResolver().isPdfEmbedded(this); } public Boolean isOwnPdfEmbedded() { return this.isPdfEmbedded; } /** * @deprecated */ public void setPdfEmbedded(boolean isPdfEmbedded) { setPdfEmbedded(Boolean.valueOf(isPdfEmbedded)); } public void setPdfEmbedded(Boolean isPdfEmbedded) { Object old = this.isPdfEmbedded; this.isPdfEmbedded = isPdfEmbedded; getEventSupport().firePropertyChange("isPdfEmbedded", old, this.isPdfEmbedded); } public Color getDefaultLineColor() { return getForecolor(); } public Object clone() { JRDesignTextElement clone = (JRDesignTextElement)super.clone(); clone.lineBox = this.lineBox.clone(clone); clone.paragraph = this.paragraph.clone(clone); return clone; } private int PSEUDO_SERIAL_VERSION_UID = 60002; /** * @deprecated */ private Byte horizontalAlignment; /** * @deprecated */ private Byte verticalAlignment; /** * @deprecated */ private HorizontalAlignEnum horizontalAlignmentValue; /** * @deprecated */ private VerticalAlignEnum verticalAlignmentValue; /** * @deprecated */ private Byte rotation; /** * @deprecated */ private Byte lineSpacing; /** * @deprecated */ private LineSpacingEnum lineSpacingValue; /** * @deprecated */ private Boolean isStyledText; /** * @deprecated */ private Integer fontSize; private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); if (this.PSEUDO_SERIAL_VERSION_UID < 30702) { this.horizontalAlignmentValue = HorizontalAlignEnum.getByValue(this.horizontalAlignment); this.verticalAlignmentValue = VerticalAlignEnum.getByValue(this.verticalAlignment); this.rotationValue = RotationEnum.getByValue(this.rotation); this.lineSpacingValue = LineSpacingEnum.getByValue(this.lineSpacing); this.horizontalAlignment = null; this.verticalAlignment = null; this.rotation = null; this.lineSpacing = null; } if (this.isStyledText != null) { this.markup = (this.isStyledText.booleanValue() ? "styled" : "none"); this.isStyledText = null; } if (this.paragraph == null) { this.paragraph = new JRBaseParagraph(this); this.paragraph.setLineSpacing(this.lineSpacingValue); this.lineSpacingValue = null; } if (this.PSEUDO_SERIAL_VERSION_UID < 50502) { this.fontsize = (this.fontSize == null ? null : Float.valueOf(this.fontSize.floatValue())); this.fontSize = null; } if (this.PSEUDO_SERIAL_VERSION_UID < 60002) { this.horizontalTextAlign = HorizontalAlignEnum.getHorizontalTextAlignEnum(this.horizontalAlignmentValue); this.verticalTextAlign = VerticalAlignEnum.getVerticalTextAlignEnum(this.verticalAlignmentValue); this.horizontalAlignmentValue = null; this.verticalAlignmentValue = null; } } }

    jar包我这边已经修改了  ,好像这里不能上传文件 。。。

    最新回复(0)