Search in sources :

Example 1 with PdfPageNumbering

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);
    }
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) GridComponent(com.qcadoo.view.api.components.GridComponent) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) PdfPageNumbering(com.qcadoo.report.api.pdf.PdfPageNumbering) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) Document(com.lowagie.text.Document) Date(java.util.Date) PdfPTable(com.lowagie.text.pdf.PdfPTable) FileOutputStream(java.io.FileOutputStream) DocumentException(com.lowagie.text.DocumentException) File(java.io.File) Map(java.util.Map) Monitorable(com.qcadoo.model.api.aop.Monitorable)

Aggregations

Document (com.lowagie.text.Document)1 DocumentException (com.lowagie.text.DocumentException)1 PdfPTable (com.lowagie.text.pdf.PdfPTable)1 PdfWriter (com.lowagie.text.pdf.PdfWriter)1 Monitorable (com.qcadoo.model.api.aop.Monitorable)1 PdfPageNumbering (com.qcadoo.report.api.pdf.PdfPageNumbering)1 ViewDefinitionState (com.qcadoo.view.api.ViewDefinitionState)1 GridComponent (com.qcadoo.view.api.components.GridComponent)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 Date (java.util.Date)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1