create account

Java - How to export User Profile in Pdf File Format by ravik2492

View this thread on: hive.blogpeakd.comecency.com
· @ravik2492 · (edited)
$4.57
Java - How to export User Profile in Pdf File Format
![image.png](https://upload.wikimedia.org/wikipedia/en/thumb/3/30/Java_programming_language_logo.svg/419px-Java_programming_language_logo.svg.png)
Source [Here](https://upload.wikimedia.org/wikipedia/en/thumb/3/30/Java_programming_language_logo.svg/419px-Java_programming_language_logo.svg.png)

##  Learn About

In This tutorial we will learn about how to export user profile in pdf file format using java technology
example such as exporting utopian user profile or steemit profile

## Requirements

1. JDK 1.7 or above
1. Eclipse IDE
1. Tomcat Application Server 7 or above
1. Any Database installed like : Oracle, mysql etc beacause we are using HQL not sql

but change the dialect and database driver depends upon database you're using

# Steps:
### Open Eclipse IDE 
###  Craete dynamic web project and configuring tomcat server

![Screenshot (96).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966219/hb9ccvcgn0lcxi1evgxc.png)

![Screenshot (97).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966258/teaneklnmbb6xx3dfyub.png)

###  Then Create Database and tables

![Screenshot (98).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966521/z4eric8oninqpghknwqz.png)

### Create Jsp file add code:

```
<h1>${users.user_name}&nbsp;&nbsp;<span style="font-size:18px;">(${users.user_gender})</span>&nbsp;&nbsp;<a href="UsersProfileExport/${users.user_id}" class="btn btn-primary">Download Profile <i class="fa fa-download"></i></a></h1>
```

### Stablish connection and get using sessionFactory and add code to get user data from database:
```
public Users getUsers(int user_id) {
		return (Users) sessionFactory.getCurrentSession().get(Users.class, user_id);
	}
```
### Create controller and add code:
```

@RequestMapping(value="/UsersProfileExport/{userid}", method = RequestMethod.GET)
	public @ResponseBody void exportUserProfile(@PathVariable("userid") String userid, HttpServletRequest req, HttpServletResponse response) throws MalformedURLException, IOException {
		UsersBean list = prepareUsersBean(usersService.getUsers(Integer.parseInt(userid)));
		userprofilepdf.exportUserProfile(list, list.getUser_name(), req, response);
		//new UserProfilePdfs().exportUserProfile(list, list.getUser_name(), req, response);
	}

```

![Screenshot (99).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966806/bdqu7hq6oxqumibifehb.png)

### create java file thats calls via controller export metod  to write user export pdf profile:
```
package beans;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.bean.UsersBean;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
 
public class UserProfilePdf
{
	private static String USER_PASSWORD = "password";
	private static String OWNER_PASSWORD = "ravi";
	private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font title = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font value = new Font(Font.FontFamily.TIMES_ROMAN, 16);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
	
   public void exportUserProfile(UsersBean list, String filename,HttpServletRequest req, HttpServletResponse response) throws MalformedURLException, IOException
   {
      Document document = new Document();
      try
      {
    	 response.setHeader("Content-Disposition", "attachment;filename="+filename+"_Profile.pdf");
		 response.setContentType("application/octet-stream");
         PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());                  
         //writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(),PdfWriter.ALLOW_PRINTING ,PdfWriter.ENCRYPTION_AES_128);
         document.open();
         document.addAuthor("TechBlogger Development Team");
         document.addCreationDate();
         document.addCreator("TechBlogger");
         document.addTitle(""+list.getUser_name()+" Profile");
         document.addSubject(""+list.getUser_name()+" Profile");
         
         PdfContentByte canvas = writer.getDirectContent();
         Rectangle rect = new Rectangle(36, 36, 559, 806);
         rect.setBorder(Rectangle.BOX);
         rect.setBorderWidth(2);
         canvas.rectangle(rect);
         
         ServletContext context = req.getServletContext();
 		 String path = context.getRealPath("/images/favicon.png");
         Image image1 = Image.getInstance(path);
         image1.setAbsolutePosition(50f, 770f);
         image1.scaleAbsolute(25, 25);
         document.add(image1);
         String userimagepath = context.getRealPath("/images/users/"+list.getUser_img_title()+"");
                           
         Image image2 = Image.getInstance(userimagepath);
         image2.setAbsolutePosition(50f, 610f);
         image2.scaleAbsolute(140, 140);
         image2.setBorder(Rectangle.BOX);   
         image2.setBorderWidth(5);
         document.add(image2);
         
         Paragraph titlepara = new Paragraph(" "+list.getUser_name()+" TechBlogger Profile", catFont);         
         titlepara.breakUp();
         LineSeparator line = new LineSeparator();
         line.setOffset(-10);
         titlepara.add(line);
         titlepara.setAlignment(Element.ALIGN_CENTER);         
         Paragraph emptypara = new Paragraph("");
         emptypara.breakUp();
         LineSeparator line2 = new LineSeparator();
         line2.setOffset(5);
         emptypara.add(line2);
         titlepara.setAlignment(Element.ALIGN_CENTER);
         
         PdfPTable table = new PdfPTable(new float[] { 30, 25, 45 });
         table.setWidthPercentage(100);
         table.setSpacingBefore(10f); 
         table.setSpacingAfter(10f);
         
         PdfPCell imgcell = new PdfPCell(new Phrase(""));
         imgcell.setPaddingLeft(10);
         imgcell.setHorizontalAlignment(Element.ALIGN_CENTER);
         imgcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
         imgcell.setBorder(Rectangle.NO_BORDER);
                    
         PdfPCell title1 = new PdfPCell();
         Phrase nametitle = new Phrase("Name: ",title);
         Phrase emailtitle = new Phrase("Email: ",title);
         Phrase mobiletitle = new Phrase("Mobile: ",title);
         Phrase gendertitle = new Phrase("Gender: ",title);
         Phrase ccitytitle = new Phrase("Current City: ",title);
         Phrase pincodetitle = new Phrase("Pin Code: ",title);
         title1.addElement(nametitle);
         title1.addElement(emailtitle);
         title1.addElement(mobiletitle);
         title1.addElement(gendertitle);
         title1.addElement(ccitytitle);
         title1.addElement(pincodetitle);
         title1.setPaddingLeft(10);
         title1.setHorizontalAlignment(Element.ALIGN_CENTER);
         title1.setVerticalAlignment(Element.ALIGN_MIDDLE);
         title1.setBorder(Rectangle.NO_BORDER);
  
         PdfPCell value1 = new PdfPCell();
         Phrase namevalue = new Phrase(list.getUser_name(),value);
         Phrase emailvalue = new Phrase(list.getUser_email(),value);
         Phrase mobilevalue = new Phrase(list.getUser_mobile().toString(),value);
         Phrase gendervalue = new Phrase(list.getUser_gender(),value);
         Phrase ccityvalue = new Phrase(list.getUser_current_city(),value);
         Phrase pincodevalue = new Phrase(list.getUser_current_city_pincode(),value);
         value1.addElement(namevalue);
         value1.addElement(emailvalue);
         value1.addElement(mobilevalue);
         value1.addElement(gendervalue);
         value1.addElement(ccityvalue);
         value1.addElement(pincodevalue);
         value1.setPaddingLeft(10);
         value1.setHorizontalAlignment(Element.ALIGN_CENTER);
         value1.setVerticalAlignment(Element.ALIGN_MIDDLE);
         value1.setBorder(Rectangle.NO_BORDER);      
  
         table.addCell(imgcell);
         table.addCell(title1);
         table.addCell(value1);
                  
         PdfPTable table1 = new PdfPTable(new float[] { 30, 70 });
         table1.setWidthPercentage(100);
         table1.setSpacingBefore(10f); 
         table1.setSpacingAfter(10f);
                             
         PdfPCell title2 = new PdfPCell();
         Phrase collegenametitle = new Phrase("College name: ",title);
         Phrase companynametitle = new Phrase("Company Name: ",title);
         Phrase hometowntitle = new Phrase("Home Town: ",title);
         Phrase citytitle = new Phrase("City: ",title);
         Phrase collegeaddresstitle = new Phrase("College Address: ",title);
         Phrase companyaddresstitle = new Phrase("Company Address: ",title);
         Phrase permanentaddresstitle = new Phrase("Permanent Address: ",title);
         Phrase countrytitle = new Phrase("Country: ",title);
         title2.addElement(collegenametitle);
         title2.addElement(companynametitle);
         title2.addElement(hometowntitle);
         title2.addElement(citytitle);
         title2.addElement(collegeaddresstitle);
         title2.addElement(companyaddresstitle);
         title2.addElement(permanentaddresstitle);
         title2.addElement(countrytitle);
         title2.setPaddingLeft(10);
         title2.setHorizontalAlignment(Element.ALIGN_CENTER);
         title2.setVerticalAlignment(Element.ALIGN_MIDDLE);
         title2.setBorder(Rectangle.NO_BORDER);
  
         PdfPCell value2 = new PdfPCell();
         Phrase collegenamevalue = new Phrase(list.getUser_college(),value);
         Phrase companynamevalue = new Phrase(list.getUser_company(),value);
         Phrase hometownvalue = new Phrase(list.getUser_hometown(),value);
         Phrase cityvalue = new Phrase(list.getUser_city(),value);
         Phrase collegeaddressvalue = new Phrase(list.getUser_college_address(),value);
         Phrase companyaddressvalue = new Phrase(list.getUser_company_address(),value);
         Phrase permanentaddressvalue = new Phrase(list.getUser_permanent_address(),value);
         Phrase countryvalue = new Phrase(list.getUser_country(),value);
         value2.addElement(collegenamevalue);
         value2.addElement(companynamevalue);
         value2.addElement(hometownvalue);
         value2.addElement(cityvalue);
         value2.addElement(collegeaddressvalue);
         value2.addElement(companyaddressvalue);
         value2.addElement(permanentaddressvalue);
         value2.addElement(countryvalue);         
         value2.setPaddingLeft(10);
         value2.setHorizontalAlignment(Element.ALIGN_CENTER);
         value2.setVerticalAlignment(Element.ALIGN_MIDDLE);
         value2.setBorder(Rectangle.NO_BORDER); 
           
         table1.addCell(title2);
         table1.addCell(value2);
  
         document.add(titlepara);
         document.add(table);
         document.add(emptypara);
         document.add(table1);
         
         document.close();
         writer.close();
      } catch (DocumentException e)
      {
         e.printStackTrace();
      } catch (FileNotFoundException e)
      {
         e.printStackTrace();
      }
   }
}
```
Let's export or Download the Profile

![Screenshot (100).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515967483/pg6xvkllhdc08e7bib4a.png)

Congratulations! Profile is successfully exported

![Screenshot (101).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515967542/owii0sva1mrnhvsnzimh.png)

Thenks  to all !

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@ravik2492/java-how-to-export-user-profile-in-pdf-file-format">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , ,
properties (23)
authorravik2492
permlinkjava-how-to-export-user-profile-in-pdf-file-format
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":115827867,"name":"utopian-io","full_name":"Ravik2492/utopian-io","html_url":"https://github.com/Ravik2492/utopian-io","fork":false,"owner":{"login":"Ravik2492"}},"pullRequests":[],"platform":"github","type":"development","tags":["utopian-io","utopian-io","java","programming"],"users":["RequestMapping","ResponseBody","PathVariable"],"links":["https://upload.wikimedia.org/wikipedia/en/thumb/3/30/Java_programming_language_logo.svg/419px-Java_programming_language_logo.svg.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966219/hb9ccvcgn0lcxi1evgxc.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966258/teaneklnmbb6xx3dfyub.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966521/z4eric8oninqpghknwqz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966806/bdqu7hq6oxqumibifehb.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515967483/pg6xvkllhdc08e7bib4a.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515967542/owii0sva1mrnhvsnzimh.png"],"image":["https://upload.wikimedia.org/wikipedia/en/thumb/3/30/Java_programming_language_logo.svg/419px-Java_programming_language_logo.svg.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966219/hb9ccvcgn0lcxi1evgxc.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966258/teaneklnmbb6xx3dfyub.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966521/z4eric8oninqpghknwqz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515966806/bdqu7hq6oxqumibifehb.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515967483/pg6xvkllhdc08e7bib4a.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515967542/owii0sva1mrnhvsnzimh.png"],"moderator":{"account":"ms10398","time":"2018-01-15T02:54:52.081Z","flagged":true,"reviewed":false,"pending":false}}
created2018-01-14 22:08:15
last_update2018-01-15 02:54:51
depth0
children5
last_payout2018-01-21 22:08:15
cashout_time1969-12-31 23:59:59
total_payout_value3.175 HBD
curator_payout_value1.396 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12,295
author_reputation1,501,149,936,516
root_title"Java - How to export User Profile in Pdf File Format"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,558,705
net_rshares601,222,830,675
author_curate_reward""
vote details (6)
@appreciator ·
<p>This post has received gratitude of 0.44 % from @appreciator thanks to: @ravik2492.</p>
properties (22)
authorappreciator
permlinkre-ravik2492-java-how-to-export-user-profile-in-pdf-file-format-20180117t145614716z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"drotto/0.0.3"}
created2018-01-17 14:56:18
last_update2018-01-17 14:56:18
depth1
children0
last_payout2018-01-24 14:56:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length91
author_reputation55,801,543,512,306
root_title"Java - How to export User Profile in Pdf File Format"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,189,259
net_rshares0
@ms10398 ·
$0.09
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/rules).

It is in the **wrong category**.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
👍  
properties (23)
authorms10398
permlinkre-ravik2492-java-how-to-export-user-profile-in-pdf-file-format-20180115t025554498z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-15 02:55:54
last_update2018-01-15 02:55:54
depth1
children2
last_payout2018-01-22 02:55:54
cashout_time1969-12-31 23:59:59
total_payout_value0.068 HBD
curator_payout_value0.019 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length263
author_reputation27,572,487,973,390
root_title"Java - How to export User Profile in Pdf File Format"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,601,240
net_rshares9,690,072,849
author_curate_reward""
vote details (1)
@ravik2492 · (edited)
Which one right category for this post you need to mentioned
properties (22)
authorravik2492
permlinkre-ms10398-re-ravik2492-java-how-to-export-user-profile-in-pdf-file-format-20180115t034922329z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-15 03:49:30
last_update2018-01-15 03:50:00
depth2
children1
last_payout2018-01-22 03:49:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length60
author_reputation1,501,149,936,516
root_title"Java - How to export User Profile in Pdf File Format"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,609,165
net_rshares0
@ms10398 ·
Tutorials will be the right category but this post cannot be submitted again.
properties (22)
authorms10398
permlinkre-ravik2492-re-ms10398-re-ravik2492-java-how-to-export-user-profile-in-pdf-file-format-20180115t074053464z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-15 07:40:54
last_update2018-01-15 07:40:54
depth3
children0
last_payout2018-01-22 07:40:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length77
author_reputation27,572,487,973,390
root_title"Java - How to export User Profile in Pdf File Format"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,644,271
net_rshares0
@sleeplesswhale ·
<p>This post has received a 18.18 % upvote from @sleeplesswhale thanks to: @ravik2492.</p>
properties (22)
authorsleeplesswhale
permlinkre-ravik2492-java-how-to-export-user-profile-in-pdf-file-format-20180117t163507600z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"drotto/0.0.2d"}
created2018-01-17 16:35:27
last_update2018-01-17 16:35:27
depth1
children0
last_payout2018-01-24 16:35:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length91
author_reputation42,005,712,593
root_title"Java - How to export User Profile in Pdf File Format"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,209,666
net_rshares0