iText 7:此pdf文档可能无法正确显示Firefox [英] iText 7 : This pdf document might not be displayed correctly Firefox

查看:154
本文介绍了iText 7:此pdf文档可能无法正确显示Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,即从iText7生成的pdf.生成的pdf在Adobe readerChrome browser中正确打开.但是同一PDF会在Firefox浏览器中部分打开.我在Firefox中收到以下消息.奇怪的是,其他不是通过iText生成的pdf也可以在firefox中正确渲染.

I am running into strange issue with generated pdf's from iText7. The generated pdf's are opening properly in Adobe reader and Chrome browser. But the same pdf is opening partially in the Firefox browser. I am getting the below message in Firefox. The strange thing is other pdf, which are not generated via iText are properly rendering in firefox.

Java代码

public static byte[] createPdf(List<String> htmlPages, PageSize pageSize, boolean rotate) throws IOException {

    ConverterProperties properties = new ConverterProperties();

    // Register classpath protocol handler to be able to load HTML resources from class patch
    org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.register();
    properties.setBaseUri("classpath:/");
    // properties.setBaseUri(baseUri);

    FontProvider fontProvider = new DefaultFontProvider(true,false,false);
    properties.setFontProvider(fontProvider);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfDocument pdf = new PdfDocument(new PdfWriter(byteArrayOutputStream));
    PdfMerger merger = new PdfMerger(pdf);

    for (String htmlPage : htmlPages) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument temp = new PdfDocument(new PdfWriter(baos));
        if(rotate) {
            temp.setDefaultPageSize(pageSize.rotate()); /** Page Size and Orientation */
        } else {
            temp.setDefaultPageSize(pageSize); /** Page Size and Orientation */
        }
         HtmlConverter.convertToPdf(htmlPage, temp, properties);
        temp = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
        merger.merge(temp, 1, temp.getNumberOfPages());
        temp.close();
    }
    pdf.close();

    byteArrayOutputStream.flush(); // Tried this

    byteArrayOutputStream.close(); // Tried this

    byte[] byteArray = byteArrayOutputStream.toByteArray();

    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    try (FileOutputStream fileOuputStream = new FileOutputStream("D:\\Labels\\Label_"+timestamp.getTime()+".pdf")){
        fileOuputStream.write(byteArray);
    }
    return byteArray;
}

谢谢.

您可以在此处找到pdf和html/css.

Edit 1: You can find pdf and html/css for reproducing issue here.

推荐答案

当您使用base64 URI将图像嵌入到html中时,条形码图像发生了奇怪的变化:代替了您嵌入了39578×44的图像! (是的,图像的宽度是高度的近一千倍...)

When you embedded the images into your html using base64 URIs, something weird happened to the image of the barcode: Instead of the 205×59 bitmap image in labelData/barcode.png you embedded a 39578×44 image! (Yes, an image nearly a thousand times wider than high...)

iText HtmlConverter可以很好地嵌入该图像,但是显然Firefox在显示具有这些尺寸的图像时存在问题,即使(或可能是因为?)将其转换为标签上所需的尺寸(大约比高度高四倍) .至少我的Firefox安装在此处停止绘制标签内容. (请注意,PDF内容中的绘制顺序与HTML元素的绘制顺序相同;尤其是在PDF中,数字3232000...是在条形码之前绘制的,而不是在条形码之后绘制的!)

The iText HtmlConverter embedded that image just fine but apparently Firefox has issues displaying an image with those dimensions even though (or probably because?) it is transformed into the desired dimensions (about four times wider than high) on the label. At least my Firefox installation stops drawing label contents right here. (Beware, the order of drawing in the PDF content is not identical to the of the HTML elements; in particular in the PDF the number 3232000... is drawn right before the barcode, not afterwards!)

在Firefox上:

在Acrobat Reader上:

On Acrobat Reader:

因此,您可能需要检查HTML文件中条形码图像到base64图像URI的转换.

Thus, you may want to check the transformation of the bar code image to the base64 image URI in your HTML file.

这篇关于iText 7:此pdf文档可能无法正确显示Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆