使用itext将html转换为pdf期间的iText 7异常:UnitValue无法强制转换为BorderRadius

时间:2018-02-20 17:18:23

标签: java html pdf itext

我使用iText 7.1.1使用HtmlConverter将html转换为pdf:

        ConverterProperties props = new ConverterProperties();

        FontProvider fp = new DefaultFontProvider(true, false, false);
        for (String font : FONTS) {
            FontProgram fontProgram = FontProgramFactory.createFont(font);
            fp.addFont(fontProgram);
        }

        props.setFontProvider(fp);

        //HtmlConverter.convertToPdf(htmlBody, outputStream, props);

        com.itextpdf.kernel.pdf.PdfWriter writer = new com.itextpdf.kernel.pdf.PdfWriter(outputStream);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);

        List<IElement> elements = HtmlConverter.convertToElements(new ByteArrayInputStream(htmlBody.getBytes(StandardCharsets.UTF_8)), props);
        for (IElement element : elements) {
            document.add((IBlockElement)element);
        }

但是对于一些Html Strings,我有以下错误:

java.lang.ClassCastException: com.itextpdf.layout.property.UnitValue cannot be cast to com.itextpdf.layout.property.BorderRadius

html看起来像:

<html>
 <head></head>
 <body>
  <meta charset="utf-8" />
  <h2>alarm<br />####</h2>
  <div>
   <p> </p>
   <b>Date: </b>01.01.2018 12:12:12 UTC-00:00
   <br />
   <b>From: </b>Google &lt;no-reply@accounts.google.com&gt;
   <br />
   <b>To: </b>mmm@mydomain.com
   <br />
   <p>#####</p>
  </div>
  <meta name="format-detection" content="date=no" />
  <meta name="format-detection" content="email=no" />
  <style>.awl a {color: #FFFFFF; text-decoration: none;}.abml a {color: #000000; font-family: Roboto-Medium,Helvetica,Arial,sans-serif; font-weight: bold; text-decoration: none;}.afal a {color: #b0b0b0; text-decoration: none;}@media screen and (min-width: 600px) {.v2sp {padding: 6px 30px 0px;} .v2rsp {padding: 0px 10px;}}</style>
  <table width="100%" height="100%" style="min-width: 348px;" border="0" cellspacing="0" cellpadding="0">
   <tbody>
    <tr height="32px"></tr>
    <tr align="center">
     <td>
      <table border="0" cellspacing="0" cellpadding="0" style="padding-bottom: 20px; max-width: 600px; min-width: 220px;">
       <tbody>
        <tr>
         <td>
          <table cellpadding="0" cellspacing="0">
           <tbody>
            <tr>
             <td></td>
             <td>
              <table width="100%" border="0" cellspacing="0" cellpadding="0" style="direction: ltr; padding-bottom: 7px;">
               <tbody>
                <tr>
                 <td align="left"><img width="92" height="32" src="https://www.gstatic.com/accountalerts/email/googlelogo_color_188x64dp.png" style="width: 92px; height: 32px;" /></td>
                 <td align="right" style="font-family: Roboto-Light,Helvetica,Arial,sans-serif;">my Name</td>
                 <td align="right" width="35"><img width="28" height="28" style="width: 28px; height: 28px; border-radius: 50%;;" src="https://www.gstatic.com/accountalerts/email/anonymous_profile_photo.png" /></td>
                </tr>
               </tbody>
              </table></td>
             <td></td>
            </tr>
            <tr>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-corner-nw.png') top left no-repeat;" width="6" height="5">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width:6px; height: 5px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-corner-nw.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-pixel-n.png') top center repeat-x;" height="5">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="height: 5px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-pixel-n.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-corner-ne.png') top right no-repeat;" width="6" height="5">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width: 6px; height: 39px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-corner-ne.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
            </tr>
            <tr>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-pixel-w.png') center left repeat-y;" width="6">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width: 6px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-pixel-w.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
             <td>
              <div style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif;padding-left: 20px; padding-right: 20px;border-bottom: thin solid #f0f0f0; color: rgba(0,0,0,0.87); font-size: 24px;padding-bottom: 38px;padding-top: 40px;text-align: center; word-break: break-word;">
               <div class="v2sp">
                Neues Gerät angemeldet
                <br />
                <a style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif;color: rgba(0,0,0,0.87); font-size: 16px; line-height: 1.8;">mmm@mydomain.com</a>
               </div>
              </div>
              <div style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 13px; color: rgba(0,0,0,0.87); line-height: 1.6;padding-left: 20px; padding-right: 20px;padding-bottom: 32px; padding-top: 24px;">
               <div class="v2sp">
                Jemand hat sich über ein neues Gerät in Ihrem Google-Konto angemeldet.
                <div style="padding-top: 24px; text-align: center;">
                 <a href="https://www.google.com" target="_blank" style="display:inline-block; text-decoration: none;">
                  <table border="0" cellspacing="0" cellpadding="0" style="background-color: #4184F3; border-radius: 2px; min-width: 90px;">
                   <tbody>
                    <tr style="height: 6px;"></tr>
                    <tr>
                     <td style="padding-left: 8px; padding-right: 8px; text-align: center;"><a href="https://www.google.com" target="_blank" style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; color: #ffffff; font-weight: 400; line-height: 20px; text-decoration: none;font-size: 13px;">AKTIVITÄT PRÜFEN</a></td>
                    </tr>
                    <tr style="height: 6px;"></tr>
                   </tbody>
                  </table></a>
                </div>
               </div>
              </div></td>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-pixel-e.png') center left repeat-y;" width="6">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width: 6px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-pixel-e.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
            </tr>
            <tr>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-corner-sw.png') top left no-repeat;" width="6" height="5">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width: 6px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-corner-sw.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-pixel-s.png') top center repeat-x" height="5">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width: 6px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-pixel-s.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
             <td style="background:url('https://www.gstatic.com/accountalerts/email/hodor/4-corner-se.png') top left no-repeat;" width="6" height="5">
              <!--[if gte mso 9]><v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width: 6px;"><v:fill type="tile" src="https://www.gstatic.com/accountalerts/email/hodor/4-corner-se.png" color="#ffffff" /><v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0"><![endif]-->
              <div></div>
              <!--[if gte mso 9]></v:textbox></v:rect><![endif]--></td>
            </tr>
            <tr>
             <td></td>
             <td>
              <div style="text-align: left;">
               <div style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif;color: rgba(0,0,0,0.54); font-size: 12px; line-height: 20px; padding-top: 10px;">
                <div>
                 Wir haben Ihnen diese E-Mail gesendet, um Sie über wichtige Änderungen zu Ihrem Google-Konto und den Diensten von Google zu informieren.
                </div>
                <div style="direction: ltr;">
                 © 2018 Google Inc.,
                 <a class="afal" style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif;color: rgba(0,0,0,0.54); font-size: 12px; line-height: 20px; padding-top: 10px;">1600 Amphitheatre Parkway, Mountain View, CA 94043, USA</a>
                </div>
               </div>
               <div style="display: none !important; mso-hide:all; max-height:0px; max-width:0px;">
                et:31
               </div>
              </div></td>
             <td></td>
            </tr>
           </tbody>
          </table></td>
        </tr>
       </tbody>
      </table></td>
    </tr>
    <tr height="32px"></tr>
   </tbody>
  </table>
 </body>
</html>

  

如何解决这些castExeption问题?

1 个答案:

答案 0 :(得分:2)

我已经能够重现这个问题,但是有一个简单的解决方法,因为它本质上是一个依赖问题。

如果您将依赖项更新为html2pdf的最新版本2.0.1,那么您的工作流程应该会成功。

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>2.0.1</version>
</dependency>

仅供参考我是iText的员工,我会确保记录这种不兼容性。