<%@ page import="ITT.WAFr.Utilities.*" %> <%@ page import="ITT.WAFr.Utilities.Upload.PostedFile" %> <%@ page import="java.io.ByteArrayInputStream" %> <%@ page import="java.io.ByteArrayOutputStream" %> <%! //****************************** //****************************** //****************************** Routine to upload, convert to PDF, ZIP, and email //****************************** //****************************** private String ExecuteToolKitDemo( HttpServletRequest request ) { //****************************** Begin Upload upload = null; //****************************** Retrieve upload object try { upload = new Upload( request ); } catch ( Exception e ) { return e.getMessage(); } //****************************** Retrieve file, email address, and base URL PostedFile posted_file = upload.File( "HTML_FILE" ); boolean has_content = posted_file.HasContent(); if ( !has_content ) { return "The uploaded HTML file contains no content"; } String email_address = upload.ElementValue( "EMAIL_ADDRESS" ); String base_url = upload.ElementValue( "BASE_URL" ); String file_name = posted_file.Name(); byte[] file_content = posted_file.Bytes(); int index = file_name.lastIndexOf( "." ); if ( index > -1 ) { file_name = file_name.substring( 0, index ); } //****************************** Generate PDF file from uploaded HTML file ByteArrayInputStream input = new ByteArrayInputStream( file_content ); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfFile pdf_file = new PdfFile(); try { pdf_file.Create( input, output, base_url ); } catch ( Exception e ) { return e.getMessage(); } //****************************** Retrieve contents of PDF file output stream file_content = output.toByteArray(); output = new ByteArrayOutputStream(); //****************************** Create new ZIP file String separator = System.getProperty( "file.separator" ); String local_folder = request.getRealPath( "/" ); ZipFile zip_file = new ZipFile( ZipFile.BestCompression ); zip_file.Add( local_folder + "include" + separator + "ThankYou.txt" ); zip_file.Add( posted_file, "Original_Html" ); zip_file.Add( file_content, ( file_name + ".pdf" ), "Converted_Pdf" ); zip_file.Create( output ); //****************************** Retrieve contents of ZIP file output stream file_content = output.toByteArray(); //****************************** Encode / Decode Email email = new Email(); StringBuffer body = new StringBuffer(); String images = local_folder + "images" + separator; //****************************** Build HTML email message with embedded images!!! body.append( "\n\n" ); body.append( "\n\n" ); body.append( "\n\n" ); body.append( "\n\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "
\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "\n" ); body.append( "
Thank You!
Thank you for sampling the Java Tool Kit online demonstration by Dan Oross Consulting. Attached is a dynamically generated ZIP file containing the original HTML file you uploaded and the PDF document converted on-the-fly.
Take notice of the embedded images and HTML within this email, and the directory structure within your ZIP file!
The Java Tool Kit: HTTP upload, streaming HTML-to-PDF conversion, streaming ZIP files, and email.
Purchase your copy of the Java Tool Kit
at www.DanOross.com


\n\n" ); body.append( "
\n" ); body.append( "
If you have received this message in error, please disregard.
\n\n" ); body.append( "\n" ); body.append( "" ); //****************************** Set the email properties try { email.To( email_address ); email.From( "Tool.Kit@DanOross.com" ); email.Subject( "Java Tool Kit Demo" ); email.Body( body.toString() ); } catch ( Exception e ) { return e.getMessage(); } //****************************** Add embedded images try { email.Attachment( images + "nav_logo.gif", "LOGO" ); email.Attachment( images + "nav_name.gif", "ITT" ); email.Attachment( images + "nav_stripe.gif", "STRIPE" ); } catch ( Exception e ) { // Don't let this stop us!!! } //****************************** Add ZIP file attachment try { email.Attachment( file_content, ( file_name + ".zip" ), "application/zip" ); } catch ( Exception e ) { return e.getMessage(); } //****************************** Send email try { email.Send( "<< SMTP_SERVER >>", "<< USERNAME >>", "<< PASSWORD >>" ); } catch ( Exception e ) { return e.getMessage(); } //****************************** Return success return "Thank you for sampling the Java Tool Kit online demonstration.\n\nYour HTML file was uploaded, converted to PDF, ZIPped, and mailed successfully.\n\nCheck you email to view the results!"; } %> <% //****************************** //****************************** //****************************** If the page is POSTED call the process routine above //****************************** //****************************** String message = ""; if ( request.getMethod().equalsIgnoreCase( "post" ) ) { message = ExecuteToolKitDemo( request ); message = message.replaceAll( "\\\\", "\\\\\\\\" ); message = message.replaceAll( "\r\n", "\\\\r\\\\n" ); message = message.replaceAll( "\r", "\\\\r" ); message = message.replaceAll( "\n", "\\\\n" ); message = message.replaceAll( "\t", "\\\\t" ); message = message.replaceAll( "'", "\\\\\'" ); } //****************************** //****************************** //****************************** When done processing or if GET page, display form //****************************** //****************************** %> Dan Oross Consulting: Java Tool Kit
O n l i n e   D e m o n s t r a t i o n
Java Tool Kit
Overview
The purpose of this page is to demonstrate the power of all four Java Tool Kit components in one simple form. Unfortunately, one simple form hardly exhibits the hundreds of features that these objects possess.
Be sure to view the online Java Tool Kit API specification for all of the methods and options available. Or feel free to email Dan Oross Consulting with any questions or comments.
• View the online Java Tool Kit API specification
• View the JSP source code for this online demonstration
• View the HTML-to-PDF conversion guide and known issues
• View the DanOross.com homepage converted to PDF with the Java Tool Kit
Instructions
For the purposes of this demonstration, you must enter your email address and select a well-formed HTML file to upload to the Dan Oross Consulting server. The Java Tool Kit will process the file UPLOAD, convert the HTML to PDF, ZIP the file and EMAIL it back in a single stream, on-the-fly, without writing any files to disk. Optionally, you may also enter an absolute base URL, like http://www.DanOross.com, to provide relative link support for stylesheets and images.
Your email address will ONLY be used to send the resulting PDF document. It will not be stored, sold or used... EVER!
The document size, margins, orientation, image and text split settings, forced page breaks, and virtual browser render width cannot be adjusted for the demonstation. The request timeout is 30 seconds and the HTML parser timeout is 15 seconds, to prevent oversized file upload and input stream processing.
T R Y   I T ! ! !
HTML Document:
Email Address:
Base URL (optional):
Submit HTML Document
Copyright ©2008 Dan Oross Consulting, Inc. All rights reserved.
Products and Web site designed and developed by Dan Oross