使用JasperReport 的項目部署到linux系統上,出現找不到中文字體

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包我這邊已經修改了  ,好像這裏不能上傳文件 。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章