On success of this WriteFile API it generates a .pdf file.
but when I open it through fileOpener or I open it manually while going it into that directory it gives me error that test.pdf is of Invalid Format
When I generate .docx file or .txt file it is opening and displaying the data.
Okay, then it is quite clear: You can just write plain text to a PDF file, PDF is a binary format.
Try to open a working PDF file in Notepad (or your favorite editor) to see what this means.
Yes, see what I wrote again: You can’t just create a PDF using writeFile. You can only create plain text files with that or if you replace the “HEllO WORLD” with the required binary format of PDF.