use of com.qcadoo.report.api.pdf.PdfPageNumbering in project qcadoo by qcadoo.
the class ExportToPdfController method generatePdf.
@Monitorable(threshold = 500)
@ResponseBody
@RequestMapping(value = { L_CONTROLLER_PATH }, method = RequestMethod.POST)
public Object generatePdf(@PathVariable(L_PLUGIN_IDENTIFIER_VARIABLE) final String pluginIdentifier, @PathVariable(L_VIEW_NAME_VARIABLE) final String viewName, @RequestBody final JSONObject body, final Locale locale, @RequestHeader("User-Agent") final String userAgent) {
try {
changeMaxResults(body);
ViewDefinitionState state = crudService.invokeEvent(pluginIdentifier, viewName, body, locale);
GridComponent grid = (GridComponent) state.getComponentByReference(QcadooViewConstants.L_GRID);
if (grid == null) {
JSONArray args = body.getJSONObject("event").getJSONArray("args");
if (args.length() > 0) {
grid = (GridComponent) state.getComponentByReference(args.getString(0));
}
}
Document document = new Document(PageSize.A4.rotate());
String date = DateFormat.getDateInstance().format(new Date());
File file = fileService.createExportFile("export_" + grid.getName() + "_" + date + ".pdf");
FileOutputStream fileOutputStream = new FileOutputStream(file);
PdfWriter pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
pdfWriter.setPageEvent(new PdfPageNumbering(footerResolver.resolveFooter(locale)));
document.setMargins(40, 40, 60, 60);
document.addTitle("export.pdf");
pdfHelper.addMetaData(document);
pdfWriter.createXmpMetadata();
document.open();
String title = translationService.translate(pluginIdentifier + "." + viewName + ".window.mainTab." + grid.getName() + ".header", locale);
Date generationDate = new Date();
pdfHelper.addDocumentHeader(document, "", title, translationService.translate("qcadooReport.commons.generatedBy.label", locale), generationDate);
List<String> columns = getColumns(grid);
List<String> columnNames = getColumnNames(grid, columns);
PdfPTable pdfTable = pdfHelper.createTableWithHeader(columnNames.size(), columnNames, false);
List<Map<String, String>> rows;
if (grid.getSelectedEntitiesIds().isEmpty()) {
rows = grid.getColumnValuesOfAllRecords();
} else {
rows = grid.getColumnValuesOfSelectedRecords();
}
addPdfTableCells(pdfTable, rows, columns, viewName);
document.add(pdfTable);
document.close();
boolean openInNewWindow = !StringUtils.isNoneBlank(userAgent) || (!userAgent.contains("Chrome") && !userAgent.contains("Safari")) || userAgent.contains("Edge");
state.redirectTo(fileService.getUrl(file.getAbsolutePath()) + "?clean", openInNewWindow, false);
return crudService.renderView(state);
} catch (JSONException | FileNotFoundException | DocumentException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
Aggregations