英文:
iText 7 : This pdf document might not be displayed correctly Firefox
问题
我在使用iText7
生成的PDF文件中遇到了奇怪的问题。生成的PDF文件在Adobe Reader
和Chrome浏览器
中可以正常打开。但是在Firefox浏览器
中打开同样的PDF文件时只能打开部分内容。在Firefox中我看到了下面的信息。奇怪的是,其他并非通过iText
生成的PDF在Firefox中可以正常渲染。
Java代码:
public static byte[] createPdf(List<String> htmlPages, PageSize pageSize, boolean rotate) throws IOException {
ConverterProperties properties = new ConverterProperties();
// 注册类路径协议处理程序,以便能够从类路径加载HTML资源
org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.register();
properties.setBaseUri("classpath:/");
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()); /** 页面大小和方向 */
} else {
temp.setDefaultPageSize(pageSize); /** 页面大小和方向 */
}
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(); // 尝试过这个操作
byteArrayOutputStream.close(); // 尝试过这个操作
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;
}
提前感谢。
编辑 1:
您可以在此处找到用于重现问题的PDF和HTML/CSS文件。
英文:
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
得分: 3
当您使用base64 URI将图像嵌入到HTML中时,条形码图像出现了奇怪的情况:在labelData/barcode.png
中,205×59位图图像被嵌入了一个39578×44的图像!(是的,这是一个几乎比高度高出一千倍的图像...)
iText的HtmlConverter
很好地嵌入了该图像,但显然Firefox在显示具有这些尺寸的图像时出现了问题,尽管(或可能是因为?)它在标签上被转换为所需的尺寸(大约是高度的四倍)。至少我的Firefox安装在这里停止绘制标签内容。 (注意,PDF内容中绘制的顺序与HTML元素的顺序不相同;特别是在PDF中,数字3232000...
会在条形码之前绘制,而不是之后!)
在Firefox上:
在Acrobat Reader上:
因此,您可能希望检查将条形码图像转换为HTML文件中的base64图像URI的过程。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论