8383 * - Enhanced PDF options (watermarks, encryption, metadata)
8484 */
8585public class TemplateRenderUtil {
86- private static final Logger logger = LoggerFactory .getLogger (TemplateRenderUtil .class );
86+ private static final Logger log = LoggerFactory .getLogger (TemplateRenderUtil .class );
8787 private static final List <TemplateLoader > additionalLoaders = new ArrayList <>();
8888 private static Configuration freemarkerConfig = createFreemarkerConfig ();
8989 private static final Map <String , Template > templateCache = new ConcurrentHashMap <>();
@@ -119,7 +119,7 @@ private static Configuration createFreemarkerConfig() {
119119 TemplateLoader [] loaders = loaderList .toArray (new TemplateLoader [0 ]);
120120 cfg .setTemplateLoader (new MultiTemplateLoader (loaders ));
121121 } catch (IOException e ) {
122- logger .warn ("Filesystem loader not available, falling back to classpath only" , e );
122+ log .warn ("Filesystem loader not available, falling back to classpath only" , e );
123123
124124 if (additionalLoaders .isEmpty ()) {
125125 cfg .setTemplateLoader (ctl );
@@ -151,11 +151,11 @@ private static void resetConfiguration() {
151151 try {
152152 freemarkerConfig .setSharedVariable (entry .getKey (), entry .getValue ());
153153 } catch (TemplateException e ) {
154- logger .error ("Failed to set shared variable: {}" , entry .getKey (), e );
154+ log .error ("Failed to set shared variable: {}" , entry .getKey (), e );
155155 }
156156 }
157157
158- logger .info ("FreeMarker configuration has been reset" );
158+ log .info ("FreeMarker configuration has been reset" );
159159 }
160160
161161 /**
@@ -172,7 +172,7 @@ public static void addSharedVariable(String name, Object value) throws TemplateE
172172
173173 sharedVariables .put (name , value );
174174 freemarkerConfig .setSharedVariable (name , value );
175- logger .info ("Added shared variable: {}" , name );
175+ log .info ("Added shared variable: {}" , name );
176176 }
177177
178178 /**
@@ -187,7 +187,7 @@ public static void removeSharedVariable(String name) {
187187
188188 sharedVariables .remove (name );
189189 // Just remove from the map, no need to set to null in the config
190- logger .info ("Removed shared variable: {}" , name );
190+ log .info ("Removed shared variable: {}" , name );
191191 }
192192
193193 /**
@@ -196,7 +196,7 @@ public static void removeSharedVariable(String name) {
196196 public static void clearSharedVariables () {
197197 sharedVariables .clear ();
198198 resetConfiguration ();
199- logger .info ("Cleared all shared variables" );
199+ log .info ("Cleared all shared variables" );
200200 }
201201
202202 /**
@@ -212,9 +212,9 @@ public static void setConfigurationProperties(Properties properties) throws Temp
212212
213213 try {
214214 freemarkerConfig .setSettings (properties );
215- logger .info ("Applied configuration properties" );
215+ log .info ("Applied configuration properties" );
216216 } catch (TemplateException e ) {
217- logger .error ("Failed to apply configuration properties" , e );
217+ log .error ("Failed to apply configuration properties" , e );
218218 throw e ;
219219 }
220220 }
@@ -227,7 +227,7 @@ public static void setConfigurationProperties(Properties properties) throws Temp
227227 */
228228 public static void setTemplatePreProcessor (BiFunction <String , Map <String , Object >, String > preprocessor ) {
229229 templatePreProcessor = preprocessor ;
230- logger .info ("Template preprocessor {}" , preprocessor != null ? "set" : "removed" );
230+ log .info ("Template preprocessor {}" , preprocessor != null ? "set" : "removed" );
231231 }
232232
233233 /**
@@ -238,7 +238,7 @@ public static void setTemplatePreProcessor(BiFunction<String, Map<String, Object
238238 */
239239 public static void setTemplatePostProcessor (BiFunction <String , Map <String , Object >, String > postprocessor ) {
240240 templatePostProcessor = postprocessor ;
241- logger .info ("Template postprocessor {}" , postprocessor != null ? "set" : "removed" );
241+ log .info ("Template postprocessor {}" , postprocessor != null ? "set" : "removed" );
242242 }
243243
244244 /**
@@ -258,7 +258,7 @@ public static void setAsyncThreadPoolSize(int threadCount) {
258258
259259 // Create a new executor service with the specified thread count
260260 executorService = Executors .newFixedThreadPool (threadCount );
261- logger .info ("Async thread pool size set to {}" , threadCount );
261+ log .info ("Async thread pool size set to {}" , threadCount );
262262 }
263263
264264 /**
@@ -268,7 +268,7 @@ public static void setAsyncThreadPoolSize(int threadCount) {
268268 public static void shutdownAsyncThreadPool () {
269269 if (executorService != null ) {
270270 executorService .shutdown ();
271- logger .info ("Async thread pool shut down" );
271+ log .info ("Async thread pool shut down" );
272272 }
273273 }
274274
@@ -286,7 +286,7 @@ public static void setTemplateDirectory(String templateDir) throws IOException {
286286 freemarkerConfig .setDirectoryForTemplateLoading (dir );
287287 // Clear the template cache when changing the template directory
288288 templateCache .clear ();
289- logger .info ("FreeMarker configured to load templates from directory: {}" , templateDir );
289+ log .info ("FreeMarker configured to load templates from directory: {}" , templateDir );
290290 }
291291
292292 /**
@@ -302,7 +302,7 @@ public static void addTemplateLoader(TemplateLoader loader) {
302302
303303 additionalLoaders .add (loader );
304304 resetConfiguration ();
305- logger .info ("Added custom template loader: {}" , loader .getClass ().getSimpleName ());
305+ log .info ("Added custom template loader: {}" , loader .getClass ().getSimpleName ());
306306 }
307307
308308 /**
@@ -315,9 +315,9 @@ public static void setTemplateCachingEnabled(boolean enabled) {
315315 templateCachingEnabled = enabled ;
316316 if (!enabled ) {
317317 templateCache .clear ();
318- logger .info ("Template caching disabled and cache cleared" );
318+ log .info ("Template caching disabled and cache cleared" );
319319 } else {
320- logger .info ("Template caching enabled" );
320+ log .info ("Template caching enabled" );
321321 }
322322 }
323323
@@ -341,15 +341,15 @@ public static void setTemplateCacheMaxSize(int maxSize) {
341341 templateCache .clear ();
342342 }
343343
344- logger .info ("Template cache max size set to: {}" , maxSize );
344+ log .info ("Template cache max size set to: {}" , maxSize );
345345 }
346346
347347 /**
348348 * Clears the template cache.
349349 */
350350 public static void clearTemplateCache () {
351351 templateCache .clear ();
352- logger .info ("Template cache cleared" );
352+ log .info ("Template cache cleared" );
353353 }
354354
355355 /**
@@ -375,10 +375,10 @@ public static void setClasspathAndFileTemplateLoaders(String classpathPrefix, St
375375 File dir = new File (fileSystemDir );
376376 if (!dir .exists ()) {
377377 if (!dir .mkdirs ()) {
378- logger .warn ("Could not create template directory: {}" , fileSystemDir );
378+ log .warn ("Could not create template directory: {}" , fileSystemDir );
379379 // Continue with just the classpath loader
380380 freemarkerConfig .setTemplateLoader (classLoader );
381- logger .info ("FreeMarker configured to load templates from classpath:{} only" , classpathPrefix );
381+ log .info ("FreeMarker configured to load templates from classpath:{} only" , classpathPrefix );
382382 return ;
383383 }
384384 }
@@ -391,13 +391,13 @@ public static void setClasspathAndFileTemplateLoaders(String classpathPrefix, St
391391 FileTemplateLoader fileLoader = new FileTemplateLoader (dir );
392392 TemplateLoader [] loaders = new TemplateLoader []{classLoader , fileLoader };
393393 freemarkerConfig .setTemplateLoader (new MultiTemplateLoader (loaders ));
394- logger .info ("FreeMarker configured to load templates from classpath:{} and directory:{}" ,
394+ log .info ("FreeMarker configured to load templates from classpath:{} and directory:{}" ,
395395 classpathPrefix , fileSystemDir );
396396 } catch (IOException e ) {
397397 // If file loader fails, continue with just the classpath loader
398398 freemarkerConfig .setTemplateLoader (classLoader );
399- logger .info ("FreeMarker configured to load templates from classpath:{} only" , classpathPrefix );
400- logger .warn ("Could not configure file system template loader" , e );
399+ log .info ("FreeMarker configured to load templates from classpath:{} only" , classpathPrefix );
400+ log .warn ("Could not configure file system template loader" , e );
401401 }
402402 }
403403
@@ -435,10 +435,10 @@ public static String renderTemplateToHtml(String templateName, Map<String, Objec
435435 return result ;
436436 }
437437 } catch (IOException e ) {
438- logger .error ("Failed to load template: {}" , templateName , e );
438+ log .error ("Failed to load template: {}" , templateName , e );
439439 throw new IOException ("Failed to load template: " + templateName , e );
440440 } catch (TemplateException e ) {
441- logger .error ("Failed to process template: {}" , templateName , e );
441+ log .error ("Failed to process template: {}" , templateName , e );
442442 throw e ;
443443 }
444444 }
@@ -480,14 +480,14 @@ private static Template getTemplateFromCacheOrLoad(String templateName) throws I
480480 // Add to cache if not at max size
481481 if (templateCache .size () < templateCacheMaxSize ) {
482482 templateCache .put (templateName , template );
483- logger .debug ("Added template to cache: {}" , templateName );
483+ log .debug ("Added template to cache: {}" , templateName );
484484 } else {
485485 // Cache is full, log a warning
486- logger .warn ("Template cache is full (size: {}). Consider increasing the cache size." ,
486+ log .warn ("Template cache is full (size: {}). Consider increasing the cache size." ,
487487 templateCacheMaxSize );
488488 }
489489 } else {
490- logger .debug ("Template loaded from cache: {}" , templateName );
490+ log .debug ("Template loaded from cache: {}" , templateName );
491491 }
492492
493493 return template ;
@@ -536,10 +536,10 @@ public static String renderTemplateStringToHtml(String templateContent, String t
536536
537537 return result ;
538538 } catch (TemplateException e ) {
539- logger .error ("Failed to process template string: {}" , templateName , e );
539+ log .error ("Failed to process template string: {}" , templateName , e );
540540 throw e ;
541541 } catch (IOException e ) {
542- logger .error ("I/O error processing template string: {}" , templateName , e );
542+ log .error ("I/O error processing template string: {}" , templateName , e );
543543 throw e ;
544544 }
545545 }
@@ -580,7 +580,7 @@ public static void saveTemplate(String templateContent, String templateName) thr
580580
581581 Path templatePath = Paths .get (templateDir .getPath (), templateName );
582582 Files .write (templatePath , templateContent .getBytes (StandardCharsets .UTF_8 ));
583- logger .info ("Template saved to: {}" , templatePath );
583+ log .info ("Template saved to: {}" , templatePath );
584584 }
585585
586586 /**
@@ -756,7 +756,7 @@ public static void renderTemplateToPdfFile(String templateName, Map<String, Obje
756756 String outputPath , PdfOptions options ) throws Exception {
757757 try (FileOutputStream fos = new FileOutputStream (outputPath )) {
758758 renderTemplateToPdf (templateName , dataModel , fos , options );
759- logger .info ("PDF created successfully at: {}" , outputPath );
759+ log .info ("PDF created successfully at: {}" , outputPath );
760760 }
761761 }
762762
@@ -788,7 +788,7 @@ public static void renderTemplateStringToPdfFile(String templateContent, String
788788 String outputPath , PdfOptions options ) throws Exception {
789789 try (FileOutputStream fos = new FileOutputStream (outputPath )) {
790790 renderTemplateStringToPdf (templateContent , templateName , dataModel , fos , options );
791- logger .info ("PDF created successfully at: {}" , outputPath );
791+ log .info ("PDF created successfully at: {}" , outputPath );
792792 }
793793 }
794794
@@ -983,12 +983,12 @@ private static void configureFonts(ITextRenderer renderer, PdfOptions opts) {
983983 String name = f .getName ().toLowerCase ();
984984 if (name .endsWith (".ttf" ) || name .endsWith (".otf" )) {
985985 fr .addFont (f .getAbsolutePath (), true );
986- logger .debug ("Loaded font: {}" , f .getName ());
986+ log .debug ("Loaded font: {}" , f .getName ());
987987 }
988988 }
989989 }
990990 } catch (Exception e ) {
991- logger .warn ("Error loading fonts from {}" , opts .getFontDir (), e );
991+ log .warn ("Error loading fonts from {}" , opts .getFontDir (), e );
992992 }
993993 }
994994 }
@@ -1297,7 +1297,7 @@ public static byte[] renderTemplateStringToImage(String templateContent, String
12971297 public static void renderHtmlToPdfFile (String htmlContent , String outputPath , PdfOptions options ) throws Exception {
12981298 try (FileOutputStream fos = new FileOutputStream (outputPath )) {
12991299 renderHtmlToPdf (htmlContent , fos , options );
1300- logger .info ("PDF created successfully at: {}" , outputPath );
1300+ log .info ("PDF created successfully at: {}" , outputPath );
13011301 }
13021302 }
13031303
@@ -1375,7 +1375,7 @@ public static CompletableFuture<byte[]> renderHtmlToPdfBytesAsync(String htmlCon
13751375 public static void saveImageToFile (byte [] imageBytes , String outputPath ) throws IOException {
13761376 try (FileOutputStream fos = new FileOutputStream (outputPath )) {
13771377 fos .write (imageBytes );
1378- logger .info ("Image saved to: {}" , outputPath );
1378+ log .info ("Image saved to: {}" , outputPath );
13791379 }
13801380 }
13811381
0 commit comments