Hi,
We are using .Net Aspose Word to mailmerge, and we observe some issue with the Thai language document.
The texts in Thai language are converted to some unrecognized characters during merge, and this happens only in Linux server (Azure Kubernetes), while it is working fine on local Windows machine.
Input (Docx)
Merged Output (Pdf)
Below is the C# code we use to mailmerge
IDictionary<string, object> parameters1 = new Dictionary<string, object> { { "PERSONNAME", "Jagan" } };
IEnumerable<TemplateSequenceParameter> sequenceParameters1 = new List<TemplateSequenceParameter>
{
new TemplateSequenceParameter
{
Key = "CONTACTTYPE",
Values = new List<object> { "home", "office", ... }
},
new TemplateSequenceParameter
{
Key = "EMPLOYEEFIRSTNAME",
Values = new List<object> { "john", "peter", ... }
}
};
using (MemoryStream stream = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings()))
{
writer.WriteStartDocument();
writer.WriteStartElement(DataSourceName);
foreach (KeyValuePair<string, object> parameter in parameters)
{
writer.WriteElementString(parameter.Key, parameter.Value.ToString());
}
int colCount = sequenceParameters.Count();
if (colCount > 0)
{
int rowCount = sequenceParameters.First().Values.Count();
for (int rowInd = 0; rowInd < rowCount; rowInd++)
{
writer.WriteStartElement(DataSourceSequenceParameterPrefix);
for (int colInd = 0; colInd < colCount; colInd++)
{
writer.WriteElementString(sequenceParameters.ElementAt(colInd).Key, sequenceParameters.ElementAt(colInd).Values.ElementAt(rowInd).ToString());
}
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
stream.Position = 0;
XmlDataSource dataSource = new XmlDataSource(stream);
ReportingEngine engine = new ReportingEngine();
engine.Options |= ReportBuildOptions.AllowMissingMembers;
engine.Options |= ReportBuildOptions.RemoveEmptyParagraphs;
engine.BuildReport(document, dataSource, DataSourceName);
using (MemoryStream ms = new MemoryStream(4096))
{
document.LayoutOptions.TextShaperFactory = HarfBuzzTextShaperFactory.Instance;
document.Save(ms, GetSaveOptions("pdf"));
byte[] documentContent = ms.ToArray();
return documentContent;
}
}
private static SaveOptions GetSaveOptions(DocumentFormat outputFormat)
{
switch (outputFormat)
{
case "pdf":
PdfSaveOptions options = new PdfSaveOptions();
options.MemoryOptimization = true;
options.ImageCompression = PdfImageCompression.Jpeg;
options.JpegQuality = 0;
options.UseHighQualityRendering = false;
options.DmlRenderingMode = DmlRenderingMode.Fallback;
options.DmlEffectsRenderingMode = DmlEffectsRenderingMode.None;
return options;
}
}
Input file -
PND91SPECIALTAX_FinalUpdated (1) (2) (1).docx (349.4 KB)
Output merged file -
PND91SPECIALTAX_Merged.pdf (157.4 KB)
Below is the docker file we use to publish
FROM mcr.microsoft.com/dotnet/aspnet:8.0
# Install dependencies
RUN sed -i 's/^Components: main$/& contrib/' /etc/apt/sources.list.d/debian.sources
RUN apt-get update \
&& apt-get install -y \
libfreetype6 \
libfontconfig1 \
libgif-dev autoconf libtool automake build-essential gettext libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev \
apt-utils libgdiplus libc6-dev \
ttf-mscorefonts-installer fontconfig \
&& fc-cache -f -v \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY ./dist /app
ENV ASPNETCORE_URLS=http://+:5000
ENTRYPOINT [ "dotnet", "Hrx.DocGen.WebApi.dll" ]
please note that I added the below line to support Thai language
document.LayoutOptions.TextShaperFactory = HarfBuzzTextShaperFactory.Instance;
After that mailMerge is throwing below exception while saving documents.
{
"ClassName": "System.Exception",
"Message": "Text shaper factory failed to return text shaper for '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', face index '0'",
"Data": null,
"InnerException": {
"ClassName": "System.DllNotFoundException",
"Message": "Unable to load shared library 'harfbuzz' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: \n/app/harfbuzz.so: cannot open shared object file: No such file or directory\n/app/libharfbuzz.so: cannot open shared object file: No such file or directory\n/app/harfbuzz: cannot open shared object file: No such file or directory\n/app/libharfbuzz: cannot open shared object file: No such file or directory\n",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at \u000F\u000F.\u0002.d(IntPtr d, UInt32 v, b c, IntPtr t, IntPtr n)\n at \u000F\u000F.\u0002(IntPtr \u0002, UInt32 \b, b \u0005, IntPtr \u0006, IntPtr \u0003)\n at \u000E\u000F.\u0002(Byte[] \u0002, Int32 \b)\n at Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.GetTextShaper(String fontPath, Int32 faceIndex)\n at Aspose.Words.Shaping.BasicTextShaperCache.Aspose.Words.Shaping.ITextShaperFactory.GetTextShaper(String fontPath, Int32 faceIndex)\n at MUi.d(String[] d, QU5 v, ITextShaperFactory c, UnicodeScript t, Boolean n, VariationAxisCoordinate[] B)",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146233052,
"Source": "Aspose.Words.Shaping.HarfBuzz",
"WatsonBuckets": null,
"TypeLoadClassName": null,
"TypeLoadAssemblyName": null,
"TypeLoadMessageArg": null,
"TypeLoadResourceID": 0
},
"HelpURL": null,
"StackTraceString": " at MUi.d(String[] d, QU5 v, ITextShaperFactory c, UnicodeScript t, Boolean n, VariationAxisCoordinate[] B)\n at MUi.d(kUJ d, ITextShaperFactory v)\n at YUi.n(kUJ d)\n at YUi.d(kUJ d)\n at kUJ.kUJiFl3()\n at kUJ.cU7iFp()\n at pUq.cU7iFp()\n at vU5.b()\n at vU5.d(Boolean d, Int32 v)\n at vU5.d(Boolean d)\n at OU5.d(vU5 d)\n at OU5.d(mU5 d, Int32 v)\n at oUZ.d(mU5 d)\n at oUZ.d(mU5 d, SUq v)\n at GUZ.d(mU5 d, SUq v)\n at GUZ.v(mU5 d, Int32 v, Boolean c)\n at zU7.v()\n at zU7.v(wU7 d, Int32 v, Boolean c)\n at zUJ.v()\n at zUJ.v(nU7 d, Int32 v, Boolean c, Boolean t)\n at pU7.c()\n at pU7.v(jU7 d, Int32 v)\n at pU7.d(jU7 d, Int32 v)\n at GU5.v(jU7 d)\n at GU5.c()\n at GU5.fUZiFd(cU7 d)\n at rU7.d(cU7 d)\n at PU7.d(JU5 d)\n at RU7.m()\n at tUi.c()\n at Aspose.Words.Document.UpdatePageLayout()\n at jUC.v(nCQ d, FP v)\n at jUC.YUYiFd(nCQ d)\n at wUa.wUaiFd(nCQ d)\n at Aspose.Words.Document.d(nCQ d)\n at Aspose.Words.Document.d(Stream d, String v, SaveOptions c)\n at Hrx.DocGen.Infrastructure.Services.WordsReportBuilderService.GetDocumentData(DocumentGenerationModel generationModel, Document document) in /home/vsts/work/1/s/src/Hrx.DocGen.Infrastructure/Services/WordsReportBuilderService.cs:line 374",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146233088,
"Source": "Aspose.Words",
"WatsonBuckets": null
}
Can you please suggest how to solve this issue