Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got DocumentException Arial with Cp1252 is not recognized

Tags:

java

itextpdf

the last months I generates thousends of pdfs (on Windows 7 while developing, OpenSuse while test & production) with font Courier.

Now I got the goal to use Arial. So I switched the font name in our property files and i got this exception:

Stacktrace snippet: com.itextpdf.text.DocumentException: Font 'Arial' with 'Cp1252' is not recognized. at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:708) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:615) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:450)

After googleing a while I found a solution. I just have to use "Arial.ttf" (like the filename) in the settings. But after that, I got another exception:

java.io.IOException: Arial.ttf not found as file or resource.
  at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:224)
  at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:173)
  at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:147)
  at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:625)
  at com.itextpdf.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:369)
  at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:699)
  at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:615)
  at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:450)

So the hint there is, that itextpdf is looking for a file right now. So I just typed the filename C:\\Windows\\Fonts\\Arial.ttf into our config. Now it works!

But the goal is just to use "Arial" as the fontname. Is it possible?

Thanx a lot!

like image 421
Mirko Avatar asked Feb 16 '23 09:02

Mirko


1 Answers

It's not possible to just use the name Arial, because Arial isn't listed as one of the 14 standard Type 1 fonts (whereas Courier is one of those 14).

If you want to use Arial, you'll always need to refer to the arial.ttf file, and yes, that can mean that you need to provide c:/windows/fonts/arial.ttf as a path.

If you want to make your application platform-independent, you can ship the font with your code as a resource, and use the path to that resource.

Another solution, is to use the FontFactory and to register all fonts. Note that the initial call to register fonts can take some time, as FontFactory will search your hard disk for as many fonts as it can find looking at directories that may contains font files (depending on the OS). Take a look at the FontFactoryExample and experiment with the register(), registerDirectory() and registerDirectories() methods.

like image 127
Bruno Lowagie Avatar answered Feb 20 '23 11:02

Bruno Lowagie