Search in sources :

Example 1 with PrintLine

use of com.axelor.apps.base.db.PrintLine in project axelor-open-suite by axelor.

the class PrintServiceImpl method generateHtml.

protected String generateHtml(Print print) {
    StringBuilder htmlBuilder = new StringBuilder();
    htmlBuilder.append("<!DOCTYPE html>");
    htmlBuilder.append("<html>");
    htmlBuilder.append("<head>");
    htmlBuilder.append("<title></title>");
    htmlBuilder.append("<meta charset=\"utf-8\"/>");
    htmlBuilder.append("<style type=\"text/css\">");
    htmlBuilder.append("</style>");
    htmlBuilder.append("</head>");
    htmlBuilder.append("<body>");
    if (!print.getHidePrintSettings()) {
        Company company = print.getCompany();
        Integer logoPosition = print.getLogoPositionSelect();
        String pdfHeader = print.getPrintPdfHeader() != null ? print.getPrintPdfHeader() : "";
        String imageTag = "";
        String logoWidth = print.getLogoWidth() != null ? "width: " + print.getLogoWidth() + ";" : "width: 50%;";
        String headerWidth = print.getHeaderContentWidth() != null ? "width: " + print.getHeaderContentWidth() + ";" : "width: 40%;";
        if (company != null && company.getLogo() != null && logoPosition != PrintRepository.LOGO_POSITION_NONE && new File(attachmentPath + company.getLogo().getFilePath()).exists()) {
            String width = company.getWidth() != 0 ? "width='" + company.getWidth() + "px'" : "";
            String height = company.getHeight() != 0 ? company.getHeight() + "px" : "71px";
            imageTag = "<img src='" + company.getLogo().getFilePath() + "' height='" + height + "' " + width + "/>";
        }
        switch(logoPosition) {
            case PrintRepository.LOGO_POSITION_LEFT:
                htmlBuilder.append("<table style='width: 100%;'><tr><td valign='top' style='text-align: left; " + logoWidth + "'>" + imageTag + "</td><td valign='top' style='width: 10%'></td><td valign='top' style='text-align: left; " + headerWidth + "'>" + pdfHeader + "</td></tr></table>");
                break;
            case PrintRepository.LOGO_POSITION_CENTER:
                htmlBuilder.append("<table style=\"width: 100%;\"><tr><td valign='top' style='width: 33.33%;'></td><td valign='top' style='text-align: center; width: 33.33%;'>" + imageTag + "</td><td valign='top' style='text-align: left; width: 33.33%;'>" + pdfHeader + "</td></tr></table>");
                break;
            case PrintRepository.LOGO_POSITION_RIGHT:
                htmlBuilder.append("<table style='width: 100%;'><tr><td valign='top' style='text-align: left; " + headerWidth + "'>" + pdfHeader + "</td><td valign='top' style='width: 10%'></td><td valign='top' style='text-align: center; " + logoWidth + "'>" + imageTag + "</td></tr></table>");
                break;
            default:
                htmlBuilder.append("<table style='width: 100%;'><tr><td style='width: 60%;'></td><td valign='top' style='text-align: left; width: 40%'>" + pdfHeader + "</td></tr></table>");
                break;
        }
        if (!pdfHeader.isEmpty() || !imageTag.isEmpty()) {
            htmlBuilder.append("<hr>");
        }
    }
    if (ObjectUtils.notEmpty(print)) {
        List<PrintLine> printLineList = print.getPrintLineList();
        if (CollectionUtils.isNotEmpty(printLineList)) {
            for (PrintLine printLine : printLineList) {
                htmlBuilder.append(printLine.getIsWithPageBreakAfter() ? "<div style=\"page-break-after: always;\">" : "<div>");
                htmlBuilder.append("<table>");
                htmlBuilder.append("<tr>");
                if (printLine.getIsSignature()) {
                    htmlBuilder.append("<td>&nbsp;</td></tr></table></div>");
                    continue;
                }
                Integer nbColumns = printLine.getNbColumns() == 0 ? 1 : printLine.getNbColumns();
                for (int i = 0; i < nbColumns; i++) {
                    htmlBuilder.append("<td style=\"padding: 0px 10px 0px 10px\">");
                    if (StringUtils.notEmpty(printLine.getTitle())) {
                        htmlBuilder.append(printLine.getTitle());
                    }
                    if (StringUtils.notEmpty(printLine.getContent())) {
                        htmlBuilder.append(printLine.getContent());
                    }
                    htmlBuilder.append("</td>");
                }
                htmlBuilder.append("</tr>");
                htmlBuilder.append("</table>");
                htmlBuilder.append("</div>");
            }
        }
    }
    htmlBuilder.append("</body>");
    htmlBuilder.append("</html>");
    return htmlBuilder.toString();
}
Also used : Company(com.axelor.apps.base.db.Company) PrintLine(com.axelor.apps.base.db.PrintLine) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Print(com.axelor.apps.base.db.Print)

Example 2 with PrintLine

use of com.axelor.apps.base.db.PrintLine in project axelor-open-suite by axelor.

the class PrintTemplateServiceImpl method processPrintTemplateLineList.

private void processPrintTemplateLineList(List<PrintTemplateLine> templateLineList, Print print, PrintLine parent, Context scriptContext, TemplateMaker maker, int level) throws AxelorException {
    int seq = 1;
    if (ObjectUtils.notEmpty(templateLineList.get(0)) && ObjectUtils.notEmpty(templateLineList.get(0).getSequence())) {
        seq = templateLineList.get(0).getSequence().intValue();
    }
    for (PrintTemplateLine printTemplateLine : templateLineList) {
        if (printTemplateLine.getIgnoreTheLine()) {
            continue;
        }
        try {
            boolean present = true;
            if (StringUtils.notEmpty(printTemplateLine.getConditions())) {
                Object evaluation = templateContextService.computeTemplateContext(printTemplateLine.getConditions(), scriptContext);
                if (evaluation instanceof Boolean) {
                    present = (Boolean) evaluation;
                } else {
                    throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_CONDITION_MUST_BE_BOOLEAN));
                }
            }
            if (present) {
                ++rank;
                String title = null;
                String content = null;
                if (StringUtils.notEmpty(printTemplateLine.getTitle())) {
                    maker.setTemplate(printTemplateLine.getTitle());
                    addSequencesInContext(maker, level, seq, parent);
                    title = maker.make();
                }
                if (StringUtils.notEmpty(printTemplateLine.getContent())) {
                    maker.setTemplate(printTemplateLine.getContent());
                    addSequencesInContext(maker, level, seq, parent);
                    if (CollectionUtils.isNotEmpty(printTemplateLine.getTemplateContextList())) {
                        for (TemplateContext templateContext : printTemplateLine.getTemplateContextList()) {
                            Object result = templateContextService.computeTemplateContext(templateContext, scriptContext);
                            maker.addContext(templateContext.getName(), result);
                        }
                    }
                    content = maker.make();
                }
                PrintLine printLine = new PrintLine();
                printLine.setRank(rank);
                printLine.setSequence(seq);
                printLine.setTitle(title);
                printLine.setContent(content);
                printLine.setIsEditable(printTemplateLine.getIsEditable());
                printLine.setIsSignature(printTemplateLine.getIsSignature());
                printLine.setNbColumns(printTemplateLine.getNbColumns());
                printLine.setParent(parent);
                printLine.setIsWithPageBreakAfter(printTemplateLine.getIsWithPageBreakAfter());
                if (CollectionUtils.isNotEmpty(printTemplateLine.getPrintTemplateLineList())) {
                    processPrintTemplateLineList(printTemplateLine.getPrintTemplateLineList(), print, printLine, scriptContext, maker, level + 1);
                }
                print.addPrintLineListItem(printLine);
                seq++;
            }
        } catch (Exception e) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_ERROR_ON_LINE_WITH_SEQUENCE_AND_TITLE), printTemplateLine.getSequence(), printTemplateLine.getTitle());
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) PrintLine(com.axelor.apps.base.db.PrintLine) PrintTemplateLine(com.axelor.apps.base.db.PrintTemplateLine) TemplateContext(com.axelor.apps.message.db.TemplateContext) Print(com.axelor.apps.base.db.Print) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException)

Aggregations

Print (com.axelor.apps.base.db.Print)2 PrintLine (com.axelor.apps.base.db.PrintLine)2 Company (com.axelor.apps.base.db.Company)1 PrintTemplateLine (com.axelor.apps.base.db.PrintTemplateLine)1 TemplateContext (com.axelor.apps.message.db.TemplateContext)1 AxelorException (com.axelor.exception.AxelorException)1 MetaFile (com.axelor.meta.db.MetaFile)1 File (java.io.File)1 IOException (java.io.IOException)1