Here is a sample implementation of a CustomProcess for PDF Automation Server that routes PDF documents based on their file name. If the file name contains the word ‘test’, the document is send via email. Otherwise, the document is saved to an output folder.
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; import com.qoppa.pas.api.CustomProcess; import com.qoppa.pas.api.JobStatus; import com.qoppa.pas.api.ServerContext; import com.qoppa.pas.api.WorkingBundle; import com.qoppa.pas.api.service.EmailService; import com.qoppa.pas.api.service.LogService; import com.qoppa.pas.api.service.EmailService.Email; import com.qoppa.pdfProcess.PDFDocument; public class FileNameSample implements CustomProcess { @Override public boolean process(ServerContext context, JobStatus status, WorkingBundle bundle) { // Get the PDFDocument from the bundle PDFDocument pdfDoc = bundle.getPDFDocument(); String fileName = bundle.getInitialFileName(); // If the file name contains the word 'test', email it somewhere if (fileName.toLowerCase().contains("test")) { // Email the PDF EmailService emailService = context.createEmailService(); // The SMTP settings are initialized from the PAS Server settings // but can be changed for this particular email service emailService.setSMTPServer("mail.mycompany.com"); emailService.setSMTPFromName("\"Test Email\"<sender@mycompany.com>"); // Create a new Email object and populate the fields Email email = emailService.createNewEmail(); email.setSubject(fileName); email.setMessage("The attached document meets the criteria to be sent via email."); email.setSendList("receiver@domain.com"); try { // You still need to save the document temporarily in order to be // able to attach it to the email. File emailAttachment = File.createTempFile("tmp", "pdf"); emailAttachment.deleteOnExit(); email.addAttachment(fileName, emailAttachment); // Send the Email emailService.sendEmail(email); // Delete the temp file emailAttachment.delete(); } catch (IOException e) { e.printStackTrace(); // Log the Exception so that is shows the the PAS Manager console. LogService logService = context.createLogService(); logService.log(Level.WARNING, "Unable to email document " + fileName, e); // The WorkingBundle did not process successfully. return false; } } else { // Otherwise, save it in a folder File outputFolder = new File("c:\\outputfolder"); if (!outputFolder.exists()) outputFolder.mkdirs(); try { FileOutputStream outputStream = new FileOutputStream(new File(outputFolder, fileName)); pdfDoc.saveDocument(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); // Log the Exception so that is shows the the PAS Manager console. LogService logService = context.createLogService(); logService.log(Level.WARNING, "Unable to save document " + fileName + " to " + outputFolder.getAbsolutePath(), e); // The WorkingBundle did not process successfully. return false; } } // return true if the WorkingBundle was successfully processed, // otherwise false return true; } } |