DEPRECATED Core Java Library + PDF/A, xtra and XML Worker. Only security fixes will be added — please use iText 7 - itext/itextpdf. IText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis Java and Eclipse knowledge. IText has a hierarchical structure. Follow iText®, a JAVA PDF library. IText®, a JAVA PDF library Web Site. Other Useful Business Software. Get secure and private access to the internet. I am migrating functionality from iText5 to iText7 and we were converting html to pdf, In iText5 we had HtmlWorker/XmlWorker but in iText7 we have html2Pdf jar but I cannot check its sources in Eclipse, also there is no javadoc available, Does any one knows how we can check these API's and understand which one to Use to convert html2Pdf.
Active1 year, 9 months ago
I have a html page with text, image and I am parsing the HTML content to iText to generate the PDF. In the generated PDF,Included images are not getting displayed and , only the text is getting displayed.
If I pass the absolute path like D:/Deiva/CRs/HTMLPage/article-101-horz.jpg then the image will get printed. But if I try to print the image from the server like
http://localhost:8085/content/dam/article-101-h1.jpg or http://www.google.co.in/intl/en_ALL/images/logos/images_logo_lg.gif
then it is not getting printed in the PDF.
IText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis Java and Eclipse knowledge. IText has a hierarchical structure.
NOTE: I am using itextpdf-5.2.1.jar to generate the PDF.
My HTML Code (Article.html):
I am using the below java code for generating the PDF:
Please suggest a solution to display the image in PDF.
Thanks in advance.
Deiva
Saksham6,23844 gold badges2727 silver badges5656 bronze badges
DeivaDeiva
5 Answers
I think you can do it easily using a Servlet for viewing the image. How to write a servlet for this is here
Here a sample dispatcher for you. Just edit the required places as needed
Sanjaya LiyanageSanjaya Liyanage3,54677 gold badges2828 silver badges4848 bronze badges
Here are some examples: https://developers.itextpdf.com/examples/xml-worker-itext5/html-images
If the HTML file you're parsing is stored in a directory that is different from the working directory, iText won't be able to create Image objects. We have to supply an implementation of the ImageProvider interface that tells iText what to do if an img tag is encountered. This interface has the following methods:
You can write your own class implementing these four methods, or you can subclass AbstractImageProvider. It is preferred to do the latter. XML Worker will use the store() method of the AbstractImageProvider class to cache all the Image objects that are encountered in a Map. These objects will be reused when the retrieve() method is called for an image with the same src. If you don't cache images, your PDF will be bloated. The same image bits and bytes will be written to the PDF more than once. The reset() method clears the cache; it is used when an ImageProvider is cloned. Finally, the getImageRootPath() method isn't implemented.
If the HTML file you're parsing is stored in a directory that is different from the working directory, iText won't be able to create Image objects. We have to supply an implementation of the ImageProvider interface that tells iText what to do if an img tag is encountered. This interface has the following methods:
You can write your own class implementing these four methods, or you can subclass AbstractImageProvider. It is preferred to do the latter. XML Worker will use the store() method of the AbstractImageProvider class to cache all the Image objects that are encountered in a Map. These objects will be reused when the retrieve() method is called for an image with the same src. If you don't cache images, your PDF will be bloated. The same image bits and bytes will be written to the PDF more than once. The reset() method clears the cache; it is used when an ImageProvider is cloned. Finally, the getImageRootPath() method isn't implemented. You have to implement it yourself, as is done in the following snippet:
Bruno Lowagie64.8k1010 gold badges8282 silver badges122122 bronze badges
sanjiv ranjansanjiv ranjan
to show image with Itext, you have to Changing the default configuration about Image Provider Like it :i do it from http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html
Abdourahmane FALLAbdourahmane FALL
Try getting the image into a memory or byte stream object and then cast that image object to an itextsharp image one.
explore the overloads of
iTextSharp.text.Image
EDIT:
Although the code is in C#, it might help you.
Get image from your local drive as:
Note:: If you have the image on your application folder then we have functions to get the local file path of them in C#. Don't know about Java. Images from external site can be downloaded as
Now convert this byte stream to an image object as
Now acreate an iTextSharp image object from it and add it to your doucment as
Hope this helps you.
SakshamSaksham6,23844 gold badges2727 silver badges5656 bronze badges
I also faced to same issue..
but it was working with the image absolute path. it seems not working with remote path.What id did here is save the image in temp location of file system and generates pdf , finally delete the image file from temp location.
Dinesh-SriLankaDinesh-SriLanka
Not the answer you're looking for? Browse other questions tagged javaitextsharpitextrelative-pathhtml-to-pdf or ask your own question.
This article talks about reading and writing PDF using iText PDF library.
pom.xml
P.S Tested with iTextPdf 5.5.10
1. iText – Write PDF
iText
PdfWriter
example to write content to a PDF file.Output, a new PDF file is created –
/tmp/itext.pdf
How To Convert Html File To Pdf
2. iText – Read PDF
iText
PdfReader
example to read above PDF file.Output
Java Itext Html To Pdf 中文
3. Talk
The code above uses 2 major classes –
PdfWriter
and PdfReader
. As indicated by the name, these classes provide the base for reading and writing a pdf. Document
object is basically a Pdf file which is being addressed. Paragraph
is a content type that can be written to the Pdf. Other possible content types include Anchor
, Chapter
, Section
, List
, PdfPTable
etc. All these classes help to create a specific type of content as per the requirement in the pdf. iText pdf is the most convenient library with its latest version supporting HTML to Pdf, Image to Pdf as well as QR codes. The only drawback of the iText pdf library is that it is complex to work with it. The class structure is tough to understand.
Note
More iText PDF examples
More iText PDF examples