Search code examples
javacssitext

iText HTML to PDF change background color


I was trying to convert an HTML to PDF file using iText with Java. However my PDF file has an area with different color cause by tag <body> background property. The PDF file should have all-white background same as the <div class='container'> class. Is there any way to achieve it instead of changing <body> background in <style> tag to white?

enter image description here

This is my HTML and CSS

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @page {
            margin-left: 0;
            margin-right: 0;
            margin-top: 0;

            @bottom-left {
                margin-left: 10px;
                content: "Page " counter(page) " / " counter(pages);
            }
        }

        body {
            scroll-behavior: smooth;
            color: #000;
            font-weight: normal;
            background: #f0f0f0;
            background-color: rgb(244, 180, 103);
            width: 100%;
            line-height: 20pt;
            text-align: justify;
            margin: 0 auto;
            height: 100%;
        }

        @media print {
            body {
                background-color: #fff;
            }
        }

        .container {
            scroll-behavior: smooth;
            width: 210mm;
            padding: 25mm 25mm 25mm 35mm;
            background: #FFF;
            margin: 0 auto;
            box-sizing: border-box;
            position: relative;
            font-size: 14pt;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>CONTAINER</h1>
        <p>This is my data</p>
        <p>This is my data</p>
        <p>This is my data</p>
        <p>This is my data</p>
        <p>This is my data</p>
        <p>This is my data</p>
        <p>This is my data</p>
        <p>This is my data</p>
    </div>
</body>

</html>

This is my Java code

private static final String HTML_FILE_PATH = "C:/yourlocation/input.html";
private static final String PDF_FILE_PATH = "C:/yourlocation/output.pdf";

public static void main(String[] args) throws IOException {
    System.out.println("Hello World!");

    FontProvider fontProvider = new DefaultFontProvider(true, true, true);
    ConverterProperties properties = new ConverterProperties();
    properties.setCharset("UTF-8");
    properties.setFontProvider(fontProvider);

    File htmlFile = new File(HTML_FILE_PATH);
    File pdfFile = new File(PDF_FILE_PATH);
    try (FileInputStream fStream = new FileInputStream(htmlFile)) {
        System.out.println("BEGIN to convert HTML to PDF");
        HtmlConverter.convertToPdf(fStream, new FileOutputStream(pdfFile), properties);
        System.out.println("FINISHED convert HTML to PDF");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Solution

  • OK so I can change the background-color of the <body> tag using iText by extends BodyTagCssApplier, override, copy the entire of the apply method and set it's background color (in cssProps below) to white.

    private static class CustomBodyTagCssApplier extends BodyTagCssApplier {
        @Override
        public void apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker) {
            Map<String, String> cssProps = stylesContainer.getStyles();
            // Set background-color to white
            cssProps.put(CommonCssConstants.BACKGROUND_COLOR, "#fff");
            BodyHtmlStylesContainer styleProperty = new BodyHtmlStylesContainer();
            IPropertyContainer container = tagWorker.getElementResult();
            if (container != null) {
                BackgroundApplierUtil.applyBackground(cssProps, context, styleProperty);
                MarginApplierUtil.applyMargins(cssProps, context, styleProperty);
                PaddingApplierUtil.applyPaddings(cssProps, context, styleProperty);
                BorderStyleApplierUtil.applyBorders(cssProps, context, styleProperty);
                if (styleProperty.hasStylesToApply()) {
                    container.setProperty(Html2PdfProperty.BODY_STYLING, styleProperty);
                }
                FontStyleApplierUtil.applyFontStyles(cssProps, context, stylesContainer, container);
            }
        }
    }
    

    And then use ConverterProperties to apply that CSS to my <body> tag

    public static void main(String[] args) {
        // Some code in question above
        ConverterProperties properties = new ConverterProperties();
        properties.setCssApplierFactory(new DefaultCssApplierFactory() {
            @Override
            public ICssApplier getCustomCssApplier(IElementNode tag) {
                if ("body".equals(tag.name())) {
                    return new CustomBodyTagCssApplier();
                }
                return null;
            }
        });
        // Some code in question above
        // HtmlConverter.convertToPdf(fStream, new FileOutputStream(pdfFile), properties);
    }