TinyButStrong - the PHP Template Engine
Categories > OpenTBS with DOCX >

Generate one document with one template and several data set ?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Bruno Mannina
Date: 2017-04-10
Time: 16:39

Generate one document with one template and several data set ?

Dear all,

Actually I use a docx template to generate one string output. All work fine.

But now I'm facing a problem:
- I have a set a record.
- With the same template, I would like to process each records
- and I would like to generate only one unique output ?

I read a lot of posts without solving my problem.

Is it possible to do that ?

This post deals with the same problem as me: http://www.tinybutstrong.com/forum.php?thr=3579#m15018
Just a little difference with me I don't use a file as output but a simple string with die() function.

My code looks like:
  .......CODE HERE...
    $template = 'onepatent.docx';
    $output_file_name = $pn . '_' . date('Y-m-d').'.docx';
    $TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8);

        // --------------------------------------------
        // Merge data in the body of the document
        $TBS->MergeBlock('fam',  $fam_array);
        $TBS->MergeBlock('discussion', $discussion_array);    
        // -----------------
        // Output the result
        // -----------------
        $TBS->Show(OPENTBS_STRING);
        $string = $TBS->Source;
   
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Disposition: attachment;filename="' . $output_file_name . '"');    // Define the name of the output file
    die($string);

I try to add a foreach but it adds header/footer docx information for each record so when I try to open the result Word shows me an error.

My code:
foreach ($data as $d) {
  .......CODE HERE....
  $TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8);

        // --------------------------------------------
        // Merge data in the body of the document
        $TBS->MergeBlock('fam',  $fam_array);
        $TBS->MergeBlock('discussion', $discussion_array);    
        // -----------------
        // Output the result
        // -----------------
        $TBS->Show(OPENTBS_STRING);
        $strings[] = $TBS->Source; <======= ARRAY NOW
}
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Disposition: attachment;filename="' . $output_file_name . '"');    // Define the name of the output file
    die($strings);

Could you help me to solve my problem please ?

Thanks a lot,
Bruno






By: Skrol29
Date: 2017-04-14
Time: 01:44

Re: Generate one document with one template and several data set ?

Hi Bruno,

I'm not sure to understand what you're trying to do.

If you want to make a download with several documents, this is simply not possible. An HTTP download is for only one one document.

You cannot neither combine several DOCX in order to have one finla DOCX. This can be done only with the Ms Word application.

Maybe the solution is to have a merging foe several records on the same template, so the result is a multi-page DOCX.
By: Bruno Mannina
Date: 2017-04-14
Time: 10:56

Re: Generate one document with one template and several data set ?

Hi Skrol29,

First thanks for your reply,

I want to download one document but this document must use several times the same template.
In my database, I have several records, each record has a title, an author, a picture, a date, citation, ....

Actually with my code, I generate a docx for one record and it works perfectly but now
I need to do that for several records and the result will be a unique document Docx.

Is it possible to do that ?


By: Skrol29
Date: 2017-04-14
Time: 12:27

Re: Generate one document with one template and several data set ?

Yes, use $TBS->MergeBlock()
By: Bruno Mannina
Date: 2017-04-14
Time: 13:06

Re: Generate one document with one template and several data set ?

ok, I will see If I can use it with MergeBlock inside and picture, etc...

thanks
By: Bruno Mannina
Date: 2017-04-17
Time: 02:03

Re: Generate one document with one template and several data set ?

I used MergeBlock as you recommended me and it works perfectly !

I have one more question please, How Can I add a PageBreak after each row ?
If I add a page break at the end of my template row, it add before the row not after ??!!

Is exist a method to add a PageBreak with a line code ?

Thx !
By: Skrol29
Date: 2017-04-19
Time: 00:27

Re: Generate one document with one template and several data set ?

Yes you can add page-break, see the DOCX example, there is things about that in it.