this is a basic example of having different "views". In one view you output your data to a html table. But now you want a different view in which the data is outputted as PDF. I would suggest looking into the package "itext", which you can find here. The website also has plenty of examples if you take the effort to look for them:
http://www.lowagie.com/iText/