package net.viralpatel.pdf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class GeneratePDF {
public static void main(String[] args) {
try {
OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
document.add(new Paragraph("Hello World, iText"));
document.add(new Paragraph(new Date().toString()));
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code language: Java (java)
In above code snippet we have created Document object which represents our PDF document. Also, by supplying OutputStream object to getInstance() method sends the output to OutputStream. Thus in our case we have created a output file and sent output to it. package net.viralpatel.struts.helloworld.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* @author KiranRavi_Hegde
*
*/
public class PdfHelloWorldAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Document document = new Document();
try{
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.add(new Paragraph("Hello Kiran"));
document.add(new Paragraph(new Date().toString()));
}catch(Exception e){
e.printStackTrace();
}
document.close();
return null;
}
}
Code language: Java (java)
If you notice in above code, we have passed response.getOutputStream()
object to getInstance()
method. Thus the output generated by iText will be sent directly to the response. Also don’t forget to set the content type of the response to application/pdf. document.addAuthor("Kiran Hegde");
document.addCreationDate();
document.addCreator("iText library");
document.addTitle("Hello World PDF");
Code language: Java (java)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Nice post..I will use this in my shopping cart for invoice generation. Is it capable of generate the pdf from html code? I mean I have html formatted document with all the tables and lists and I want same formats in pdf.
I am a newbie. Could you explain in detail how to add .jar files to classpath. I appreciate your help.
Hi James,
CLASSPATH is the environment variable that your JVM uses to find Jar files and load the classes. You can put the jar file in class file by either specifing its path in jvm by -classpath attribute:
java -classpath c:\lib\ HelloWorld
Or by setting it in CLASSPATH environment variable.
Can we convert PDF file to DOC file?
Hi Pankaj,
iText jar provides functionality for creating PDF files only. For generating a DOC file you may want to use Apache POI.
I am not sure if there is any library available to convert JAR file to DOC. I will update once I come across.
Thanks.
Great post and it works good for me .
Satisfies my need and thanks for the information
I am working in android to convert file to pdf conversition can some one give me the solution fo thr cobnversition of the file to pdf conversiton in android.
Thank you so much for this post.
While i had found itext i was unsure how to make it print out into a local file.
this guide helped immensly and was so simple Thanks alot :-)
Great post .
I have generated Dynamic PDF file having barcode in IT using IText.
I have one simple question regarding this.
In web application if user (client) does not have Accrobat Reader (PDF Reader), then how can we manage this?
Is there any idea?
I would appreciate any information.
Thanks in advance.
Hi Samir,
I am afraid user may not be able to see generated PDFs if she does not have any PDF viewer installed. User must install PDF viewer (Acrobat Reader for eg.) in order to view the PDFs.