I've built a program that receives data from a management system and creates a PDF file inside a server folder, the amount of PDFs created by the end of the day are 300 more or less. Those PDFs are created during the day as the users create their orders, once the program creates the PDF the user sends the PDF attached by email to somebody.
My problem is, a random PDF gets corrupted during the day while sending it by email, like only one arrives corrupted in the server.
I've checked the generated PDFs and they aren't corrupted in the creation folder, I've also added a size check before sending it by email and they always have the correct size, the problems happens while sending them.
I've also tried to change the way it was being created and sent, implemented the creation in different ways but nothing worked for me.
Do you guys have any idea of what could cause it? It's strange that it happens to only one of many.
Thanks in advance.
Follow my code below for the PDF generation:
String sCurrentLine = "";
Document document = null;
PdfContentByte cb = null;
PdfReader reader = null;
PdfImportedPage page = null;
Paragraph top = null;
PdfWriter writer = null;
BufferedReader bufferReader = null;
SmbFileOutputStream sfosWritter = null;
SmbFileInputStream sfosReader = null;
SmbFile smbFile;
String[] doc = null;
//*****************************************************************************************
try{
doc = docs_folder.split("/");
smbFile = Utilities.authentication(docFolder, domain, user, pwd);
// Creates the pdf file inside the server's folder
sfosWritter = new SmbFileOutputStream(smbFile);
// Reads the pdf's template inside the server's folder
sfosReader = new SmbFileInputStream(Utilities.authentication(templateFolder,
domain, user, pwd));
// Creates an empty pdf document
document = new Document(PageSize.A4);
writer = PdfWriter.getInstance(document, sfosWritter);
document.open();
cb = writer.getDirectContent();
reader = new PdfReader(sfosReader);
page = writer.getImportedPage(reader, 1);
// Document's configuration
document.newPage();
cb.addTemplate(page, par1, par2);
// Do while there are pages to read in the Spool File
do {
bufferReader = new BufferedReader(new InputStreamReader(spoolFile, charset));
if(bufferReader.ready()) {
while ((sCurrentLine = bufferReader.readLine()) != null) {
top = new Paragraph(floatLeading, sCurrentLine, FontFactory.getFont(FontFactory.COURIER, font));
top.setPaddingTop(paddingTop);
document.add(top);
}
document.newPage();
}
}while(spoolFile.nextPage());
document.close();
}catch (Exception e) {
e.printStackTrace();
} finally {
// closing FileOutputStream
try {
if(sfosWritter != null && sfosReader != null) {
sfosWritter.close();
sfosReader.close();
}
} catch (IOException io) {/*Failed to close*/
io.printStackTrace();
}
}
Sending email code:
DataSource source = null;
MimeMessage message;
MimeBodyPart attachmentBodyPart, textBodyPart;
MimeMultipart multipart;
Session session = null;
Properties properties = null;
MimeMessage message = null;
boolean attach = true;
try {
properties = System.getProperties();
properties.setProperty("mail.smtp.host", server_ip);
properties.put("mail.smtp.port", server_port);
session = Session.getDefaultInstance(properties);
message = new MimeMessage(session);
message.setHeader("Disposition-Notification-To", from);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
if(!copy.isEmpty())
message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(copy));
textBodyPart = new MimeBodyPart();
attachmentBodyPart = new MimeBodyPart();
multipart = new MimeMultipart();
message.setSubject(subject);
if(attach && !attachmentName.isEmpty()) {
source = new FileDataSource(new StringBuilder(attachmentPath).append(attachmentName).toString());
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachmentName);
multipart.addBodyPart(attachmentBodyPart);
}
if(!body.isEmpty()) {
textBodyPart.setText(body);
textBodyPart.setContent(body, "text/html");
multipart.addBodyPart(textBodyPart);
}
message.setContent(multipart);
message.saveChanges();
return message;
} catch (Exception e) {
e.printStackTrace();
}