Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Oct 11, 2024 7:27 pm

This issue is related to private font directories in Spire.Xls for Python. It seems that when we generate two documents in different threads but accessing the same font, the second thread to access it crashes the process. The error message is below:

Code: Select all
Unhandled Exception: System.ArgumentException: Argument_AddingDuplicate__, /home/eric/documents/demonstration/calefy-backend/insurtech_backend/core/tmp/fonts-29bac59f-b766-4d14-b244-c858d55a80fe - en/Roboto/Roboto-Light.ttf, /home/eric/documents/demonstration/calefy-backend/insurtech_backend/core/tmp/fonts-29bac59f-b766-4d14-b244-c858d55a80fe - en/Roboto/Roboto-Light.ttf
   at System.Collections.Hashtable.Insert(Object, Object, Boolean) + 0x38b
   at spropj.spra() + 0x1dd
   at Spire.Xls.Workbook.set_CustomFontFileDirectory(String[]) + 0xeb
   at Spire.Xls.AOT.NLWorkbook.Workbook_set_CustomFontFileDirectory(IntPtr, IntPtr, Int32) + 0x5c


As you can see, we've even tried to create a custom font folder (in this case, fonts-29bac59f-b766-4d14-b244-c858d55a80fe - en) for each generated document, but that doesn't appear to solve the issue. We set the font in

Code: Select all
external_quote_proposal = Workbook()external_quote_proposal.LoadFromFile(str(english_external_destination_path))
external_quote_proposal.CustomFontFileDirectory = [str(this_run_font_file_directory_en)]


The above error is the one that we encounter when this is run on Ubuntu 22.04.1. Interestingly, we get a different error when run on a Docker container based off of the Python 3.9 image at https://hub.docker.com/_/python. In that case, we get the following error (which presumably is related):

Code: Select all
Unhandled Exception: System.InvalidCastException: Arg_InvalidCastException
   at System.Runtime.TypeCast.CheckCastClass(MethodTable*, Object) + 0x2b
   at SkiaSharp.SKAbstractManagedStream.ReadInternal(IntPtr s, Void* context, Void* buffer, IntPtr size) + 0xcc
   at Spire.Xls.Base!<BaseAddress>+0x1fe1150


We had a similar issue earlier which was tracked as SPIREDOC-10645.

I'm working on an MWE, but do you have any ideas why this is occurring or what I can do to avoid it?

ericcampbell
 
Posts: 8
Joined: Thu Jun 27, 2024 10:39 pm

Sat Oct 12, 2024 3:28 am

Hello,

Thanks for your inquiry. I tested it on Ubuntu 22 using the following code. I first tested version 14.2.2, which reported an error at runtime:
Code: Select all
spire.xls.common.SpireException: Argument_AddingDuplicate__, /root/amin/python/font/Roboto-Light.ttf, /root/amin/python/font/Roboto-Light.ttf:   at System.Collections.Hashtable.Insert(Object, Object, Boolean) + 0x38b
   at sprsuz.spra() + 0x1dd
   at Spire.Xls.Workbook.set_CustomFontFileDirectory(String[]) + 0xeb
   at Spire.Xls.AOT.NLWorkbook.Workbook_set_CustomFontFileDirectory(IntPtr, IntPtr, Int32, IntPtr) + 0x71

Then I updated to the latest version 14.7.3 for testing, and the results showed that everything was normal. If you are not using the latest version, please update and try again. If the issue persists after the update, please provide us with your complete input code and input Excel documents for investigation. Thank you in advance.
Code: Select all
# Define thread function
def createXls(fileName): 
  # Create a workbook object
  workbook = Workbook()
  PATH_TO_FONT = "/root/amin/python/font"
  workbook.CustomFontFileDirectory = [PATH_TO_FONT]
# Get the first worksheet
  sheet = workbook.Worksheets[0]
# set font
  row = 1
  sheet.Range[row, 1].Value = "FontName"
  sheet.Range[row, 2].Value = "Roboto-Light"
  sheet.Range[row, 2].Style.Font.FontName = "Roboto-Light"
# Save workbook
  workbook.SaveToFile("output/"+fileName, ExcelVersion.Version2016)
  workbook.Dispose()
 # Create two threads 
thread1 = threading.Thread(target=createXls, args=("thread1.xlsx",))
thread2 = threading.Thread(target=createXls, args=("thread2.xlsx",))

# Start thread
thread1.start() 
thread2.start()

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 285
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.XLS