java - 使用 Itext Pdf 将图像添加到 Pdf 文件

标签 java spring pdf itext

我正在使用 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 的指导的人。

查看http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-1-introducing-basic-building-blocks

关于java - 使用 Itext Pdf 将图像添加到 Pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45102459/

相关文章:

java - 如何在 IBM WebSphere 7.0 中的所有应用程序中更改 .jsp 处理错误页面

java - 无法执行目标 org.openjfx :javafx-maven-plugin

java - HttpURLConnection 随机延迟

angularjs - 属性 'finally' 在类型 'Observable<Object>' 上不存在

php - 使用PHP错误查看存储在MySQL数据库中的PDF文件

java - 嵌套异常是 java.lang.IllegalStateException : Cannot convert value of type [java. lang.String] 到所需类型

java - 如何基于@Profile 向 spring-security 添加过滤器?

Spring webSocket - 使用 IE8 拒绝基于用户权限的主题订阅 - 从未调用过确定用户()

python - 正则表达式匹配非常慢

c# - 让 CefSharp 正确处理 WPF 中的 PDF 文件