1818
1919package it .eng .knowage .engine .cockpit .api .export .pdf ;
2020
21- import java .awt .Color ;
22- import java .io .ByteArrayOutputStream ;
23- import java .io .IOException ;
24- import java .math .BigDecimal ;
25- import java .text .DateFormat ;
26- import java .text .SimpleDateFormat ;
27- import java .util .ArrayList ;
28- import java .util .Date ;
29- import java .util .HashMap ;
30- import java .util .List ;
31- import java .util .Locale ;
32- import java .util .Map ;
33- import java .util .Objects ;
34-
21+ import be .quodlibet .boxable .*;
22+ import it .eng .knowage .engine .cockpit .api .export .AbstractFormatExporter ;
23+ import it .eng .spago .error .EMFAbstractError ;
24+ import it .eng .spagobi .analiticalmodel .document .bo .ObjTemplate ;
25+ import it .eng .spagobi .commons .SingletonConfig ;
26+ import it .eng .spagobi .commons .dao .DAOFactory ;
27+ import it .eng .spagobi .utilities .assertion .Assert ;
28+ import it .eng .spagobi .utilities .exceptions .SpagoBIRuntimeException ;
3529import org .apache .commons .lang3 .StringUtils ;
3630import org .apache .logging .log4j .LogManager ;
3731import org .apache .logging .log4j .Logger ;
3832import org .apache .pdfbox .io .MemoryUsageSetting ;
3933import org .apache .pdfbox .pdmodel .PDDocument ;
4034import org .apache .pdfbox .pdmodel .PDPage ;
4135import org .apache .pdfbox .pdmodel .common .PDRectangle ;
42- import org .apache .pdfbox .pdmodel .font .PDType1Font ;
36+ import org .apache .pdfbox .pdmodel .font .PDFont ;
37+ import org .apache .pdfbox .pdmodel .font .PDType0Font ;
4338import org .json .JSONArray ;
4439import org .json .JSONException ;
4540import org .json .JSONObject ;
4641
47- import be .quodlibet .boxable .BaseTable ;
48- import be .quodlibet .boxable .Cell ;
49- import be .quodlibet .boxable .HorizontalAlignment ;
50- import be .quodlibet .boxable .Row ;
51- import be .quodlibet .boxable .VerticalAlignment ;
52- import it .eng .knowage .engine .cockpit .api .export .AbstractFormatExporter ;
53- import it .eng .spago .error .EMFAbstractError ;
54- import it .eng .spagobi .analiticalmodel .document .bo .ObjTemplate ;
55- import it .eng .spagobi .commons .SingletonConfig ;
56- import it .eng .spagobi .commons .dao .DAOFactory ;
57- import it .eng .spagobi .utilities .assertion .Assert ;
58- import it .eng .spagobi .utilities .exceptions .SpagoBIRuntimeException ;
42+ import java .awt .*;
43+ import java .io .ByteArrayOutputStream ;
44+ import java .io .File ;
45+ import java .io .IOException ;
46+ import java .math .BigDecimal ;
47+ import java .net .URISyntaxException ;
48+ import java .net .URL ;
49+ import java .text .DateFormat ;
50+ import java .text .SimpleDateFormat ;
51+ import java .util .*;
52+ import java .util .List ;
5953
6054public class PdfExporter extends AbstractFormatExporter {
6155
@@ -181,7 +175,7 @@ private void exportTableWidget(PDDocument document, String templateString, long
181175
182176 private void addDataToTable (BaseTable table , JSONObject settings , JSONArray columnsOrdered ,
183177 List <Integer > pdfHiddenColumns , String [] columnDateFormats , JSONObject [] columnStyles , JSONArray rows )
184- throws JSONException {
178+ throws JSONException , IOException , URISyntaxException {
185179 // Check if summary row is enabled
186180 boolean summaryRowEnabled = false ;
187181 String summaryRowLabel = null ;
@@ -262,11 +256,14 @@ private void addDataToTable(BaseTable table, JSONObject settings, JSONArray colu
262256
263257 Cell <PDPage > cell = row .createCell (columnPercentWidths [c ], valueStr ,
264258 HorizontalAlignment .get ("center" ), VerticalAlignment .get ("top" ));
259+ URL resource = getClass ().getClassLoader ().getResource ("/fonts/DejaVuSans.ttf" );
260+ File pdfFontFile = new File (resource .toURI ());
261+ PDFont font = PDType0Font .load (table .document , pdfFontFile );
262+ cell .setFont (font );
265263 // first of all set alternate rows color
266264 if (settings != null && settings .has ("alternateRows" )) {
267265 JSONObject alternateRows = settings .getJSONObject ("alternateRows" );
268266 if (alternateRows .optBoolean ("enabled" )) {
269- cell .setFont (PDType1Font .HELVETICA );
270267 if (r % 2 == 0 ) {
271268 cell .setFillColor (
272269 getColorFromString (alternateRows .optString ("evenRowsColor" ), Color .WHITE ));
@@ -289,7 +286,7 @@ private void addDataToTable(BaseTable table, JSONObject settings, JSONArray colu
289286 }
290287
291288 private void addHeaderToTable (BaseTable table , JSONObject style , JSONObject widgetData , JSONObject widgetContent ,
292- JSONArray columnsOrdered , List <Integer > pdfHiddenColumns ) throws JSONException {
289+ JSONArray columnsOrdered , List <Integer > pdfHiddenColumns ) throws JSONException , IOException , URISyntaxException {
293290// HashMap<String, String> arrayHeader = new HashMap<String, String>();
294291// for (int i = 0; i < widgetContent.getJSONArray("columnSelectedOfDataset").length(); i++) {
295292// JSONObject column = widgetContent.getJSONArray("columnSelectedOfDataset").getJSONObject(i);
@@ -304,6 +301,9 @@ private void addHeaderToTable(BaseTable table, JSONObject style, JSONObject widg
304301
305302 JSONArray groupsFromWidgetContent = getGroupsFromWidgetContent (widgetData );
306303 Map <String , String > groupsAndColumnsMap = getGroupAndColumnsMap (widgetContent , groupsFromWidgetContent );
304+ URL resource = getClass ().getClassLoader ().getResource ("/fonts/DejaVuSans.ttf" );
305+ File pdfFontFile = new File (resource .toURI ());
306+ PDFont font = PDType0Font .load (table .document , pdfFontFile );
307307
308308 if (!groupsAndColumnsMap .isEmpty ()) {
309309 Row <PDPage > groupHeaderRow = table .createRow (15f );
@@ -313,15 +313,15 @@ private void addHeaderToTable(BaseTable table, JSONObject style, JSONObject widg
313313 if (groupName != null ) {
314314 Cell <PDPage > cell = groupHeaderRow .createCell (columnPercentWidths [i ], groupName ,
315315 HorizontalAlignment .get ("center" ), VerticalAlignment .get ("top" ));
316- styleHeaderCell (style , cell );
316+ styleHeaderCell (style , cell , font );
317317 // check if adjacent header cells have same group names in order to add merged region
318318 int adjacents = getAdjacentEqualNamesAmount (groupsAndColumnsMap , columnsOrdered , i , groupName );
319319 if (adjacents > 1 ) {
320320 cell .setRightBorderStyle (null );
321321 for (int j = 1 ; j < adjacents ; j ++) {
322322 cell = groupHeaderRow .createCell (columnPercentWidths [i +j ], "" ,
323323 HorizontalAlignment .get ("center" ), VerticalAlignment .get ("top" ));
324- styleHeaderCell (style , cell );
324+ styleHeaderCell (style , cell , font );
325325 cell .setLeftBorderStyle (null );
326326 if (j + 1 < adjacents ) {
327327 cell .setRightBorderStyle (null );
@@ -332,7 +332,7 @@ private void addHeaderToTable(BaseTable table, JSONObject style, JSONObject widg
332332 } else {
333333 Cell <PDPage > blankCell = groupHeaderRow .createCell (columnPercentWidths [i ], "" ,
334334 HorizontalAlignment .get ("center" ), VerticalAlignment .get ("top" ));
335- styleHeaderCell (style , blankCell );
335+ styleHeaderCell (style , blankCell , font );
336336 }
337337
338338 }
@@ -363,7 +363,7 @@ private void addHeaderToTable(BaseTable table, JSONObject style, JSONObject widg
363363
364364 Cell <PDPage > cell = headerRow .createCell (columnPercentWidths [i ], columnName ,
365365 HorizontalAlignment .get ("center" ), VerticalAlignment .get ("top" ));
366- styleHeaderCell (style , cell );
366+ styleHeaderCell (style , cell , font );
367367 }
368368
369369 table .addHeaderRow (headerRow );
@@ -387,10 +387,9 @@ private int getAdjacentEqualNamesAmount(Map<String, String> groupsAndColumnsMap,
387387 }
388388 }
389389
390- private void styleHeaderCell (JSONObject style , Cell <PDPage > headerCell ) throws JSONException {
390+ private void styleHeaderCell (JSONObject style , Cell <PDPage > headerCell , PDFont font ) throws JSONException {
391391 if (style != null && style .has ("th" ) && style .getJSONObject ("th" ).optBoolean ("enabled" )) {
392392 JSONObject headerStyle = style .getJSONObject ("th" );
393- headerCell .setFont (PDType1Font .HELVETICA_BOLD );
394393 if (headerStyle .has ("font-size" )) {
395394 float size = getFontSizeFromString (headerStyle .getString ("font-size" ));
396395 if (size != 0 )
@@ -399,6 +398,7 @@ private void styleHeaderCell(JSONObject style, Cell<PDPage> headerCell) throws J
399398 headerCell .setFillColor (getColorFromString (headerStyle .optString ("background-color" ), Color .WHITE ));
400399 headerCell .setTextColor (getColorFromString (headerStyle .optString ("color" ), Color .BLACK ));
401400 }
401+ headerCell .setFont (font );
402402 }
403403
404404 private String [] getColumnDateFormats (JSONArray columnsOrdered , JSONObject widgetContent ) {
0 commit comments