We will not be writing directly to the PDF file, the content is going to be buffered as it is created. Only after the PDF document is closed, and after some rearranging, will it be sent as a PDF file to the browser for download. So, as a first step, we will need to create a function to buffer the output. As it will be used internally within the PDF class, let's make it a private function. Here you can see straight away a number of class variables being used. Let's take a moment to work through them. The $_state variable keeps track of four different states that the PDF document can be in: The state is important in this method for determining how to buffer the output. If there is an open page, output is sent to the $_pages array. For any other state it is sent to the main buffer held in $_buffer variable. This distinction is necessary because page content is handled as a separate object within PDF and hence will need extra work on it when it is finally written to the main buffer. As you will later see, the $_state variable is used elsewhere to similarly add logic according to the document state. It is recommended to use the newline (“\n”) following each output, as it is required in some cases (for example certain PDF instructions have to begin on a new line). Also, remember that PDF is case sensitive, so always follow the exact spelling of PDF syntax. The second line writes the initial header that is required to identify the file and the PDF version being followed. The version number helps PDF readers handle the file properly. This tutorial will not be covering anything exotic, so you might as well stick with version 1.3. If you do start incorporating the more advanced PDF features found in 1.5 you will need to change the version number.
blog comments powered by Disqus |