I want to generate the pdf and save it in library?

Saving in WordPress library can be done by using this function media_handle_upload.

PDF can be created in many ways. The best method depends on your runtime enviroment and the functionality you are looking for. There are several PDF generating libs for PHP out there, for example PDFlib. But again your question is far broad.