Export to PDF with embedded subset font

1 Answer 1279 Views
PdfProcessing WordsProcessing
Indra
Top achievements
Rank 1
Iron
Iron
Indra asked on 14 Feb 2022, 08:06 PM

As explained in https://www.telerik.com/forums/size-of-pdf-document

RadPdfProcessing does not embed the whole font but instead embeds only the font subset of the used characters which guarantees minimum font size

 

I want to use a docx as a template for PDF,  so from your sample github https://github.com/telerik/document-processing-sdk/tree/master/WordsProcessing/ContentControls

I use Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider.ExportToFixedDocument to create a RadFixedDocument

then use Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider to export to final PDF.

The problem is the docx is using non standard font, so when I embed the font set (normal, italic, bold, italic bold) the final PDF size reached 3MB with just simple text and no image. This PDF is definitely embedding the full font sets, not the subset.

Is this scenario unsupported or bug?

 

I modified your DocumentGenerator.cs in ContentControls_NetStandard project. Must copy the segoeui.ttf and seguili.ttf to the SampleData folder.


 //overwrite this
        private void Save(RadFlowDocument document)
        {
            IFormatProvider<RadFlowDocument> formatProvider = new DocxFormatProvider();

            string path = "CVTemplate.docx";
            using (FileStream stream = File.OpenWrite(path))
            {
                formatProvider.Export(document, stream);
            }

            Console.WriteLine("Document generated.");

            ProcessStartInfo psi = new ProcessStartInfo()
            {
                FileName = path,
                UseShellExecute = true
            };

            Process.Start(psi);

            //modification
            ExportToFixedDocumentPdfFromRadFlowDocumentDocx(document);
        }

        private void ExportToFixedDocumentPdfFromRadFlowDocumentDocx(RadFlowDocument document)
        {
            //handle png image for NetStandard
            Telerik.Windows.Documents.Extensibility.JpegImageConverterBase jpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
            Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = jpegImageConverter;

            using (FileStream stream = File.Create("ExportedToFixedDocumentPdfFromRadFlow_with_embeddedFont.pdf"))
            {
                // Read the font file -- copy these 2 ttf to sample folder
                byte[] fontDataNormal = File.ReadAllBytes($"{SampleDataFolder}segoeui.ttf");
                byte[] fontDataLight = File.ReadAllBytes($"{SampleDataFolder}seguili.ttf");

                // Register the font set 
                Telerik.Documents.Core.Fonts.FontFamily fontFamily = new Telerik.Documents.Core.Fonts.FontFamily("Segoe UI");
                Telerik.Documents.Core.Fonts.FontFamily fontFamily2 = new Telerik.Documents.Core.Fonts.FontFamily("Segoe UI Light");

                Telerik.Windows.Documents.Fixed.Model.Fonts.FontsRepository.RegisterFont(
                    fontFamily,
                    Telerik.Documents.Core.Fonts.FontStyles.Normal,
                    Telerik.Documents.Core.Fonts.FontWeights.Normal, fontDataNormal);
                Telerik.Windows.Documents.Fixed.Model.Fonts.FontsRepository.RegisterFont(
                    fontFamily2,
                    Telerik.Documents.Core.Fonts.FontStyles.Normal,
                    Telerik.Documents.Core.Fonts.FontWeights.Normal, fontDataLight);

                //export the docx RadFlowDocument to RadFixedDocument
                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);
            }
        }

 

 

 

 

 

1 Answer, 1 is accepted

Sort by
1
Martin
Telerik team
answered on 15 Feb 2022, 09:54 AM

Hello Indra,

I am sorry to hear you are experiencing issues due to a currently not supported by the PdfProcessing functionality. The embedding of font subsets is currently supported only for the .NET Framework version of the library: PdfProcessing: Expose an option to plug logic for embedding a subset of a registered font for platforms different than .NET Framework. You can cast your vote for the implementation as well as subscribe to the task by clicking the Follow button so you can receive updates about status changes.

However, because of the huge demand (even not following the PDF Specification), we have a feature request logged in our backlog to provide an option to export documents without embedding the fonts in them: PdfProcessing: Implement a setting that allows choosing if the fonts should be embedded in the document. I cannot engage with a specific date but this feature is currently in development and if it passes all QA tests it could be live until the next official release.

I hope you find this information helpful.

Regards,
Martin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing WordsProcessing
Asked by
Indra
Top achievements
Rank 1
Iron
Iron
Answers by
Martin
Telerik team
Share this question
or