简体   繁体   English

自定义字体不适用于java中的itextrenderer

[英]Custom font is not working with itextrenderer in java

I have embedded my custom fonts in html with @font-face. 我用@ font-face在html中嵌入了自定义字体。 This working with all browser but when I am trying to convert it to pdf through itextrender in java custom font is not working anymore its taking default fonts like Arial. 这适用于所有浏览器,但当我试图通过java自定义字体中的itextrender将其转换为pdf时,它不再使用它采用像Arial这样的默认字体。

css code : css代码

    @font-face {
        font-family: Subaru-Medium;
        src: url('fonts/Subaru-Medium.eot'); /* IE9 Compat Modes */
        src: url('fonts/Subaru-Medium.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
             url('fonts/Subaru-Medium.ttf')  format('truetype'), /* Safari, Android, iOS */
             url('fonts/Subaru-Medium.svg#svgFontName') format('svg'); /* Legacy iOS */
        }

java code : java代码

public static void writePDF(String HTMLfileName, String PDFFileName, String WhereToSave,String fontDirectory)
    {
        try
        {
            String url = new File(HTMLfileName).toURI().toURL().toString();
            String outputFile = WhereToSave+PDFFileName;
            OutputStream os = new FileOutputStream(outputFile);

            ITextRenderer renderer = new ITextRenderer();
            ITextFontResolver fontResolver=renderer.getFontResolver();
                  fontResolver.addFont("C:\\Users\\benay.debnath\\Desktop\\htmltemplate\\fonts\\Subaru-Medium.ttf", true);

                // fontResolver.addFontDirectory(fontDirectory, true);
                SharedContext scontext=renderer.getSharedContext();
                // scontext.setDPI(72);
                scontext.setDotsPerPixel(12);
                renderer.setDocument(url);
                renderer.layout();
                renderer.createPDF(os);
                os.close();
                System.out.println("status:$:true^#^message:$:PDF    Genarated^#^fileName:$:"+outputFile);
        }catch (Exception e) {
            System.out.println("status:$:false^#^message:$:"+e.getMessage());
        }
    }

Any one can help me to find what I am doing wrong? 任何人都可以帮我找到我做错了什么? help much appreciated. 非常感谢。

This issue has resolved, the problem was in css I mentioned font-family:"Subaru-Medium" in font file(.ttf) font name was "Subaru Medium" so it was not working. 这个问题已经解决了,问题出在css我提到的字体系列:字体文件中的“Subaru-Medium”(.ttf)字体名称是“Subaru Medium”所以它无效。 Now its working fine :) 现在它的工作正常:)

First of all make sure that below font has been installed in your system if not then install font and add following line in your code. 首先确保系统中已安装了以下字体,如果没有,则安装字体并在代码中添加以下行。

public static void writePDF(String HTMLfileName, String PDFFileName, String WhereToSave,String fontDirectory)
    {
        try
        {
            String url = new File(HTMLfileName).toURI().toURL().toString();
            String outputFile = WhereToSave+PDFFileName;
            OutputStream os = new FileOutputStream(outputFile);

            ITextRenderer renderer = new ITextRenderer();
    renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\Calibri.ttf","UTF-8",BaseFont.NOT_EMBEDDED);

                // fontResolver.addFontDirectory(fontDirectory, true);
                SharedContext scontext=renderer.getSharedContext();
                // scontext.setDPI(72);
                scontext.setDotsPerPixel(12);
                renderer.setDocument(url);
                renderer.layout();
                renderer.createPDF(os);
                os.close();
                System.out.println("status:$:true^#^message:$:PDF    Genarated^#^fileName:$:"+outputFile);
        }catch (Exception e) {
            System.out.println("status:$:false^#^message:$:"+e.getMessage());
        }

I was also facing same problem i have resolved using above solution. 我也面临同样的问题,我已经解决了使用上述解决方案。 hope this will be useful for you :) 希望这对你有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM