iText 7 : This pdf document might not be displayed correctly Firefox

梦想与她 提交于 2021-02-05 06:39:46

问题


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 code

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;
}

Thanks in advance.

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


回答1:


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...)

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!)

On Firefox:

On Acrobat Reader:

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



来源:https://stackoverflow.com/questions/63507162/itext-7-this-pdf-document-might-not-be-displayed-correctly-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!