Each type of file has its own internal format - a set of rules about what it takes to determine what information it should represent. Usually the file extension (.pdf) in this case is set to indicate which internal format the file uses, but there is no absolute guarantee of this.
If you write a line to a file with Python, the file will have exactly what you put in it, in this case only five ASCII characters H, e, l, l and o. This will correspond to the normal text file format. Thus, in this case, you created a text file, but added the .pdf extension on it. Its internal format is still a text file, and if you rename it to file.txt, you will find that you can simply open it (using a text editor).
If you want to create a real PDF file (something with the correct internal format for PDF), you will need to use a specialized package that can write this type of file. @gsamaras and @ rasmus-lyngdal-christensen made some good suggestions (Reportlab, PyPDF2 and fpdf).
source share