Formatting a printable document with PHP

I am developing a site for generating a form with PHP & MySQL whereby a user puts in details (selections / static and freetext) and it produces the form in a printable format at the end.

The problem is the form has to be identical to the hand written / word printed forms. The main issue I have is when it goes over 1 page long.

The form used has a large header on first page (including how many total pages in the form) and then each subsequent page has a header with page number X of Y on it and every page has a signature space in footer.

Whilst I can emulate the form design fine using HTML there is no way to distinguish if form goes onto more than 1 page and how many pages there are etc…

Can anyone suggest any ideas on best way to generate the page and have those items inserted?

I have tried FPDF which has page counting and can insert total pages and page numbers but as it is a totally new class to me it was taking forever to try and emulate design off the form.

Have found some simple word document generators which would be ideal but no idea how to use them to insert headers / footers / page numbers.

Well, I have been very busy answering various questions lately. A large number of questions can be very easy to handle. This one is a bit tricky. First, a little more info will be needed. I assume you are not limiting the text areas to certain number of characters. (That would solve it, but, limit it’s usefulness.) Next, since freeflow text can be any number of lines there is no easy way to solve this. Basically, you would need to use separate strings. One for the headers, footers and actual data. The header string would be the normal salutation, such as “Dear sir, here is your report”, whatever. Footer something like “Sincerely/r/n/r/n/r/nYour Boss!”. LOL

The data string would be the total data to be printed between top and bottom static info. Once all of these are calculated, you would combine them $report=$header.$data.$footer. Since the header and footer would always be a set size, you just have to “PAGINATE” the data.

To paginate data we would need to know the formatting of the input fields first. If the data can always be split on a line break, that is easy. Just count the letters and line breaks and end on the previous line when over the max number of lines allow in the data string. Standard printouts using standard font-size is 80 chars per line. Start there, every line can have any number of chars up to 80. You can use this code to count lines: $lines = count( explode( “\n”, $_POST[‘textarea’] ) ); You could set your textarea to 80 cols across and that might be all you need. (I have not tested that, it’s up to you.)

Or, you can parse the textarea counting lines and chars, which is tricky. If a word ends at 3 spaces before the end of the line and the next word is 8 chars long, then you have to adjust for that and call it the end of the line. You can search in a ton of ways. Test the textarea setting to 80 chars and if that is no-go, we can create a script to pull characters into lines.

Good luck…

Thanks for your reply. Unfortunately this confirms my fears that there is no simple answer:/

The form generated is an incident report, it contains a lot of static text (e.g. introduction into your job role), some basic entries (e.g. date, time) and then a lot of free text (e.g. what happened) which would be impossible to predict.

Does anyone know of any free (or cheap) word document generators which would allow footers / field codes that would allow me to output something “This document consists of {pages} pages” which would then be completed by word when the document is opened?

Failing that I think it is going to have to be just printing the result in standard text and getting user to copy/paste into word document which is not ideal but only feasible method I can see atm.

Well, you last mentioned an introduction and basic entries. These would go into the header I mentioned earlier. The Footer would be the closing. So, you basically just have to paginate the incident free-form text.

A couple further ideas:

You could limit character input per page. If they need another page, there could be a button to continue.

Or, if MS Office is on all the systems this would run on you could send it to that with headers and footers, but, I think that is a whole lot of other problems.

I did a LOT of internet searching for you. I find a lot of people asking this same question. And, very little answers. I assume you will have to do this with character counting. Usually, free-text input is run together until the user presses return. At that point, the text is considered a paragraph. You could just cut off your text at that point when it has exceeded a max number of characters. This would be an easy routine. Basically, just take text at length max and count back looking for a return. When found that is a page. Of course, if the user does not press return, you have to cut it off at the end of a sentence or even at a word level.

Another point is that you could show the user a preview of their layout and then allow them to edit it. You could just show x number of characters and then show a page-break. The user may alter the text to finish neatly. That would be using ONCHANGE inside of a textarea and using Javascript to alter what is displayed. When the user goes over, let’s say 1600 chars (20 lines of 80 chars), you would add something like CRLF+"-----page break----"+CRLF… The user would see this appear and realize next page.

I realize I did not give you a real answer, but, one of these ideas might help…

Sponsor our Newsletter | Privacy Policy | Terms of Service