@asad.ali I thought that I had this working, but it seems that I’m still unable to get the fonts configured in this AWS Lambda environment.
Originally it seems to blow up like my original question if you do NOT do a FontRepository.Sources.Add
giving it something that can find fonts. When I added a directory that had .ttf
files in it, that seemed to get past it.
After that I was fighting with getting libgdiplus
appropriately configured in the AWS Lambda environment, but I eventually got past that with a lambda layer.
Now I’m still struggling with getting it to find fonts.
I created a fonts
directory and put all of the fonts from another Windows system in there from C:\Windows\fonts
. To test / simulate the AWS Lambda environment I’m using the following:
docker run --rm -it -v $PWD:/sandbox -w /sandbox lambci/lambda:20201112-build-dotnetcore3.1 /bin/bash
Once in there, I have to set LD_LIBRARY_PATH
so I can make libgdiplus
available.
$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sandbox/layers/libgdiplus/lib
$ PATH=$PATH:/sandbox/layers/libgdiplus/bin
To test my function I’m just simply doing a dotnet run
. Here is my routine that I’m testing:
private static void Main(string[] args)
{
var license = new License();
license.SetLicense("Aspose.Pdf.csharp.lic");
var document = new Document("xfa_filled.pdf");
document.Form.Type = FormType.Standard;
document.Save("flattened.aspose.pdf");
}
So the first time you dotnet run
here, you’ll get my original error:
ash-4.2# dotnet run
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at #=z2rshOQYLk8j6LcibgDCnJpx28yCTsFI_C4RCuTw=..ctor(FontCollection #=zbpJ5jZ01eRJL, #=zmQ2IzQQuwJuzpkkhGQkD9qLkjs6dVhmEmA== #=z0AOheY$$hzFL00GNVg==)
at Aspose.Pdf.Forms.Form.#=zvzcuW4Rrz3IejRx3w3uWOIg=(String #=z0hyUue80LjHvYl_jzed_NbM=, String #=zau2CIZoWdQDMkWXapQ==, String #=zAjVfsl__S$HzdzslXw==)
at Aspose.Pdf.Forms.Form.set_Type(FormType value)
at AwsDotnetCsharp.PdfUtils.Main(String[] args) in /sandbox/PdfUtils.cs:line 22
This now requires you to to add the following line:
FontRepository.Sources.Add(new FolderFontSource("/sandbox/msfonts")); // from c:\windows\fonts
Once adding that now you’ll get the following:
bash-4.2# dotnet run
Fontconfig error: Cannot load default config file
Unhandled exception. System.TypeInitializationException: The type initializer for '#=zxi2oiph7E_QrSuk4FyBC7Vo=' threw an exception.
---> System.ArgumentException: Font '?' cannot be found.
at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)
at System.Drawing.FontFamily.GetGdipGenericSansSerif()
at System.Drawing.FontFamily.get_GenericSansSerif()
at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)
at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
at System.Drawing.Font..ctor(String familyName, Single emSize, String systemName)
at System.Drawing.SystemFonts.get_DefaultFont()
at #=zs0HZcWyJMF6QVJnacXvhuXq5u8TC.#=zi4B5ghRwkaRe()
at #=zDxN2KO_gFVoQOlvsVKTX2IzoFHxV.#=z2Ul_ohf_ETfq(String #=zqTiGoxc=, FontStyle #=zKnLz$18=, String #=z6Q_S19KrKRMr)
at #=zDxN2KO_gFVoQOlvsVKTX2IzoFHxV.#=zBECmHXmvWXhm(String #=zqTiGoxc=, Single #=zc5R8OOSA0Npt, FontStyle #=zKnLz$18=, String #=z6Q_S19KrKRMr)
at #=zDxN2KO_gFVoQOlvsVKTX2IzoFHxV.#=zBECmHXmvWXhm(String #=zqTiGoxc=, Single #=zc5R8OOSA0Npt, FontStyle #=zKnLz$18=)
at #=zxi2oiph7E_QrSuk4FyBC7Vo=..cctor()
--- End of inner exception stack trace ---
at #=zxi2oiph7E_QrSuk4FyBC7Vo=..ctor(PointF #=zQqrH5so=, String #=zO7PsOj0=, SizeF #=zDMIZvYg=, Boolean #=znB7xfXqZ88Cb)
at #=zNu0IYqfxsWx2JVHV_g5HBjBrYvH8UblDtSoSA9U=.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zMd93ULWdFZfAmF9ilFPW$TUis3EHPY7Rqg==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zXGtb6kI=, #=z1JZuFYvOARS2SB2Yo7LY0j$Szk1e #=zprqRCoo=)
at #=zNB9TpMtC4hr1l8nt2F$tMwBKSugRnDYeavzjB1c=.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zusO7XRyLVGafakVu8YwhmhcSgAXczv7h5DkAj0U=.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=z2rshOQYLk8j6LcibgDCnJigGUrBsrwJz0w==.#=zcSGo4sRJDOq2(Dictionary`2 #=zAQGKTuUki_YA, XfaParserOptions #=z4Gp8a2g=, #=zMirV2avbYCsTZJj7o9Xx4DD7Bf0CVmaMyA== #=z9cbJbqowxnrv, #=z2rshOQYLk8j6LcibgDCnJpx28yCTsFI_C4RCuTw= #=z$uM8DgiOn3AH)
at #=zA8JgbNdKWulAsBZhVNm$oBDT3RMHxdFFxQ==.#=z0evQ6Lg=(Dictionary`2 #=zAQGKTuUki_YA, XfaParserOptions #=z4Gp8a2g=, #=z2rshOQYLk8j6LcibgDCnJpx28yCTsFI_C4RCuTw= #=z$uM8DgiOn3AH)
at Aspose.Pdf.Forms.Form.#=zvzcuW4Rrz3IejRx3w3uWOIg=(String #=z0hyUue80LjHvYl_jzed_NbM=, String #=zau2CIZoWdQDMkWXapQ==, String #=zAjVfsl__S$HzdzslXw==)
at Aspose.Pdf.Forms.Form.set_Type(FormType value)
at AwsDotnetCsharp.PdfUtils.Main(String[] args) in /sandbox/PdfUtils.cs:line 23
To get past that, in my /sandbox/fonts
directory I added a fonts.conf
and put this in there:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/sandbox/fonts/</dir>
<cachedir>/tmp/fonts-cache/</cachedir>
<include ignore_missing="yes">conf.d</include>
<config></config>
</fontconfig>
As well set setting FONTCONFIG_PATH=/sandbox/fonts
. Now when I fc-list
it shows fonts and this gets me past the error where it’s trying to load the System.Drawing.SystemFonts.DefaultFont
.
However, now I run into my next error where it blows up saying that it cannot find any system fonts (which is probably true…in an AWS Lambda environment there likely aren’t any system fonts installed)
bash-4.2# dotnet run
Unhandled exception. System.TypeInitializationException: The type initializer for '#=zxi2oiph7E_QrSuk4FyBC7Vo=' threw an exception.
---> System.InvalidOperationException: Cannot find any fonts installed on the system.
at #=zDxN2KO_gFVoQOlvsVKTX2IzoFHxV.#=z2Ul_ohf_ETfq(String #=zqTiGoxc=, FontStyle #=zKnLz$18=, String #=z6Q_S19KrKRMr)
at #=zDxN2KO_gFVoQOlvsVKTX2IzoFHxV.#=zBECmHXmvWXhm(String #=zqTiGoxc=, Single #=zc5R8OOSA0Npt, FontStyle #=zKnLz$18=, String #=z6Q_S19KrKRMr)
at #=zDxN2KO_gFVoQOlvsVKTX2IzoFHxV.#=zBECmHXmvWXhm(String #=zqTiGoxc=, Single #=zc5R8OOSA0Npt, FontStyle #=zKnLz$18=)
at #=zxi2oiph7E_QrSuk4FyBC7Vo=..cctor()
--- End of inner exception stack trace ---
at #=zxi2oiph7E_QrSuk4FyBC7Vo=..ctor(PointF #=zQqrH5so=, String #=zO7PsOj0=, SizeF #=zDMIZvYg=, Boolean #=znB7xfXqZ88Cb)
at #=zNu0IYqfxsWx2JVHV_g5HBjBrYvH8UblDtSoSA9U=.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zMd93ULWdFZfAmF9ilFPW$TUis3EHPY7Rqg==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zXGtb6kI=, #=z1JZuFYvOARS2SB2Yo7LY0j$Szk1e #=zprqRCoo=)
at #=zNB9TpMtC4hr1l8nt2F$tMwBKSugRnDYeavzjB1c=.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zusO7XRyLVGafakVu8YwhmhcSgAXczv7h5DkAj0U=.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=zlOJsHZYIDrpPC4nxlyV2ZoIaOtrHRdXL4Q==.#=zSCwT2Do=(#=zrLgNkPePdgvkxIvamT438vMyQnhiPCd1Kg== #=zJS1FR_4=, XmlNode #=zYyovubM=)
at #=z2rshOQYLk8j6LcibgDCnJigGUrBsrwJz0w==.#=zcSGo4sRJDOq2(Dictionary`2 #=zAQGKTuUki_YA, XfaParserOptions #=z4Gp8a2g=, #=zMirV2avbYCsTZJj7o9Xx4DD7Bf0CVmaMyA== #=z9cbJbqowxnrv, #=z2rshOQYLk8j6LcibgDCnJpx28yCTsFI_C4RCuTw= #=z$uM8DgiOn3AH)
at #=zA8JgbNdKWulAsBZhVNm$oBDT3RMHxdFFxQ==.#=z0evQ6Lg=(Dictionary`2 #=zAQGKTuUki_YA, XfaParserOptions #=z4Gp8a2g=, #=z2rshOQYLk8j6LcibgDCnJpx28yCTsFI_C4RCuTw= #=z$uM8DgiOn3AH)
at Aspose.Pdf.Forms.Form.#=zvzcuW4Rrz3IejRx3w3uWOIg=(String #=z0hyUue80LjHvYl_jzed_NbM=, String #=zau2CIZoWdQDMkWXapQ==, String #=zAjVfsl__S$HzdzslXw==)
at Aspose.Pdf.Forms.Form.set_Type(FormType value)
at AwsDotnetCsharp.PdfUtils.Main(String[] args) in /sandbox/PdfUtils.cs:line 26
So I’m at a loss in trying to understand how I can get this to work. If I yum install -y dejavu-sans-fonts
, it will work. But you can’t do that in a lambda function because you can’t control that. The only way you can control it is via lambda layers (which is what I am using to get the libgdiplus
layers as well as the fonts
layer).
Please advise.