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.

Tue Jan 10, 2023 4:09 am

I am using spire.xls.free version 5.1.0 in my java application which is converting the excel file to pdf. The conversion code was copied from your website's documentation only.

But the pdf generated are empty. Because I am getting below error. Can you please help me here? I want to copy all the fonts in my projects directory and have my code look into that custom directory to view all the font files. Could you please provide me the list of font files that needs to be added as well as the code to be added to my function to look at the custom directory for fonts. The error occurs when I call workbook.saveToFile function of yours.

java.lang.IllegalStateException: Cannot found font[Arial] installed on the system.
at com.spire.xls.packages.sprNKB.spr (Unknown Source)
at com.spire.xls.packages.sprSJB.spr (Unknown Source)
at com.spire.xls.packages.sprSJB.spr (Unknown Source)
at com.spire.xls.packages.sprLKa.spr (Unknown Source)
at com.spire.xls.packages.sprIcA.spr (Unknown Source)
at com.spire.xls.packages.sprAsa.spr (Unknown Source)
at com.spire.xls.packages.sprAsa.spr (Unknown Source)
at com.spire.xls.packages.sprAsa.spr (Unknown Source)
at com.spire.xls.packages.sprAsa.spr (Unknown Source)
at com.spire.xls.packages.sprFua.spr (Unknown Source)
at com.spire.xls.packages.sprFua.spr (Unknown Source)
at com.spire.xls.packages.sprHPA.spr (Unknown Source)
at com.spire.xls.packages.sprHPA.spr (Unknown Source)
at com.spire.xls.core.spreadsheet.XlsWorkbook.spr (Unknown Source)
at com.spire.xls.core.spreadsheet.XlsWorkbook.spr (Unknown Source)
at com.spire.xls.Workbook.saveToFile(Unknown Source)
at com.scm.moca.clientstatements.murex.SaaS.ConvertWorkbookToPdf.convertToPdf(ConvertWorkbookToPdf.java:27)

manishak
 
Posts: 2
Joined: Tue Jan 10, 2023 3:32 am

Tue Jan 10, 2023 4:39 am

I tried testing below code too after googling few solutions:
public void convertToPdf(String excelFileLocation) {

PdfDocument.setCustomFontsFolders("/fonts/msttcorefonts/");

String[] customFontDir = {"fonts/msttcorefonts/"};

//Create a Workbook instance
Workbook workbook = new Workbook();

//Load an Excel file
workbook.loadFromFile(excelFileLocation);

//Fit all worksheets on one page
workbook.getConverterSetting().setSheetFitToPage(true);

workbook.setCustomFontFileDirectory(customFontDir);

// workbook.setDefaultFontName(value);

//Save the workbook to PDF
workbook.saveToFile(excelFileLocation.replace("xlsx", "pdf"), FileFormat.PDF);

logger.info("\nPDF Created: " + excelFileLocation.replace("xlsx", "pdf"));

}

I created fonts/msttcorefonts/ in my src/main/resources folder.

But this does not work either. Please help! Its quite urgent for me to deploy the solution. Thanks.

manishak
 
Posts: 2
Joined: Tue Jan 10, 2023 3:32 am

Tue Jan 10, 2023 9:17 am

Hi,

Thanks for your inquiry.
If you want to get the path in the resources folder, you are supposed to call the method: this.getClass().getClassLoader().getResource("fonts/msttcorefonts").getPath();
This will return the path with a prefix “/”, then call path.substring(1) to remove the prefix. you can see the following code and picture and do a test. If you have furter questions, just let us know.

Code: Select all
        String path = Test.class.getClassLoader().getResource("fonts/msttcorefonts").getPath();
        path = path.substring(1);
        String[] pathDir = {path};
        PdfDocument.setCustomFontsFolders(path);
        Workbook wb = new Workbook();
        String excelLocation = "test.xlsx";
        wb.loadFromFile(excelLocation);
        wb.getConverterSetting().setSheetFitToPage(true);
        wb.setCustomFontFileDirectory(pathDir);
        wb.saveToFile(excelLocation.replace("xlsx","pdf"),FileFormat.PDF);

structure.png
structure.png (53.98 KiB) Viewed 1854 times


Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.XLS