ITextRenderer does not recognize page size defined in xhtml

Viewed 109

I'm using an xhtml file to create an html page that is then rendered in pdf. That files in the html head contains:

<style>
    page {
        background: white;
        display: block;
        margin: 0 auto;
        margin-bottom: 0.5cm;
        padding: 0.5cm 2cm;
    }

    page[size="A4"] {
        width: 21cm;
        height: 29.7cm;
    }
</style>

In my java file I have:

String XHTMLstyle = new String(Files
                        .readAllBytes(Paths.get(getServletContext().getRealPath("/WEB-INF/xhtmlStyle.xhtml"))));
StringBuffer XHTML = new StringBuffer(XHTMLstyle);
                XHTML.append("<body>");
                XHTML.append("<page size=\"A4\">\r\n" + 
                        "        <div class=\"col-1\">\r\n" + 
                        "            <img src=\"logo.png\" style=\"width: 100%;\" />\r\n" + 
                        "        </div>\r\n" + 
                        "    </page>");

                XHTML.append("</body>");
                XHTML.append("</html>");

The produced xhtml is well formed. And then I create the pdf file using:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(XHTML.toString());
renderer.layout();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    renderer.createPDF(baos);

    } catch (DocumentException e) {
        setErrorMessage(response, 500, "Internal Server Error",
                            "Could not create the PDF base54 version of the eP.");
        return;
    }
    // convert PDF to base64
    String base64PDF = Base64.encodeBytes(baos.toByteArray());
    // remove new line escape character
    base64PDF = base64PDF.replaceAll("\\n", "");

The encoded base64PDF has not page size A4 all all the structure of the html head is not presented.

Any ideas?

0 Answers
Related