Blink engine ignores font styles on AWS Lambda while (HTML to PDF)

Hello,

Having a weird issue while converting HTML to PDF on AWS Lambda. 

This is my code block running on the Lambda. It basically converts an HTML string to PDF file and returns the bytes: 

using Amazon.Lambda.Core;
using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;
using System.Text;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AWSLambda
{
public class PdfCreator
{

public static byte[] CreatePdfFromHtml()
{
try {
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("<key_here>");
PdfDocument doc = new PdfDocument();
PdfPage page = doc.Pages.Add();
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
PdfDocument pdfDocument = htmlToPdfConverter.Convert(
html: "<html><body>" +
"<span style='font-size: 24px;font-family:\"Times New Roman\"'>Sample content with Times نص بسيط </span><br/>" +
"<span style='font-size: 24px;font-family: \"Arial\"';>Sample content with Arial نص بسيط </span>" +
"</body></html>",
baseurl: "");
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
pdfDocument.Close();
return stream.ToArray();
} catch (Exception e) {
Console.WriteLine(e);
throw e;
}
}
}
}


But the issue is, blink engine on AWS just ignores the font styles specified in the sample HTML. I used 'Arial' and 'Times New Roman' for the different spans, but they are displayed in some other font. And that font doesn't probably support arabic characters, so this is the output I am getting.



My project file looks like this:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Aws" Version="22.2.5" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.0" />
</ItemGroup>
</Project>

Is there anybody experienced the same issue before? I really appreciate any tip to make this work.

Thanks.


6 Replies

SN Santhiya Narayanan Syncfusion Team August 4, 2023 02:10 PM UTC

Currently we re checking the reported issue on our end and will update the further details on Aug 8th,2023.



SN Santhiya Narayanan Syncfusion Team August 8, 2023 02:02 PM UTC

The reported text preservation issue occurs due to the required font is not available in the AWS lambda environment. The converter will automatically fallbacks to the system's available font. So the text may be rendered differently based on the fallback font. We have to include the required supported font file in our AWS environment. The arialunicodems.ttf font supports to handle most of unicode characters, so we have included the fonts to render the characters correctly. Kindly please include the required supported font files to your AWS environment like below steps on your end and let us know the result.

Please create a /fonts folder in your project and placed all the needed font files (in our case arialunicodems.ttf). Then create a config file for font configuration by using below commands and placed inside that fonts folder,

<?xml version="1.0"?>

<!DOCTYPE fontconfig SYSTEM "fonts.dtd">

<fontconfig>

   <dir>/var/task/fonts/</dir>

   <cachedir>/tmp/fonts-cache/</cachedir>

   <config></config>

</fontconfig>



And include the below environment variable in your AWS lambda function,

FONTCONFIG_FILE = /var/task/fonts.conf



We have attached the runnable AWS Lambda sample with this solution for your reference, please find the below link,
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/AWSLambdaDockerContainer443714913
Output: https://www.syncfusion.com/downloads/support/directtrac/general/pd/file638271098138637929-2082575647

Please try the above solution on your end and let us know the result.



AF Alper Filiz August 11, 2023 10:05 AM UTC

Hello,

Special thanks for your support. With your suggestion, I overcame the charset issue, and the arabic letters are displayed correctly in the output now.

However, font is still not correctly displayed.

Image_7559_1691703470279

Image_2244_1691703412985

I ran the same code both on my local environment and AWS. The first is the output of the one running on AWS, and second one is taken from the output generated on my local.

The font difference can be seen here clearly. Despite I provided and specified the "Arial" font, the output is printed with different font on AWS.

The font file I used can be found in the attachments. When I install the font on mac, it displays the characters properly in fontbook. So we know the font file is correct.

I am already glad with the solution you provided. It will be extra appreciated if you can suggest a solution for this as well :)



Attachment: arial.ttf_17b5bb08.zip



SN Santhiya Narayanan Syncfusion Team August 11, 2023 02:14 PM UTC

We have checked the reported behavior with HTML String to PDF in AWS lambda function. The AWS lambda environment default supported font is “DejaVu”. If the provided font is not available, then The converter will automatically fallbacks to the system's available font. To overcome this substitute font behavior, we have to include the supported font file in the AWS environment “/usr/share/fonts/truetype/” location using docker commands. We have included the required font and ensured the output document, its font renders correctly.


Please refer the below docker command to copying the required to system folder and output screenshot,


RUN yum -y install fontconfig

COPY fonts /usr/share/fonts/truetype/

 

RUN fc-cache -f -v


We have attached the modified sample with output document for your reference, please find the below link.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/AWSLambdaDockerContainerModified-1885088593
Output: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output-279581168
Fonts: https://www.syncfusion.com/downloads/support/directtrac/general/ze/fonts1549013305


Please try the above solution on your end and let us know the result.



AF Alper Filiz August 18, 2023 07:24 AM UTC

Hello,


Tried to adopt this approach, but I couldn't make it work. Because we have a ci/cd infrastructure creating lambdas from zip files instead of docker images, and I don't have a permission to change this unfortunately.


This is fine though, because fixing the font family is not a mandatory change for us. Our prior concern was displaying the arabic letters properly which is solved thanks to your help. We appreciate your support.



SN Santhiya Narayanan Syncfusion Team August 21, 2023 01:28 PM UTC

Thanks for the update. We glad to know that your has been resolved.


Loader.
Up arrow icon