【问题标题】:Font is not available to the JVM with Jasper Reports in Docker Container带有 Docker 容器中的 Jasper Reports 的 JVM 无法使用字体
【发布时间】:2020-12-13 22:11:29
【问题描述】:

我正在尝试在 Docker 容器中使用 DynamicJasper 生成报告? 如果我收到以下错误:

net.sf.jasperreports.engine.util.JRFontNotFoundException:  
                                 Font 'Arial' is not available to the JVM.   

我该如何解决这个问题?

【问题讨论】:

    标签: java docker fonts jasper-reports


    【解决方案1】:

    本质上,JVM 本身不包含任何字体。这就是为什么你必须自己安装它们。您的解决方案似乎已经很好了。对于基于 Debian 的 Docker 映像,我执行以下操作:

    #Add non-free repositories to get MS fonts
    
    RUN apt-get update
    RUN apt-get install -y --no-install-recommends software-properties-common
    RUN apt-add-repository contrib
    RUN apt-get update
    
    # If you want to use Microsoft fonts in reports, you must install the fonts
    # Andale Mono, Arial Black, Arial, Comic Sans MS, Courier New, Georgia, Impact,
    # Times New Roman, Trebuchet, Verdana,Webdings)
    RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections
    RUN apt-get install -y --no-install-recommends fontconfig ttf-mscorefonts-installer
    ADD localfonts.conf /etc/fonts/local.conf
    RUN fc-cache -f -v
    
    
    # If you want to use special characters, such as chinese ideograms, you must install a 
    font that support them
    # For example:
    RUN apt install -y fonts-wqy-zenhei
    

    localfonts.conf 文件针对调整“msttcorefonts”集合的一些抗锯齿规则进行了优化。哟可以在这里找到它:https://wiki.ubuntu.com/Fonts#Manual_Font_Smoothing 不幸的是,文件长度太长,无法在 SO 上共享,但假设我发布的链接将保留在 Ubuntu wiki 上是合理的。

    【讨论】:

    • 当我使用你的文件时,我收到如下错误:E: Unable to locate package ttf-mscorefonts-installer 我猜基于 debian 和基于 ubuntu 是不同的。
    • 你的图片是基于 debian 还是基于 ubuntu?
    • 我的镜像是基于 Ubuntu 的。
    【解决方案2】:

    我使用了一个像下面这样的 Dockerfile,问题就解决了。 我尝试在容器的文件系统中搜索字体,使用:

    ls /usr/share/fonts/truetype/msttcorefonts/
    

    我可以看到文件夹加载的字体。

    FROM openjdk:8u141-jre
    COPY /build/libs/my-app*.jar /my-app.jar
    EXPOSE 9500
    
    RUN apt-get update && apt-get install -y vim
    RUN echo "deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty multiverse  deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty-updates multiverse  deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse" | tee /etc/apt/sources.list.d/multiverse.list
    RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true |  debconf-set-selections
    RUN ["apt-get", "-y", "install", "ttf-mscorefonts-installer"]
    RUN dpkg-reconfigure  ttf-mscorefonts-installer
    RUN apt-get install -y apt-transport-https apt-utils
    RUN apt-get install  --reinstall -y ttf-mscorefonts-installer
    CMD java -XX:+PrintFlagsFinal $JAVA_OPTS -jar /my-app.jar
    

    【讨论】:

      【解决方案3】:

      这对我基于 Debian 的 Docker 映像有所帮助

      RUN apt-get -q update \
          && apt-get -q install -y cabextract libmspack0 \
          && wget http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.6_all.deb \
          && dpkg -i ttf-mscorefonts-installer_3.6_all.deb
      

      【讨论】:

        猜你喜欢
        • 2011-04-18
        • 1970-01-01
        • 1970-01-01
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多