Jump to content
Changes to the Jaspersoft community edition download ×

JasperReports 3.7 to 6.3 migration: How to replace PdfFont in font mapping code


farrukh.najm

Recommended Posts

I am migrating some legacy JasperReports client code from Jasper 3.7 to Jasper 6.3. The code uses PdfFont class to do font mapping when exporting report to PDF.

The font mapping API in Jasper 6.3 seems to have changed a lot from 3.7 and I cannot find any examples of how to replace code to match new API.

 

The current code looks like this:

 

    String jasperTemplateFileName = ....;            
JasperPrint jp = JasperFillManager.fillReport( jasperTemplateFileName, _jasperParams );

File generatedPDFFile = ...;
String outputFileName = generatedPDFFile.getAbsolutePath();

JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);

//Do font mapping. This is where I am having trouble
HashMap fontMap = new HashMap();
PdfFont boldPdfFont = new PdfFont(REPORT_FONTS_PATH + "arialbd.ttf", "Cp1252", true);
FontKey fontKey = new FontKey("SansSerif",true,false);
fontMap.put(fKey, boldPdfFont);

exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);
exporter.exportReport();[/code]
 

 

There is no PdfFont class in Jasper 6.3 and the replacement code is not obvious to me. Here is what I have so far:

Exporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFileName));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setPermissions(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);

//How to do font mapping here??

exporter.exportReport();[/code]
 

TIA for any guidance.

 

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

@hozawa Thanks for your help. Can you be more specific as to what the code would look like if I want to set default font for normal, bold, italic, boldItalic font face and sepcifu pdf Encoding and whether the font is embedded or not for my entire report. I created a FontFamily using code below but then I do not know how to set this as the default for my entire PDF report:

SimpleFontFamily fontFamily = new SimpleFontFamily();                
fontFamily.supportsLocale(reportLocale);
fontFamily.setPdfEmbedded(Boolean.TRUE);
            SimpleFontFace fontFace = new SimpleFontFace(_reportContext);
            fontFace.setTtf(REPORT_FONTS_PATH + "arial.ttf");
            fontFamily.setNormalFace(fontFace);
            
            fontFace = new SimpleFontFace(_reportContext);
            fontFace.setTtf(REPORT_FONTS_PATH + "arialbd.ttf");
            fontFamily.setBoldFace(fontFace);
            
            fontFace = new SimpleFontFace(_reportContext);
            fontFace.setTtf(REPORT_FONTS_PATH + "ariali.ttf");
            fontFamily.setItalicFace(fontFace);
            
            fontFace = new SimpleFontFace(_reportContext);
            fontFace.setTtf(REPORT_FONTS_PATH + "arialbi.ttf");
            
            fontFamily.setPdfEncoding("Cp1252");

At this point I have a FontFamily all good to go but how to I set this for my entire report as default? How can I pass this FontFamily to my JRPdfExporter?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...