.
This commit is contained in:
@@ -28,18 +28,45 @@ public class ResearchPdfService {
|
||||
* Убедитесь, что файл шрифта DejaVuSans.ttf находится в /resources/fonts/
|
||||
*/
|
||||
public ResearchPdfService() {
|
||||
BaseFont resolvedBaseFont;
|
||||
try {
|
||||
// Загружаем шрифт, поддерживающий кириллицу. Это КЛЮЧЕВОЕ изменение.
|
||||
this.baseFont = BaseFont.createFont("fonts/DejaVuSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
|
||||
this.titleFont = new Font(baseFont, 18, Font.BOLD);
|
||||
this.headingFont = new Font(baseFont, 14, Font.BOLD);
|
||||
this.subheadingFont = new Font(baseFont, 12, Font.BOLD);
|
||||
this.normalFont = new Font(baseFont, 10, Font.NORMAL);
|
||||
this.smallFont = new Font(baseFont, 8, Font.ITALIC);
|
||||
this.linkFont = new Font(baseFont, 8, Font.UNDERLINE);
|
||||
// Пытаемся загрузить шрифт из classpath (работает внутри fat JAR)
|
||||
java.io.InputStream is = Thread.currentThread().getContextClassLoader()
|
||||
.getResourceAsStream("fonts/DejaVuSans.ttf");
|
||||
if (is != null) {
|
||||
try (is) {
|
||||
byte[] fontBytes = is.readAllBytes();
|
||||
resolvedBaseFont = BaseFont.createFont(
|
||||
"DejaVuSans.ttf",
|
||||
BaseFont.IDENTITY_H,
|
||||
BaseFont.EMBEDDED,
|
||||
false,
|
||||
fontBytes,
|
||||
null);
|
||||
}
|
||||
} else {
|
||||
// Фолбэк: пробуем загрузить по файловому пути (на случай, если ресурс не
|
||||
// упакован)
|
||||
resolvedBaseFont = BaseFont.createFont("fonts/DejaVuSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to load font", e);
|
||||
// Фолбэк на стандартный шрифт, чтобы не падал бином (кириллица может
|
||||
// отображаться некорректно)
|
||||
try {
|
||||
resolvedBaseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
|
||||
System.err.println("ResearchPdfService: DejaVuSans.ttf not found. Falling back to Helvetica.");
|
||||
} catch (Exception inner) {
|
||||
throw new RuntimeException("Failed to load font", e);
|
||||
}
|
||||
}
|
||||
|
||||
this.baseFont = resolvedBaseFont;
|
||||
this.titleFont = new Font(baseFont, 18, Font.BOLD);
|
||||
this.headingFont = new Font(baseFont, 14, Font.BOLD);
|
||||
this.subheadingFont = new Font(baseFont, 12, Font.BOLD);
|
||||
this.normalFont = new Font(baseFont, 10, Font.NORMAL);
|
||||
this.smallFont = new Font(baseFont, 8, Font.ITALIC);
|
||||
this.linkFont = new Font(baseFont, 8, Font.UNDERLINE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user