Java Itext生成的pdf未在Adobe reader中打开?
我正在使用itext(版本5.5.4)在服务器中创建pdf文件。当我在客户端下载文件并尝试在adobe reader中打开它时,它没有打开,并弹出一条消息说“处理页面时出错。阅读此文档时出现问题(129)” 此pdf文件在其他应用程序(如evince、foxit和google chrome)中打开效果良好。下面是我正在使用的代码部分Java Itext生成的pdf未在Adobe reader中打开?,java,server,itext,pdf-generation,Java,Server,Itext,Pdf Generation,我正在使用itext(版本5.5.4)在服务器中创建pdf文件。当我在客户端下载文件并尝试在adobe reader中打开它时,它没有打开,并弹出一条消息说“处理页面时出错。阅读此文档时出现问题(129)” 此pdf文件在其他应用程序(如evince、foxit和google chrome)中打开效果良好。下面是我正在使用的代码部分 public static String genPdfAsBase64(String orientation, JSONObject data) thr
public static String genPdfAsBase64(String orientation, JSONObject data)
throws IOException, DocumentException {
if(orientation.equals("landscape")) {
doc = new Document(PageSize.A4.rotate(), 10f, 10f, 50f, 5f);
} else {
doc = new Document();
}
JSONArray header = (JSONArray)data.get("header");
JSONArray body = (JSONArray)data.get("body");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(doc, baos);
TableHeader evt = new TableHeader();
evt.setOrientation(orientation);
writer.setPageEvent(evt);
doc.addAuthor(AUTHOR);
doc.open();
Image img = Image.getInstance(Base64.decode(BASE_64_IMG));
img.setAlignment(Image.ALIGN_MIDDLE);
img.setBorder(Rectangle.NO_BORDER);
img.scaleToFit(20f,20f);
doc.add(img);
Paragraph par = new Paragraph("Report", new Font(FontFamily.HELVETICA, 10));
par.setAlignment(Element.ALIGN_CENTER);
doc.add(par);
doc.add(new Paragraph(" "));
PdfPTable table = new PdfPTable(header.size());
table.setTotalWidth(1500);
table.setHeaderRows(1);
/*Header*/
for(Object obj : header) {
String text = (String)obj;
PdfPCell cell = new PdfPCell(new Phrase(text));
cell.setBackgroundColor(headerCol);
table.addCell(cell);
}
/*Body*/
for(int i=0; i<body.size(); i++) {
JSONArray row = (JSONArray)body.get(i);
for(Object obj : row) {
String text = String.valueOf(obj);
PdfPCell cell = new PdfPCell(new Phrase(text, sansFont));
if(i%2 != 0) {
cell.setBackgroundColor(evenCol);
}
table.addCell(cell);
}
}
doc.add(table);
doc.close();
byte[] bytes = baos.toByteArray();
baos.close();
String base64 = Base64.encodeBytes(bytes);
return base64;
}
公共静态字符串genPdfAsBase64(字符串方向,JSONObject数据)
抛出IOException,DocumentException{
if(方向等于(“横向”)){
doc=新文档(PageSize.A4.rotate()、10f、10f、50f、5f);
}否则{
doc=新文档();
}
JSONArray头=(JSONArray)data.get(“头”);
JSONArray body=(JSONArray)data.get(“body”);
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
PdfWriter writer=PdfWriter.getInstance(doc,baos);
TableHeader evt=新的TableHeader();
evt.设置方向(方向);
writer.setPageEvent(evt);
文件作者(作者);
doc.open();
Image img=Image.getInstance(Base64.decode(BASE_64_img));
img.setAlignment(Image.ALIGN_MIDDLE);
图像顺序(矩形,无边框);
内模比例配合(20f,20f);
文件添加(img);
段落=新段落(“报告”),新字体(FutoFr.Helvigi,10);
标准对齐(元素,对齐中心);
新增文件(第页);
添加文件(新段落(“”);
PdfPTable table=新的PdfPTable(header.size());
表2.setTotalWidth(1500);
表2.setHeaderRows(1);
/*标题*/
用于(对象对象对象:标题){
字符串文本=(字符串)对象;
PdfPCell cell=新的PdfPCell(新短语(文本));
cell.setBackgroundColor(headerCol);
表2.addCell(cell);
}
/*身体*/
对于(int i=0;iHi),您可以用此替换最后4行。编码前不需要关闭ByteArrayOutputStream
byte[] bytes = baos.toByteArray();
String base64 = Base64.getEncoder().encodeToString(bytes);
return base64;
上面@mkl所述的原因就是这个问题。我使用了错误的字体。我从下载的字体现在可以使用了。请共享有问题的PDF。谢谢,但我无法共享。我将尝试创建一个伪PDF。@mkl我添加了一个指向示例文件的链接。看起来问题与sansFont
有关,至少是通过删除它来解决的从共享文件中,原始版本消失了。您的代码中似乎根本没有定义该字体。因此,请在genPdfAsBase64
中定义该字体,并且不要在调用中重复使用该字体。@mkl我已删除该字体。这没有帮助。您能告诉我您是如何从pdf中删除该字体的吗?谢谢,但这没有帮助p、 pdf正在其他浏览器jusft上打开。只有adobe reader没有打开。