iTextPDF 7-将base64内嵌图像转换为PDF的HTML。 PNG正常,但JPG失败

时间:2019-02-15 14:51:05

标签: html itext itext7

我的html中有5个base64嵌入式图像。 4个PNG和1个JPG。 将html转换为PDF时,该过程失败。从html删除JPG图片节点时,它可以正常工作!

iTextPDF7的Java代码:

HtmlConverter.convertToPdf(new File(src), new File(dest));

JPG base64 HTML img:

<img content-height="4.22cm" content-width="7.45cm" src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4Q4kRXhpZgAATU..........

PNG base64 HTML img:

<img content-width="scale-down-to-fit" width="100%" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACrCAY........

我收到此错误消息:

  

错误的Base64输入字符为76:37(十进制)18:34:13.582 [main]   错误c.i.h.r.resource.ResourceResolver-无法检索图像   具有给定的基本URI(文件:/ D:/ PDFCONVERTER / ITEXPDF7 / html /)和图像   源路径   (数据:image / jpg; base64,/ 9j / 4AAQSkZJRgABAQEBLAEsAAD / 4Q4kRXhpZgAATU0AKgAAAAgAB .................)

java.net.MalformedURLException: unknown protocol: data
    at java.net.URL.<init>(URL.java:600) ~[na:1.8.0_72]
    at java.net.URL.<init>(URL.java:490) ~[na:1.8.0_72]
    at com.itextpdf.html2pdf.resolver.resource.UriResolver.resolveAgainstBaseUri(UriResolver.java:117) ~[html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.resolver.resource.ResourceResolver.retrieveImage(ResourceResolver.java:122) ~[html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.tags.ImgTagWorker.<init>(ImgTagWorker.java:72) [html2pdf-1.0.1.jar:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [na:1.8.0_72]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [na:1.8.0_72]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [na:1.8.0_72]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [na:1.8.0_72]
    at com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory.getTagWorker(DefaultTagWorkerFactory.java:88) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:224) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:240) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:240) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:240) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:240) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.processDocument(DefaultHtmlProcessor.java:200) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.attach.Attacher.attach(Attacher.java:78) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.HtmlConverter.convertToDocument(HtmlConverter.java:298) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:244) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:231) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:193) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:167) [html2pdf-1.0.1.jar:na]
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:147) [html2pdf-1.0.1.jar:na]
    at cl.cgr.sistradoc.pdfconverter.itextpdf7.Html2Pdf.createPdf(Html2Pdf.java:78) [classes/:na]
    at cl.cgr.sistradoc.pdfconverter.itextpdf7.Html2Pdf.main(Html2Pdf.java:54) [classes/:na]
18:34:13.587 [main] ERROR c.i.h.a.impl.DefaultHtmlProcessor - Worker of type com.itextpdf.html2pdf.attach.impl.tags.DivTagWorker unable to process com.itextpdf.html2pdf.attach.impl.tags.ImgTagWorker

iTxtPDF 7不支持JPG base64 html嵌入式图像吗? 谢谢您的帮助!!!

迭戈

更新2019-02-19 我的POM:

    <properties>
        <itext.version>7.1.5</itext.version>
    </properties>
  <dependencies>
  <!-- iText 7 License Key Library -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-licensekey</artifactId>
        <!-- version>2.0.4</version--><!-- for itext 7.0.4 -->
        <version>3.0.4</version><!-- for itext 7.1.5 -->
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>${itext.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>${itext.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>${itext.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>${itext.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>${itext.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>${itext.version}</version>
    </dependency>

    <!-- only needed for digital signatures -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>sign</artifactId>
        <version>${itext.version}</version>
    </dependency>

    <!-- only needed for barcodes -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>barcodes</artifactId>
        <version>${itext.version}</version>
    </dependency>

    <!-- only needed for Asian fonts -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>font-asian</artifactId>
        <version>${itext.version}</version>
    </dependency>

    <!-- only needed for hyphenation -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>hyph</artifactId>
        <version>${itext.version}</version>
    </dependency>




    <!-- pdfHTML -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.1.2</version><!-- para itext 7.1.5 -->
        <!--version>1.0.1</version--><!-- para itext 7.0.4 -->
        <!--version>1.0.0</version--><!-- para itext 7.0.3 -->
    </dependency>

    <!-- Styled XML parser is used by iText7 modules to parse HTML and XML -->
    <!-- https://mvnrepository.com/artifact/com.itextpdf/styled-xml-parser -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>styled-xml-parser</artifactId>
        <version>${itext.version}</version>
    </dependency>


    <!-- only needed for Asian fonts -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>font-asian</artifactId>
        <version>${itext.version}</version>
    </dependency>


    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.18</version>
    </dependency>

2019年2月19日更新2

使用以下方法打开html文件

1)Chrome,看起来还可以!!所有图片都可以。 2)Internet Explorer 8,相同的JPG图像,和另一个(PNG),未显示在页面上。 3)Internet Explorer 11,所有图像都可以。

这让我更加困惑。

1 个答案:

答案 0 :(得分:0)

我发现了问题。 JPG base64已损坏。 它们仅在HTML base64 JPG图像中包含许多“%”字符。将XML + XSLT转换为HTML时,JPG base 64损坏。原始XML JPG base 64中没有“%”字符。 现在,我必须看看我的trasnform操作。 谢谢。