Friday, 15 March 2019

Creating PDF in Liferay 7,.

Follow the below steps to export your data as PDF.

1) Create a Liferay Module Project build type as Maven. Her  we are using Liferay resourceURL
when button  is clicked PDF will be generated.

Add the following content in view.jsp


<portlet:resourceURL var="submitFormDetailsResourceURL" id="createPDF" escapeXml="false"/>
<br><br><br>
<form action="${submitFormDetailsResourceURL}" method="post">
<input type="submit" value="Create PDF" name="createPDF">
</form>

2) Now write the resource method in Controller class . use the below  method in your controller.

@ResourceMapping("createPDF")
public void createPdf(ResourceRequest request, ResourceResponse response) throws DocumentException, IOException
{

System.out.println("Inside create PDF");
String[] headers = new String[]{ "Instructor Id", "Course Name", "Course Description", "Instructor Name" };
     
        Document document = new Document(PageSize.LETTER.rotate());
        PdfWriter.getInstance(document,
                new FileOutputStream(new File("C:/Users/CC130/Desktop/PDF/pdf/table.pdf")));
//C:/Users/CC130/Desktop/PDF/pdf/table.pdf in this location pdf will be generated.
        document.open();
        Font font = new Font();
     
        Font fontHeader = new Font(font.TIMES_ROMAN, 12, Font.BOLD);
        Font fontRow = new Font(font.TIMES_ROMAN, 10, Font.NORMAL);
     
        PdfPTable table = new PdfPTable(headers.length);
        for (String header : headers) {
            PdfPCell cell = new PdfPCell();
            cell.setGrayFill(0.9f);
            cell.setPhrase(new Phrase(header.toUpperCase(), fontHeader));
            table.addCell(cell);
        }
        table.completeRow();
   
     
        List<pdf.creator.model.Course> courseList= new ArrayList<pdf.creator.model.Course>();
    pdf.creator.model.Course course = new pdf.creator.model.Course();
    course.setInstructorId(12323);
    course.setCourseName("java");
    course.setCourseDescription("Java Bascis");
    course.setInstructorName("Sunil");
    pdf.creator.model.Course course1 = new pdf.creator.model.Course();
    courseList.add(course);
    course1.setInstructorId(12323);
    course1.setCourseName("Js");
    course1.setCourseDescription("JS Bascis");
    course1.setInstructorName("ram");
      courseList.add(course1);
     
     
       // List<Course> courseList = CourseLocalServiceUtil.getCourses(-1, -1);
        System.out.println(courseList.size());
        for(pdf.creator.model.Course c:courseList) {

        long l = c.getInstructorId();
        String str = Long.toString(l);
table.addCell(str);
table.addCell( c.getCourseName());
table.addCell( c.getCourseDescription());
table.addCell(c.getInstructorName());


}
     
     
     
        document.addTitle("PDF Table Demo");
        document.add(table);
        document.close();
    System.out.println(" created PDF");
   
    //create csv file
    String csv = "C:/Users/CC130/Desktop/PDF/pdf/table.csv";
   
    FileWriter fileWriter = new FileWriter(csv);
    CSVWriter csvWriter = null;
    //CSVWriter writer = new CSVWriter(new FileWriter(csv));
   
 
    ColumnPositionMappingStrategy<pdf.creator.model.Course> mappingStrategy =
    new ColumnPositionMappingStrategy<pdf.creator.model.Course>();
   
    mappingStrategy.setType(pdf.creator.model.Course.class);
   
    csvWriter = new CSVWriter(fileWriter,
                CSVWriter.DEFAULT_SEPARATOR,
                CSVWriter.NO_QUOTE_CHARACTER,
                CSVWriter.DEFAULT_ESCAPE_CHARACTER,
                CSVWriter.DEFAULT_LINE_END);
   

csvWriter.writeNext(headers);

for (pdf.creator.model.Course customer : courseList) {
String[] data = {
String.valueOf(customer.getInstructorId()),
customer.getCourseName(),
customer.getCourseDescription(),
customer.getInstructorName()};

csvWriter.writeNext(data);
}
   
fileWriter.close();
csvWriter.close();
   
        //Result set

    //writer.writeAll(data);
 
    System.out.println("CSV File Created");


}


3) Here i am using Course as a Model Class in which i have already stored some records in database
here is the Model Class. You can just fetch Liferay users and create PDF. i just used my own model.

package pdf.creator.model;

public class Course {
private  long instructorId;
private String courseName;
private String courseDescription;
private String instructorName;


public Course() {
super();
// TODO Auto-generated constructor stub
}
public Course(long instructorId, String courseName, String courseDescription, String instructorName) {
super();
this.instructorId = instructorId;
this.courseName = courseName;
this.courseDescription = courseDescription;
this.instructorName = instructorName;
}
public long getInstructorId() {
return instructorId;
}
public void setInstructorId(long instructorId) {
this.instructorId = instructorId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseDescription() {
return courseDescription;
}
public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
public String getInstructorName() {
return instructorName;
}
public void setInstructorName(String instructorName) {
this.instructorName = instructorName;
}


}

4)  Make sure that you have the same dependencies in pom.xml

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>com.liferay.portal.kernel</artifactId>
<version>2.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
<scope>provided</scope>
</dependency>


<dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>pdfbox</artifactId>
         <version>2.0.1</version>
      </dependency> 
 
      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>fontbox</artifactId>
         <version>2.0.0</version>
      </dependency>
     
      <dependency> 
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>jempbox</artifactId>
         <version>1.8.11</version>
      </dependency>
       
      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>xmpbox</artifactId>
         <version>2.0.0</version>
      </dependency>
   
      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>preflight</artifactId>
         <version>2.0.0</version>
      </dependency>
   
      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>pdfbox-tools</artifactId>
         <version>2.0.0</version>
      </dependency>
     
      <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
   <dependency>

<groupId>com.lowagie</groupId>

<artifactId>itext</artifactId>

    <version>2.1.7</version>

</dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->

   
      <!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency>

<dependency>
<groupId>edcst.course</groupId>
<artifactId>edcst-course-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
  <groupId>com.opencsv</groupId>
  <artifactId>opencsv</artifactId>
  <version>4.4</version>
</dependency>



</dependencies>

5) Deploy your portlet  and it willl generate PDfF file in System's Specified Location.

No comments:

Post a Comment