Export to pdf - font styles not exported?

4 posts, 0 answers
  1. John
    John avatar
    2 posts
    Member since:
    Nov 2010

    Posted 20 Mar Link to this post

    Hi! I'm generating an Excel document containing various fonts of "HelveticaNeue". Please see attached images for samples.

    When exporting to pdf using PdfFormatProvider, the font styles are changed, e.g. bold style disappears. However, when exporting using Excel interops it works.

    Code using interops:

    public static void ExcelToPdf( string xlFilePath, string pdfOutputPath )
    {
        try
        {
            Excel.Application app = new Excel.Application();
            app.Visible = false;
            app.DisplayAlerts = false;
            Excel.Workbook wkb = app.Workbooks.Open( xlFilePath, ReadOnly: true );
            wkb.ExportAsFixedFormat( Excel.XlFixedFormatType.xlTypePDF, pdfOutputPath );
     
            wkb.Close();
            app.Quit();
        }
        catch ( Exception ex )
        {
            Console.WriteLine( ex.StackTrace );
            throw ex;
        }
    }

     

    Code using Telerik PdfFormatProvider:

    public override void Generate( DimensionTableInput input )
    {
        CreateWorkbook();
        CreateWorkbookStyles();
        CreateWorksheets( input );
     
        XlsxFormatProvider xlsxFormatProvider = new XlsxFormatProvider();
     
        PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
        // this will result in 1 page per sheet
        pdfFormatProvider.ExportSettings = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.Export.PdfExportSettings( ExportWhat.EntireWorkbook, true );
     
        string xlsxFileName = $"{input.DocumentTitle}.xlsx";
        string xlsxFullPath = $"{ Path.Combine( input.OutputPath, xlsxFileName )}";
        ExportToFile( xlsxFormatProvider, xlsxFullPath );
     
        string pdfFileName = $"{input.DocumentTitle}.pdf";
        string pdfFullPath = $"{ Path.Combine( input.OutputPath, pdfFileName )}";
        ExportToFile( pdfFormatProvider, pdfFullPath );
        //Helpers.ExcelHelper.ExcelToPdf( xlsxFullPath, pdfFullPath );
    }
     
    private void ExportToFile( IWorkbookFormatProvider formatProvider, string fileName )
    {
        using ( Stream fs = File.OpenWrite( fileName ) )
        {
            formatProvider.Export( _workbook, fs );
        }
    }

     

    I can share the original Excel and Pdf files if needed.

    Thanks in advance,

    John

  2. Polya
    Admin
    Polya avatar
    262 posts

    Posted 22 Mar Link to this post

    Hi John,

    When the Helvetica Neue font is used we're using Segoe UI as fallback font when exporting the xlsx document to pdf. When this fallback happens, we are not preserving the bold/italic, etc. styles of the text. That is the reason why after export the bold style is lost.
    Microsoft Excel also have a fallback font for the Helvetica Neue font, when exporting to pdf, but they are using Cambria, instead of Segoe UI. Also, they are preserving the bold style of the text.
    That said, I can only suggest using a different font; one that can also be used when exporting, instead of a fallback font replacement. For example, Arial font.
    Hope this is suitable for your scenario.

    Regards,
    Polya
    Progress Telerik

  3. John
    John avatar
    2 posts
    Member since:
    Nov 2010

    Posted 26 Mar Link to this post

    Hi Poyla,

    Thank you for the reply. Why does the fallback happen at all? Why can't Helvetica Neue be used during the export to pdf? I can't use another font because the marketing department is very specific about the fonts and styles.

     

    BR,

    John

  4. Polya
    Admin
    Polya avatar
    262 posts

    Posted 28 Mar Link to this post

    Hi John,

    We've investigated our side and it appears that there is a problem with the "HelveticaNeue.ttf" font. In mode detail, the issue is that this font file does not contain information about encoding for the Windows platform in its data. For that reason, we, as well as Microsoft Excel, fall back to different font when exporting this document.
    In order to try and fix the font, I've opened it using FontForge, changed its encoding (Encoding->Reencode-> "ISO 10646-1 (Unicode Full)") and generated a new font (File->Generate Fonts; in the dialog choose "True Type" for the first combo box and then save the new MyHelveticaNeue.ttf file). After that, I've installed the new font (MyHelveticaNeue.ttf), replacing the previously installed font (HelveticaNeue.ttf). As a result, the xlsx file was exported with the "Helvetica Neue" font, instead of "Segoe UI". 
    In short, the font file was fixed after passing it through the FontForge program so you could give this approach a try at your side and see whether the issue is resolved.

    Regards,
    Polya
    Progress Telerik

Back to Top