CSS classes not getting applied while converting HTML to pdf using iTextPdf

101 Views Asked by At

I am using thymeleaf template in spring boot to create pdf. My thymeleaf template is getting rendered correctly if I open the genrated HTML on browser. But when PDF is generated some of the CSS styles are not getting applied properly. like class row, col not working during PDF creation.

Here is my template with fragments :

Main template :

</head>
<body>

    <!-- <div th:class="'sec sec-middle'"> -->
    <div>
        <h3 th:text="${subModuleDto.submodule_name}"></h3>
        <p th:text="${subModuleDto.summary}"></p>
        <p th:text="${subModuleDto.description}"></p>

         <div th:class="'request-container'">
            <h4>Request Parameters</h4>
            <div id="paramTree">
                <ul th:class="'tree'">
                    <li>
                        <div th:insert="fragments/paramObject :: paramObject(${subModuleDto.requestParams})"></div>
                    </li>
                </ul>
            </div>
        </div>
        
    </div>
<script type="text/javascript" th:src="@{/js/jquery-3.3.1.slim.min.js}"></script>
</body>
</html>

Fragment 1 - paramObject :


<div th:fragment="paramObject(paramObj)">
    <ul>
        <li th:each="paramInfo : ${paramObj}">
                <div th:class="'tree-sec row'">
                    <div th:class="'parm-heading col'">
                        <strong th:text="${paramInfo.param_name}"></strong>
                        <span th:if="${paramInfo.mandatory} == 'true'" th:class="'seconday-text mandatory-text'">mandatory</span>
                    </div>
                    <div th:class="'parm-description col'">
                        <span th:text="${paramInfo.data_type}" th:class="'btn grey-btn string-btn'"></span>
                        <span th:text="${paramInfo.description}" th:class="'string-text'"></span>
                    </div>      
                </div>
            <!-- Render child objects recursively if they exist -->
            <th:block th:if="${paramInfo.childObjects != null}">
                <div th:insert="fragments/childParamNode :: childParamNode(${paramInfo.childObjects})"></div>
            </th:block>
        </li>
    </ul>
</div>


Fragment 2 - childParamNode :


<div th:fragment="childParamNode(childParam)">
    <ul>
        <li th:each="paramInfo : ${childParam}">
                    <div th:class="'tree-sec row'">
                        <div th:class="'parm-heading col'">
                            <strong th:text="${paramInfo.child_param_name}"></strong>
                            <span th:if="${paramInfo.mandatory} == 'true'" th:class="'seconday-text mandatory-text'">mandatory</span>
                        </div>
                        <div th:class="'parm-description col'">
                            <span th:text="${paramInfo.child_data_type}" th:class="'btn grey-btn string-btn'"></span>
                            <span th:text="${paramInfo.child_description}" th:class="'string-text'"></span>
                        </div>
                    </div>          
            
            <!-- Render child objects recursively if they exist -->
            <th:block th:if="${paramInfo.childObject != null}">
                <div th:insert="fragments/childParamNode :: childParamNode(${paramInfo.childObject})"></div>
            </th:block>
        </li>
    </ul>
</div>

this is my pdf creation service :


@Service
public class DocumentGenerator {
    

    public String htmlToPdf(String processedHtml){
        
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        
        try {
    
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(byteArrayOutputStream));
            pdfDoc.setDefaultPageSize(PageSize.A4);
            
            
            DefaultFontProvider defaultFont = new DefaultFontProvider(false, true, false);
            
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setFontProvider(defaultFont);
            converterProperties.setBaseUri(ServiceUtility.getCurrentBaseUrl());
            
            HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
            
            FileOutputStream fout = new FileOutputStream("/home/vaibhav/Desktop/CreatePDF/sample.pdf");
            
            byteArrayOutputStream.writeTo(fout);
            byteArrayOutputStream.close();
            byteArrayOutputStream.flush();
            fout.close();
            
            return null;
            
        } catch (Exception e) {
            System.out.println("error in html to pdf method");
        }
        
        return null;
    }
}

I have tried tried several ways but PDF is not getting created as it is rendering on the browser when I open created html template.

I am using internal CSS. and also used external CSS. Some styles are working fine like text-color, background color.

0

There are 0 best solutions below