flyingsaucerproject / flyingsaucer

XML/XHTML and CSS 2.1 renderer in pure Java
Other
2.02k stars 564 forks source link

java.lang.IllegalArgumentException: Font size too small: 0.0 #362

Closed sinclairjaza closed 3 months ago

sinclairjaza commented 3 months ago

I have this error when I try to convert my html template to pdf.

Java Error:

java.lang.IllegalArgumentException: Font size too small: 0.0
    at com.lowagie.text.pdf.PdfContentByte.setFontAndSize(PdfContentByte.java:1649)
    at org.xhtmlrenderer.pdf.ITextOutputDevice.drawString(ITextOutputDevice.java:505)
    at org.xhtmlrenderer.pdf.ITextTextRenderer.drawString(ITextTextRenderer.java:40)
    at org.xhtmlrenderer.render.AbstractOutputDevice.drawText(AbstractOutputDevice.java:78)
    at org.xhtmlrenderer.render.InlineText.paint(InlineText.java:129)
    at org.xhtmlrenderer.render.InlineLayoutBox.paintInline(InlineLayoutBox.java:262)
    at org.xhtmlrenderer.layout.Layer.paintInlineContent(Layer.java:258)
    at org.xhtmlrenderer.layout.Layer.paint(Layer.java:315)
    at org.xhtmlrenderer.pdf.ITextRenderer.paintPage(ITextRenderer.java:495)
    at org.xhtmlrenderer.pdf.ITextRenderer.writePDF(ITextRenderer.java:443)
    at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:396)
    at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:328)
    at fr.site.service.EmailService.convertHtmlToPdf(EmailService.java:417)
    at fr.site.service.EmailService.GeneratePdfFromHtml(EmailService.java:406)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)

I use Java 17 and Spring boot 3.1.0 and flying-saucer-pdf: 9.9.0

My Java Code:

public void convertHtmlToPdf(String html) throws IOException {
        final String directoryPath = "src/main/resources/pdf/test.pdf";
        OutputStream outputStream = new FileOutputStream(directoryPath);

        ITextRenderer iTextRenderer = new ITextRenderer();
        iTextRenderer.setDocumentFromString(html);
        iTextRenderer.layout();
        iTextRenderer.createPDF(outputStream);
        outputStream.close();
    }

My HTML file:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
    <TITLE>Title</TITLE>
    <STYLE type="text/css">
        .outlook-group-fix {
            width: 100% !important;
        }
    </STYLE>
    <meta name="GENERATOR" content="MSHTML 11.00.10570.1001"/>
</head>

<body>
<div>
    <!--[if mso | IE]>
    <table role="presentation" style="WIDTH: 640px" cellSpacing="0" cellPadding="0" width=640 align="center" border="0">
        <TBODY>
        <TR>
            <TD style="font-size: 0px; LINE-HEIGHT: 0px; mso-line-height-rule: exactly">
    <![endif]-->
    <div style="MAX-WIDTH: 100%; MARGIN: 0px auto">
        <table role="presentation" style="font-size: 0px; WIDTH: 100%" cellspacing="0" cellpadding="0" align="center"
               border="0">
            <tbody>
            <tr>
                <td style="MAX-WIDTH: 640px; margin : auto; font-size: 0px; VERTICAL-ALIGN: top; PADDING-BOTTOM: 10px; DIRECTION: ltr; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 8px; PADDING-RIGHT: 8px">
                    <!--[if mso | IE]>
                    <table role="presentation" cellSpacing="0" cellPadding="0" border="0">
                        <TBODY>
                        <TR>
                            <TD style="WIDTH: 640px; VERTICAL-ALIGN: top">
                    <![endif]-->
                    <div class="mj-column-per-100 outlook-group-fix"
                         style="font-size: 13px; WIDTH: 640px; MAX-WIDTH: 640px;  VERTICAL-ALIGN: top; DIRECTION: ltr; TEXT-ALIGN: left; DISPLAY: inline-block">
                        <div align="center" style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                        </div>
                        <table role="presentation" cellspacing="0" cellpadding="0" width="100%" border="0">
                            <tbody></tbody>
                        </table>
                        <table style="border: none;width:640px;border-collapse:collapse;">
                            <tbody>
                            <tr>
                                <br/>
                                <td style="padding:0cm 0cm 0cm 0cm;text-align: justify; text-justify: inter-word;">
                                    <p style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                                        <span style="font-size:12px;font-family:'Arial',sans-serif;color:black;">Bonjour,</span>
                                        <span style="font-size:12px;font-family:  'Arial',sans-serif;color:black;">
                                            <br/>&nbsp;
                                            <br/>Suite à votre réclamation
                                            <span style="font-weight: bold;">Total Energies</span>
                                                un dédommagement <span style="font-weight: bold;">22/04/2025</span> €. </span>
                                    </p>
                                    <br/>
                                    <p style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                                        <span style="font-size:12px;font-family:  'Arial',sans-serif;color:black;">
                                            A partir de demain, nous vous invitons (<a
                                                href="https://my.site.fr/space/con" target="_blank">https://my.site.fr/space/con</a>).
                                            Vous pourrez y saisir sous 3 et 5 jours ouvrés, après avoir.
                                        </span>
                                    </p>
                                </td>

                            </tr>
                            <tr>
                                <td style="padding:0cm 0cm 0cm 0cm;text-align: justify; text-justify: inter-word;">
                                    <br/>
                                    <p style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                                         <span style="font-size:12px;font-family:  'Arial',sans-serif;color:black;">
                                             Nous notre site internet <a href="www.site.fr"
                                                                         target="_blank">www.site.fr</a> ou au
                                             <a href="tel:09" target="_blank">09</a> du de 8h à 17h.
                                        </span>
                                    </p>

                                    <br/>
                                    <p style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                                        <span style="font-size:12px;font-family:  'Arial',sans-serif;color:black;">
                                            La confiance.
                                        </span>
                                    </p>

                                    <p style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                                         <span style="font-size:12px;font-family:  'Arial',sans-serif;color:black;">
                                            <br/>Bien cordialement,
                                            <br/>Votre conseiller
                                        </span>
                                    </p>
                                    <p style="margin:0cm;font-size:15px;font-family:'Calibri',sans-serif;">
                                         <span style="font-size:12px;font-family:  'Arial',sans-serif;color:black;">
                                              <br/> <br/>Retrouvez <a href="www.site.fr" target="_blank">www.site.fr</a>.
                                        </span>
                                    </p>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <br/>
                    <!--[if mso | IE]>
                    </TD>
                    </TR>
                    </TBODY>
                    </table>
                    <![endif]-->
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <!--[if mso | IE]></TD></TR></TBODY></table><![endif]-->
</div>
</body>
</html>

Thanks for your support.

asolntsev commented 3 months ago

@sinclairjaza But you have font-size: 0px; in the html.

According to chatgpt,

font-size: 0 is not a valid value for displaying readable text

using font-size: 0 is generally not recommended if the goal is to hide content. Other CSS properties like visibility: hidden, display: none, or opacity: 0 might be more appropriate depending on the context.