The entire solution would be too large and complex to post. Here are some pointers to get you going. One alternative is to use BI/XML Publisher to create the output document and launch it from a button on the page (PeopleCode and Derived/Work record). Another alternative is to use Apache POI to generate a doc and iText to generate PDF. Customers have used all of these approaches.