我正在使用 iText 生成 Pdf。但是当我尝试在 pdf 中添加图像时,
Image schoolLogo = Image.getInstance(new URL(timetableResource.getImageUrl()));
document.add(schoolLogo);
但是我收到错误
HTTP Status 500 - Server returned HTTP response code: 400 for URL: http://139.59.72.150:8080/sms/attachments/23/42/school/23/23/Vandana International School Logo.png
type Exception report
message Server returned HTTP response code: 400 for URL:(myUrl)
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.IOException: Server returned HTTP response code: 400 for URL: (myUrl) sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) java.net.URL.openStream(URL.java:1045) com.lowagie.text.Image.getInstance(Unknown Source)
最佳答案
请考虑切换到 iText7。正如 Bruno 已经指出的那样,您当前使用的版本不再受支持。 供您引用,iText7 添加图像的方式是:
String FOX = "path/to/resource/fox.png";
String DOG = "path/to/resource/dog.png";
Image fox = new Image(ImageDataFactory.create(FOX));
Image dog = new Image(ImageDataFactory.create(DOG));
Paragraph p = new Paragraph("The quick brown ")
.add(fox)
.add(" jumps over the lazy ")
.add(dog);
document.add(p);
有一个完整的入门教程,针对那些已经了解 iText 如何工作并需要一些迁移到 iText7 的指导的人。
关于java - 使用 Itext Pdf 将图像添加到 Pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45102459/