We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Font Arial was not found: com.aspose.pdf.exceptions.FontNotFoundException

Hi, I am running the evaluation version of Aspose Java PDF on AWS Lambda.

Font Arial was not found: com.aspose.pdf.exceptions.FontNotFoundException
class com.aspose.pdf.exceptions.FontNotFoundException: Font Arial was not found
com.aspose.pdf.FontRepository.findFont(Unknown Source)
com.aspose.pdf.SignatureField.m1(Unknown Source)
com.aspose.pdf.SignatureField.sign(Unknown Source)
com.aspose.pdf.facades.PdfFileSignature.m1(Unknown Source)
com.aspose.pdf.facades.PdfFileSignature.save(Unknown Source)
aspose.intelleigen.com.IWFAsposePDF.handleRequest(IWFAsposePDF.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
at com.aspose.pdf.FontRepository.findFont(Unknown Source)
at com.aspose.pdf.SignatureField.m1(Unknown Source)
at com.aspose.pdf.SignatureField.sign(Unknown Source)
at com.aspose.pdf.facades.PdfFileSignature.m1(Unknown Source)
at com.aspose.pdf.facades.PdfFileSignature.save(Unknown Source)
at aspose.intelleigen.com.IWFAsposePDF.handleRequest(IWFAsposePDF.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)

EDIT:
I found this… https://docs.aspose.com/display/pdfjava/use+pdf+core+fonts
How do I change the font used for signatures from Ariel to Helvetica?

@ngzhongcai,

Please visit this link to change font Replace fonts in existing PDF file.

Please feel free to let us know whenever you require any further assistance.

Hi Naeem,

Solution provided replace font for the entire file.
I only face this issue when adding a digital signature.
How do I add “Arial” font to the FontRepository?

@ngzhongcai,

Please use below code to change font

try
        {
            originalFont = FontRepository.findFont("Ariel");
        }
        catch (com.aspose.pdf.exceptions.FontNotFoundException e)
        {
            FontRepository.getSubstitutions().add(new SimpleFontSubstitution("Ariel", "Helvetica"));    
        }

Am sorry to bother with repeated questions…
I tried setting the FontRepository.getSu…add… method right at the top…

FontRepository.getSubstitutions().add(new SimpleFontSubstitution("Ariel", "Helvetica"));
Font font= FontRepository.findFont("Ariel");

The second line caused the same exception.
I thought the first line would have set the font in place.

I also tried uploading a TFF font resource together with the JAR file. And then adding it to the FontRepository by using the “document.setLocalFontPaths” method. Again this didn’t work. And the same exception arises. Am hoping you can give some help…

This doesn’t work for me also…

Font font= FontRepository.openFont(“fonts/Arial.ttf”);
font.setEmbedded(true);

@ngzhongcai,

Please make sure, you are using full path for open font like “C://FontName.ttf”. Below example demonstrates how to open font and replace the font of text of first page.

              // Open font
    		Font font = FontRepository.openFont("C:\\WINDOWS\\Fonts\\arial.ttf");

    		// Open document
    		Document doc = new Document("E:\\temp\\input.pdf");
    		// Create TextFragmentAbsorber object to find all "Lorem ipsum" text occurrences
    		TextFragmentAbsorber absorber = new TextFragmentAbsorber("Lorem ipsum");

    		// Accept the absorber for first page
    		doc.getPages().get_Item(1).accept(absorber);

    		// Change font of the first text occurrence
    		absorber.getTextFragments().get_Item(1).getTextState().setFont ( font);

    		// Save document
    		doc.save("E:\\temp\\output.pdf");

input.pdf (1.3 MB)
output.pdf (1.3 MB)

Thank you Naeem,
Am terribly sorry to trouble you.
But I am at a loss.

Below is my code snippet. I am trying to digitally sign a PDF document that is empty. It’s a blank white page. At nowhere is there any text snippets. “FontNotFoundException” happens when I try to save the pdfFileSignature.

    byte[] pdfBytes= Base64.getDecoder().decode((String)event.get("body>>pdf"));
    InputStream pdfInputStream= new ByteArrayInputStream(pdfBytes);
    ByteArrayOutputStream pdfOutputStream= new ByteArrayOutputStream();
    PdfFileSignature pdfFileSignature= new PdfFileSignature();
    pdfFileSignature.bindPdf(pdfInputStream);

    byte[] imageBytes= Base64.getDecoder().decode((String)event.get("body>>image"));
    InputStream imageInputStream= new ByteArrayInputStream(imageBytes);
    pdfFileSignature.setSignatureAppearanceStream(imageInputStream);

    byte[] keyBytes= Base64.getDecoder().decode((String)event.get("body>>key"));
    InputStream keyInputStream= new ByteArrayInputStream(keyBytes);
    PKCS1 key= new PKCS1(keyInputStream, (String)event.get("user>>sigPassword"));
    Rectangle rectangle= new Rectangle(100, 200, 300, 300);
    pdfFileSignature.sign(1, true, rectangle, key);

    Font font= FontRepository.openFont("fonts/Arial.ttf");
    TextFragmentAbsorber absorber= new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
    pdfFileSignature.getDocument().getPages().get_Item(1).accept(absorber);
    System.out.println(absorber.getTextFragments().size());

    absorber.getTextFragments().get_Item(1).getTextState().setFont(font);

    pdfFileSignature.save(pdfOutputStream);

Hi Naeem,

I finally got it working. Thank you for the patience!
I added the .tff font file into resource folder.
Lambda’s designated HOME is “/var/task/”.

List list= Document.getLocalFontPaths();
list.add("/var/task/fonts/");
Document.setLocalFontPaths(list);

@ngzhongcai,

We are glad to hear that your problem is solved. For any further query, please feel free to contact.

Hi @ngzhongcai
How did you add extra fonts ?
I am also working on AWS Lambda and getting FontNotFoundException.

list.add("/var/task/fonts/");

This is just a path to the folder.
Are the fonts actually already there ?

Let me know .
Thanks

@shamikjv

Thanks for posting your inquiry.

Please note that the required font should be present/installed on the machine where you are using the API. Please make sure that fonts are present in the directory which path you are adding to the list. Please feel free to let us know if issue still persists.