当文件转换为PDF时,为什么格式会更改?

问题描述

为此,我想将HTML页面转换为pdf,我使用了flying-saucer-pdf-itext5。添加了flying-saucer-pdf-itext5和flying-saucer-core依赖项,但是在生成pdf文件时,其格式会更改一些CSS不适用于PDF文件。 我的代码是这样的:

        String xHtml = convertToXhtml(htmlContent);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(xHtml);
        renderer.setScaletoFit(true);
        renderer.layout();
        final File pdfFile = new File("fileName.pdf");
        OutputStream outputStream = new FileOutputStream(pdfFile);
        renderer.createPDF(outputStream);
        outputStream.close();

要将HTML转换为XHTML,我编写以下代码

            Tidy tidy = new Tidy();
            tidy.setInputEncoding(UTF-8);
            tidy.setoutputEncoding(UTF-8);
            tidy.setXHTML(true);
            ByteArrayInputStream inputStream = new 
            ByteArrayInputStream(htmlContent.getBytes(UTF-8));
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            tidy.parseDOM(inputStream,outputStream);

pom.xml是:

<dependency>
           <groupId>org.xhtmlrenderer</groupId>
           <artifactId>flying-saucer-core</artifactId>
           <version>9.1.6</version>
           <scope>compile</scope>
   </dependency>
   <dependency>
           <groupId>org.xhtmlrenderer</groupId>
           <artifactId>flying-saucer-pdf-itext5</artifactId>
           <version>9.1.6</version>
           <scope>compile</scope>
   </dependency>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...