docx4j icon indicating copy to clipboard operation
docx4j copied to clipboard

How To Embed New Font into Generated PDF

Open lkgamor opened this issue 8 months ago • 1 comments

Hello! Thanks for such a lovely library API.

I am working on a Java EE project which is deployed using Wildfly. For some days now, I have been working on trying to embed the CordoS and CordoA fonts into the project so that PDFs generated by the application will have their text disaplayed with these fonts. The texts inside my .docx templates have the font type CordoS and some CordoA (this is why I need to embed them).

I have searched online on how to include these fonts into my project, so that the resulting PDFs I generate will reflect the same fonts. I haven't been able to find any related solutions and I get a fallback font anytime I generate PDFs.

So far: I have downloaded and edited the MicrosoftFonts.xml file to include the two new fonts as shown below:

   <font name="CorpoA" filename="CORPOA.ttf"  mac="CorpoA Regular.ttf" clearTypeCollection="true">
	<bold filename="CORPOAB.ttf" mac="CorpoA Bold.ttf" />
   </font>

   <font name="CorpoS" filename="CORPOS.ttf"  mac="CorpoS Regular.ttf" clearTypeCollection="true">
	<bold filename="CORPOSB.ttf" mac="CorpoS Bold.ttf" />
   </font>

I added the .tff files for both CordoS and CordoA to a folder called /fonts under src/main/resources/.

I also added the customized MicrosoftFonts.xml file into this same src/main/resources/fonts directory.

Next, I used the below config option in the docx4j.properties file to specify the path to the customized MicrosoftFonts.xml file.

docx4j.fonts.microsoft.MicrosoftFonts=fonts/MicrosoftFonts.xml

Not sure if this following step is relevant, but I also edited the docx4j-ImportXHTML.properties with the config below

docx4j-ImportXHTML.fonts.default.sans-serif=CorpoS, CorpoA, Calibri, Arial, sans-serif

I do not know what to do next to finally get the docx4j library to recognize the two new fonts I've added and ensure my generated PDFs display the right font-family. I am sure this should be possible without making any Java code changes.

Please help me out!

lkgamor avatar Jun 06 '24 01:06 lkgamor