iText 7:此pdf文档可能无法正确显示Firefox
我遇到了一个奇怪的问题,即从iText7
生成的pdf.生成的pdf在Adobe reader
和Chrome 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中时,条形码图像发生了奇怪的变化:代替了
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.