Generate Word RTF documents with php

Create RTF files by combining a document with data stored on the Intranet/Extranet.

In many web developments, the need arises to create documents that come from the combination of a source document with the data stored in the company’s Intranet/Extranet, something similar to what is achieved with Word’s mail merge.

Practical examples may be the following:

Label generation from a customer or item database

Certificates or diplomas for students in a course

List of participants in an event.

Controlling printing from an HTML document is quite complex, so generating a DOC document, or rather RTF, will allow us to solve our problem in a very effective way.

In this article we will develop a practical example, that of obtaining an attendance certificate for each of the students who attend a course.

The strategy:

Our strategy is based on the following steps:

We start from the document in Word format that we want to obtain.

We convert the .DOC format into .RTF. (.RTF files are human readable text)

We introduce in the .RTF document some manual codes that will be replaced by the data coming from the database.

We create a PHP application that reads the .RTF file and replaces the codes entered with the searched data.

First step, Prepare the template:

For our example, we start from a .DOC document that certifies that a student has taken a course. In the following image we see a type certificate.

It will be necessary to create a certificate for each of the students who have taken a course, the data that varies are those of the student and the course taken.

See also  Running a NodeJS app in production with PM2

Once the document is opened with Word, we save it in RTF format and replace each variable data with easily recognizable codes. Be careful, the RTF format is very bloody, so it will be necessary to write the codes without getting confused so that strange characters are not introduced.

In the case of the example we use the # and the * in such a way that, for example, we substitute the student’s name for #*NAME*#

We will save the document with the name template.rtf

Second step, The PHP program:

We will create two helper functions, the first one is called leef().

As can be seen, this function receives the name of the RTF file as an argument, opens it and stores the content in the text variable $todo. This function therefore returns the content of a file.

The next function, rtf(), is the one that creates an RTF from a template and replacing the codes with the data from the intranet.

This function takes four arguments:

$sql: the sql statement that returns the data that is needed

$template: the RTF template with the codes

$foutput: the name of the output file

$matequivalences: an array with the equivalences between the data coming from the sql statement and the fields of the certificate.

Let’s discuss the program line by line:

Lines 13 and 14 determine the name and path of the output file, we use the time() function to ensure that two files with the same name will not be produced.

In line 17 the content of the template is stored in the variable $txttemplate

See also  How to paginate results with PHP and MySQL

In lines 19 to 24 the body of the RTF document is extracted, the body of the document is what is repeated, to extract it we first obtain the header of the document, the header is determined by the rtf sectd tag. An rtf document always ends in }, with that information we extract the body.

In step 3, from line 26 we are writing the result file.

In line 27 we write the header of the document

In line 28 we execute the query and in line 29 we enter a loop, in each cycle we will obtain the data of a student and create a certificate.

In line 30 we create a copy of the body of the document called $later, on that copy we will make the changes.

In line 31 we enter a loop that goes through the matrix of equivalences, line 32 obtains the data from the database, line 34 obtains from the matrix the code to be replaced in the template and line 35 replaces the data with the code.

Once all the fields have been substituted, line 37 writes the student’s certificate and lines 38 and 39 generate a page break to write the next certificate again.

Once we have left the While that is extracting the data, line 41 closes the rtf document and 42 the writing of the file.

Third step, Call to the RTF function:

Once we have created these two functions, we can call the rtf() function and generate the document.

We comment the lines:

See also  Page that counts and shows the results of the survey

In line 48 we specify what the template is.

In line 49 we establish the SQL sentence that extracts the data, in the case of the example it is a bit complex since it extracts the data from several tables.

In lines 50 to 71 we create the equivalence matrix, it is a two-dimensional matrix that relates the code that will be found in the RTF file with the SQL statement field.

Line 72 calls the rtf() function

And finally lines 73 and 74 generate a link to the result file.

Improvements and comments.

Everything in this life is capable of being improved, in this case I still have to generate the output file directly to the browser, in order not to have to write it to the hard drive.

In addition, we have developed examples of lists and labels that we can document in subsequent articles.

An important aspect to continue progressing on this subject is knowing the syntax of rtf documents, it is a bit of a hard job that we can also address in later articles.

Loading Facebook Comments ...
Loading Disqus Comments ...