Hi, We are trying to provide few font substitutes during PDF conversion, but even after providing the font files the PDF is generated using “Dejavu Sans”.
Code snippet:
public byte[] generatePdfUsingGroupDocs(byte[] contentInBytes, boolean showDocTitle, String title, String fileExtensionType, String customFontsFolderName) throws IOException {
long startTime = System.currentTimeMillis();
List<String> customFontDirectoryPathList = new ArrayList<>();
customFontDirectoryPathList.add(defaultFontsPath);
logger.info("custom fonts directory: {}", customFontsVolumePath + "/" + customFontsFolderName);
if (isCustomFontsEnabled) {
String customFontsDirectoryPath = customFontsVolumePath + "/" + customFontsFolderName;
File directory = new File(customFontsDirectoryPath);
File[] files = directory.listFiles();
if (files != null && files.length > 0) {
logger.info("{} custom fonts present in directory {} with file names {}",
files.length, customFontsDirectoryPath, Arrays.stream(files)
.map(File::getName)
.collect(Collectors.joining(",")));
} else {
logger.info("files is null/empty in directory {}", customFontsDirectoryPath);
}
customFontDirectoryPathList.add(customFontsDirectoryPath);
}
ConverterSettings converterSettings = new ConverterSettings();
converterSettings.setFontDirectories(customFontDirectoryPathList);
ConverterSettingsProvider converterSettingsProvider = () -> converterSettings;
final LoadOptions loadOptions;
if (fileExtensionType != null) {
switch (fileExtensionType.toLowerCase()) {
case DOCX:
loadOptions = new WordProcessingLoadOptions();
// Below is required for adding Tags in the output PDF document
((WordProcessingLoadOptions) loadOptions).setPreserveDocumentStructure(true);
//Add the font substitutes for default fonts
((WordProcessingLoadOptions) loadOptions).setFontSubstitutes(createFontSubstitution());
logger.info("Default Font Substitution flag: {}",
((WordProcessingLoadOptions) loadOptions).getAutoFontSubstitution());
break;
case PPTX:
loadOptions = new PresentationLoadOptions();
break;
default:
throw new PdfGenerationException("Invalid file extension");
}
} else {
throw new PdfGenerationException("Input fileExtension Type is null");
}
logger.info("Office document size in bytes: {}", contentInBytes.length);
InputStream is = new ByteArrayInputStream(contentInBytes);
title = isBase64Encoded(title) ? new String(Base64.decodeBase64(title), StandardCharsets.UTF_8) : title;
try (Converter converter = new Converter(() -> is, () -> loadOptions, converterSettingsProvider)) {
try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) {
PdfConvertOptions convertOptions = new PdfConvertOptions();
PdfOptions pdfOptions = convertOptions.getPdfOptions();
pdfOptions.getFormattingOptions().setDisplayDocTitle(showDocTitle);
PdfDocumentInfo pdfDocumentInfo = pdfOptions.getDocumentInfo();
pdfDocumentInfo.setTitle(title);
if(converterSettingsProvider.get() != null && converterSettingsProvider.get().getFontDirectories() != null) {
logger.info("Font directories set for GroupDocs conversion: {}",
String.join(",", converterSettingsProvider.get().getFontDirectories()));
}
converter.convert(() -> ms, convertOptions);
byte[] outputPdfBytes = ms.toByteArray();
long endTime = System.currentTimeMillis();
logger.info("Generated PDF content size in bytes: {}", outputPdfBytes.length);
logger.info(
"Office document to PDF conversion using GroupDocs completed in {}ms", (endTime - startTime));
return outputPdfBytes;
}
} catch (IOException e) {
System.out.println(e.getMessage());
throw e;
}
}
private List<FontSubstitute> createFontSubstitution(){
List<FontSubstitute> fontSubstitutes = new ArrayList<>();
fontSubstitutes.add(FontSubstitute.create("Arial", "Arimo"));
fontSubstitutes.add(FontSubstitute.create("Carlito", "Calibri"));
fontSubstitutes.add(FontSubstitute.create("Caladea", "Cambria"));
return fontSubstitutes;
}
Attaching the input docx file and generated PDF for reference.
Note : The Arial font is getting embedded in the PDF but not applied to the text.
DGBPTest_FontSubs_11.docx (11.7 KB)
DGBPTest_FontSubs_11.pdf (24.6 KB)