My intention is to export a list to PDF. The list originally came from an XML file, then sorted and put into a list.
I am creating a web interface that would instantiate the export to PDF function. The code below allows a PDF to be created however when I open the PDF, it says PDF failed to load.
I think my issue is in the pdfAsc function but I am not sure. Would it help if I made it into a table? If so, can anyone guide me to a tutorial on how to create tables with itext library that allows for this type of list?
This is my code:
package com.wywm.superconsole.controller;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;
import com.wywm.superconsole.Functions.PDF;
import com.wywm.superconsole.Functions.Troops;
import com.wywm.superconsole.user.User;
import com.wywm.superconsole.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@Controller
public class AppController {
@Autowired
private UserRepository userRepo;
@GetMapping("")
public String viewHomePage() {
return "index";
}
@GetMapping("/index")
public String logoutPage() {return "index";}
@GetMapping("/menu")
public String menuPage() {return "menu";}
@GetMapping("/register")
public String showRegistrationForm(Model model) {
model.addAttribute("user", new User());
return "signup_form";
}
@PostMapping("/process_register")
public String processRegister(User user) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);
userRepo.save(user);
return "menu";
}
@GetMapping("/users")
public String listUsers(Model model) {
List<User> listUsers = userRepo.findAll();
model.addAttribute("listUsers", listUsers);
return "users";
}
// Reads data from an XML file and copys the data to a List (List<User>
// userList).
public List<Troops> getTroops() {
List<Troops> TroopList = new LinkedList<>();
try {
// File path to the XML file.
Path filePath = Paths.get("C:\\Users\\amani\\OneDrive\\WYWM\\Java\\Capstone
1\\dataset.xml");
File file = new File(String.valueOf(filePath.toAbsolutePath()));
if (file.exists()) {
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document =
documentBuilder.parse(String.valueOf(filePath.toAbsolutePath()));
// Reads the XML tagName of full_name and id.
NodeList[] user = { document.getElementsByTagName("full_name"),
document.getElementsByTagName("id") };
for (int i = 0; i < user[0].getLength(); i++) {
String fullName = user[0].item(i).getTextContent();
int id = Integer.parseInt(user[1].item(i).getTextContent());
Troops newTroop = new Troops(fullName, id);
TroopList.add(newTroop);
}
} else {
System.out.println("File not found");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Returns the TroopList with data from the XML file.
return TroopList;
}
@RequestMapping(value = "/numasc", method = RequestMethod.GET)
public String listAsc(Model model) {
List<Troops> ascList = getNumAsc();
model.addAttribute("AscList", ascList);
return "numasc";
}
public List<Troops> getNumAsc( ) {
List<Troops> ascList = new LinkedList<Troops>();
ascList.addAll(getTroops());
ascList.sort((u1, u2) -> u1.getId() - u2.getId());
return ascList;
}
@RequestMapping(value = "/numdesc", method = RequestMethod.GET)
public List<Troops> getNumDes() {
List<Troops> desList = new LinkedList<Troops>();
desList.addAll(getTroops());
desList.sort((u1, u2) -> u2.getId() - u1.getId());
return desList;
}
public void pdfAsc(HttpServletResponse response) throws DocumentException, IOException {
try {
// Create Document instance.
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
// Create OutputStream instance.
OutputStream outputStream = new FileOutputStream(
new File("C:\\Users\\amani\\OneDrive\\WYWM\\Java\\Capstone
1\\asc.pdf"));
// Create PDFWriter instance.
PdfWriter.getInstance(document, response.getOutputStream());
// Open the document.
document.open();
// Create asc List
com.itextpdf.text.List ascList = new
com.itextpdf.text.List(com.itextpdf.text.List.ORDERED);
ascList.add(new ListItem(String.valueOf((getNumAsc()))));
// Add casdList to the pdf.
document.add(ascList);
// Close document and outputStream.
document.close();
outputStream.close();
System.out.println("\n" + "\n" + "PDF created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
private void exportToPDFAsc(HttpServletResponse response, List<Troops> players) throws
DocumentException, IOException {
response.setContentType("application/pdf");
DateFormat dateFormatter = new SimpleDateFormat("yyyy-dd-MM_HH-mm");
String currentDateTime = dateFormatter.format(new Date());
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=Ascending List of Troops" +
currentDateTime + ".pdf";
response.setHeader(headerKey, headerValue);
List<Troops> listAscTroops = getNumAsc();
PDF PdfAsc = new DataPDFExporter(listAscTroops);
PdfAsc.export(response);
}
@GetMapping ("/pdfasc")
public void exportToPDFASC(HttpServletResponse response) throws IOException,
DocumentException {
exportToPDFAsc(response, getNumAsc());
}
private class DataPDFExporter extends PDF {
public DataPDFExporter(List<Troops> Troops) {
this.Troops = Troops;
}
}
@GetMapping("/ExportToPDFDesc")
public void pdfDesc() {
try {
// Create Document instance.
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
// Create OutputStream instance.
OutputStream outputStream = new FileOutputStream(
new File("C:\\Users\\amani\\OneDrive\\WYWM\\Java\\Capstone
1\\desc.pdf"));
// Create PDFWriter instance.
PdfWriter.getInstance(document, outputStream);
// Open the document.
document.open();
// Create reverseOrder list.
com.itextpdf.text.List descList = new com.itextpdf.text.List(com.itextpdf.text.List.ORDERED);
descList.add(new ListItem(String.valueOf((getNumDes()))));
Collections.reverseOrder();
// Add descList.
document.add(descList);
// document.add(unorderedList);
// Close document and outputStream.
document.close();
outputStream.close();
System.out.println("\n" + "\n" + "PDF created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}