ankit
ankit

Reputation: 25

Adding image to Pdf file using Itext Pdf

I am using iText to generate Pdf. But when I'm trying to add images in the pdf,

Image schoolLogo = Image.getInstance(new URL(timetableResource.getImageUrl())); document.add(schoolLogo);

But I'm getting error as

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)

Upvotes: 0

Views: 7595

Answers (3)

ankit
ankit

Reputation: 25

I have solved the problem, the problem is with the spaces in the url. I have replaced the blank spaces with '%20' and it working perfectly fine.

Upvotes: 0

Joris Schellekens
Joris Schellekens

Reputation: 9057

Please consider switching to iText7. As Bruno already indicated, you are currently using a version that is no longer supported. For your information, the iText7 way of adding an image would be:

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);

There a complete jumpstart tutorial, aimed at those people who already know how iText works and need some pointers migrating to iText7.

Check it out at http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-1-introducing-basic-building-blocks

Upvotes: 1

pikamorfo
pikamorfo

Reputation: 31

400 https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#HTTP_BAD_REQUEST

obviously is a error of connection, check if url is valid please and you can do log in normally.

Upvotes: 0

Related Questions