PDF generated from HTML, via itext (5.5.6), does not have the font size defined in CSS

Asked

Viewed 414 times

1

I am using itext 5.5.6 to generate a PDF file from an HTML document generated by Thymeleaf.

In this HTML, the styles are being reflected correctly in the browser. However, when generating the PDF, it displays all styles correctly except the definition of "font-size".

Could someone help?


    public static void main(String[] args) throws IOException, DocumentException {

        FileInputStream cssFis =
                new FileInputStream("estilo.css");

        ...

        String resultado = ... // String contendo o HTML

        ByteArrayInputStream bais = new ByteArrayInputStream(resultado.getBytes());

        // itextpdf e itext tools

        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("arquivo.pdf"));

        document.open();

        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(cssFis);
        cssResolver.addCss(cssFile);

        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(bais);

        document.close();

    }

body{
    font-family: Courier;
    font-size: 8pt;
}
.tabela {
    width: 90%;
    border: 1px solid #000000;
}
.cabecalho {
    background-color: #d3d3d3;
    text-align: center;
    font-weight: bolder;
}
.linha_numero {
    text-align: center;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cadastros Veiculo</title>
    <link rel="stylesheet" type="text/css" href="estilo.css" />
</head>
<body>
    <table id="tabela" class="tabela">
        <tr class="cabecalho">
            <td rowspan="1" colspan="1">ID</td>
            <td rowspan="1" colspan="1">PLACA</td>
            <td rowspan="1" colspan="1">PROPRIETARIO</td>
            <td rowspan="1" colspan="1">CPF</td>
            <td rowspan="1" colspan="1">RENAVAM</td>
        </tr>
        <tr>
            <td class="linha_numero" rowspan="1" colspan="1">0</td>
            <td class="linha_numero" rowspan="1" colspan="1">X</td>
            <td rowspan="1" colspan="1">X</td>
            <td class="linha_numero" rowspan="1" colspan="1">0</td>
            <td class="linha_numero" rowspan="1" colspan="1">0</td>
        </tr>
    </table>
</body>
</html>  

  • It would have been possible to generate a mcve to reproduce the problem? Only with this it becomes a little difficult to help.

  • I updated as per the question asked in stackoverflow in English. Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.