java - 使用itext将html转成PDF,PDF空白的问题。
ringa_lee
ringa_lee 2017-04-18 10:21:03
0
0
648

我将一个html转换成Pdf,结果转出的PDF是空白的。

经过排查,导致的原因是里面有一个最外层的p设置了CSS样式为width: 800px;

这个数字如果设置成660px就没有问题,再大就会空白。

我使用的itext是5.5.6版本。请问各位大神,这是为什么?或者,能否帮忙分析一下?

Java代码如下

public static byte[] htmlToPdf(String html) throws Exception {

        ByteArrayInputStream htmlin = null;
        Document document = null;
        PdfWriter writer = null;
        ByteArrayOutputStream pdfout = null;

        CloudSignFontProvider fontProvider = new CloudSignFontProvider();

        try {

            pdfout = new ByteArrayOutputStream();
            htmlin = new ByteArrayInputStream(html.getBytes());
            document = new Document(PageSize.A4, 50, 50, 50, 50);
            writer = PdfWriter.getInstance(document, pdfout);
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer, document, htmlin, null, fontProvider);
            writer.flush();
            pdfout.flush();

            document.close();
            document = null;

            byte[] pdfdata = pdfout.toByteArray();
            return pdfdata;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw ex;
        } finally {
            if (writer != null) {
                writer.close();
            }
            if (document != null) {
                document.close();
            }
            if (htmlin != null) {
                htmlin.close();
            }
            if (pdfout != null) {
                pdfout.close();
            }
        }
    }
ringa_lee
ringa_lee

ringa_lee

全員に返信(0)
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!