0

I have embedded my custom fonts in html with @font-face. 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.

css code:

    @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:

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Robert Smith
  • 457
  • 2
  • 9
  • 25
  • I have same Problem. I am working in grails and using rendering plugin. I am using css to import font but in pdf I am getting expected result. have you some idea about this? – user1791574 Jun 05 '13 at 08:46

2 Answers2

3

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 :)

shrey
  • 833
  • 1
  • 7
  • 15
2

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. Now its working fine :)

Robert Smith
  • 457
  • 2
  • 9
  • 25
  • If it's one, please [accept your answer](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/). – ollo Jan 21 '13 at 19:04
  • Comparing the actual "Font Name" property and "font-family" in CSS was the decisive hint. These values should be identical... This worked for me. – hvsp Nov 22 '13 at 22:04