HTML to PDF font-family missing in .net

1 Answer 303 Views
General Discussions PdfProcessing
Evlo
Top achievements
Rank 1
Evlo asked on 07 Dec 2022, 10:19 PM | edited on 08 Dec 2022, 10:39 AM

Hi, i'm trying to use document processing to convert HTML template to PDF, but fonts are missing from resulting file, maybe even from the imported html.

How can I find out if the fonts are already missing after importing HTML?

What is the best way to convert HTML to PDF keeping images, fonts, etc. using document processing?

This is one of my attempts to get it to work.

static void Main(string[] args)
        {
            string templateFileName = @"html template.htm";
            string pdfPath = @"test html template to pdf.pdf";
            FileStream templateAsStream = new FileStream(templateFileName, FileMode.Open, FileAccess.Read);

            ///////////
            //telerik
            ///////////

            

            try
            {               
                // Register the font 
                byte[] fontDataR = File.ReadAllBytes("Raleway-VariableFont_wght.ttf");
                System.Windows.Media.FontFamily fontFamilyR = new System.Windows.Media.FontFamily("Releway");
                byte[] fontDataRL = File.ReadAllBytes("Raleway-Light.ttf");
                System.Windows.Media.FontFamily fontFamilyRL = new System.Windows.Media.FontFamily("Releway Light");

                //Telerik.Windows.Documents.Fixed.Model.Fonts.
                FontsRepository.RegisterFont(fontFamilyR, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, fontDataR);
                FontsRepository.RegisterFont(fontFamilyRL, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, fontDataRL);

                byte[] fontDataRb = File.ReadAllBytes("Raleway-Bold.ttf");
                System.Windows.Media.FontFamily fontFamilyRb = new System.Windows.Media.FontFamily("Releway");
                //Telerik.Windows.Documents.Fixed.Model.Fonts.
                FontsRepository.RegisterFont(fontFamilyR, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Bold, fontDataRb);


                //import AFTER setting fonts
                //Telerik.Windows.Documents.Flow.FormatProviders.Html.
                HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
                HtmlImportSettings importSettings = new HtmlImportSettings();
                HtmlExportSettings exportSettings = new HtmlExportSettings();
                var document = htmlProvider.Import(templateAsStream);                

                PdfFormatProvider pdfProvider = new PdfFormatProvider();

                //Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
                //Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;

                PdfExportSettings pdfExportSettings = new PdfExportSettings();
                pdfExportSettings.ShouldEmbedFonts = true;

                pdfProvider.ExportSettings = pdfExportSettings;


                FileStream stream = File.Create("fixed pdf.pdf");
                var radFixedDocument = (new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider()).ExportToFixedDocument(document);
                var fixedFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
                fixedFormatProvider.Export(radFixedDocument, stream);
                stream.Close();

                //var resultBytes = pdfProvider.Export(document);
                //File.WriteAllBytes(pdfPath, resultBytes);

            }
            catch(Exception e)
            {
                throw e;
            }

I have also attached html file that is result of importing html and then export html

 

also I get this in pdf reader both if I use embed fonts option or not

 

https://www.telerik.com/forums/radflowdocument-to-pdf---arialnarrow-font-issues

1 Answer, 1 is accepted

Sort by
0
Accepted
Maria
Telerik team
answered on 12 Dec 2022, 11:09 AM

Hello Evlo,

I wanted to ask what your application type is. In addition, are using NET Framework or Net Standard? If you are using the Net Standard version you need to handle the fonts and images manually. Detailed information is available here: PdfProcessing.

I noticed that you have manually registered the custom font. I tested your code and it turns out that there is a typo in the FontFamilly names (Releway instead of Raleway). Once this is changed the font is correctly exported to PDF. 

byte[] fontDataR = File.ReadAllBytes("Raleway-VariableFont_wght.ttf"); System.Windows.Media.FontFamily fontFamilyR = new System.Windows.Media.FontFamily("Raleway");byte[] fontDataRL = File.ReadAllBytes("Raleway-Light.ttf"); System.Windows.Media.FontFamily fontFamilyRL = new System.Windows.Media.FontFamily("Raleway Light");

I hope this information helps. Should you have any other questions, I will be glad to assist you.

Regards,


Maria
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Evlo
Top achievements
Rank 1
commented on 13 Dec 2022, 12:49 PM

Thank you so much, it was that typo, honestly those are the bane of my life in doing any code, I just can't see it.
Tags
General Discussions PdfProcessing
Asked by
Evlo
Top achievements
Rank 1
Answers by
Maria
Telerik team
Share this question
or