OpenTBS/TinyButStrong Is Not Replacing Merge Fields In DOCX - php

I'm trying to use the OpenTBS/TinyButStrong library to replace merge fields in a word document.
We can take a very basic word document like this:
Hello, My Name Is Bob, My Age Is <<BOBAGE>>
Which in word has the following code:
{MERGEFIELD BOBAGE\*MERGEFORMAT}
And my code would be basic:
$TBS = new \clsTinyButStrong();
$TBS->PlugIn(TBS_INSTALL,OPENTBS_PLUGIN);
$TBS->LoadTemplate($path,OPENTBS_ALREADY_UTF8);
$TBS->MergeField('BOBAGE','TEST');
$TBS->Show(OPENTBS_FILE,$tmpPath . 'test.docx');
When i open test.docx, the merge field isn't replaced!
It works if i use [bobage] which isn't actually a word merge field! That's not what i expected it to do, that's pretty useless.
Is there a way to replace the actual word merge fields?

The instruction ̀$TBS->MergeField() is for merging TBS fields, not Ms Word Mail Merge Fields.
TBS fields are those like [my_field] or [my_block.my_field] in your template.
So your snippet could work if you have a piece of text in your template like [BOBAGE].
By the way, OpenTBS can merges document fields if the type is IF field, but not MERGEFIELD. See the documentation and examples for more details.

Related

how to find word position in PHPWord

i just interesting using PHPWord to be my MS word file editor. lets go to the case!.
first i have ms word file named template.docx. below are the data inside the file.
Name : ${name}
Address : ${address}
Now, i want to find this using php, i want to find any "${" word, why?, because it could show what name that could be inserted dynamically, so i could foreach those later on. the problem is, its not that simple, i see OOXML are using something that seperate the wording. Question is, how to solve this problem?.
i already try this code
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
but it cannot work with me.

OpenTBS - Repeating Paragraph with Token Replacement?

I have a text-based (as opposed to table-based) .docx document with a section that has several places in the paragraph for replacement. This paragraph must be duplicated and the tokens replaced for each entry in the array. I'm continuing to comb through the documentation but am kind of stuck on this one -- most of the examples are table-based and I'm not seeing how I can accomplish what I'm shooting for.
Here's an example of the section in the .docx file I am using as a template so far:
[onshow; block=begin; personsblock=tbs:p;]
Person 1
[flex_27.xx.01] [flex_27.xx.02], currently of [flex_27.xx.05], as amazing with the following: __{this has yet to be determined}__.
[onshow; block=end; personsblock=tbs:p]
... and my data so far is:
$personarray = array();
$personarray[] = array('tID27.01.01'=> 'Steve', 'tID27.01.02'=>'Klutcher' , 'tID27.01.05'=>'', 'tID27.01.06'=>'Cook');
$personarray[] = array('tID27.02.01'=> 'Tommy', 'tID27.02.02'=>'Boonary' , 'tID27.02.05'=>'Clarksville', 'tID27.02.06'=>'Montgomery');
... At this point, I am pretty much lost. I'll be programatically replacing the center 'code' (marked by xx) with the count of the person involved. What is the difference between a merge and a replace? How I combine the action? Can I do a multi-pass on the document somehow?
Sorry if these seem such basic questions but as I said, I've been stuck on this for two days.
When you use block=begin ... block=end those is the syntax for absolute bounds. They indicate the position of the start and the end of the block in the source.
In a doc, you don't have access to the inner XML source. So it is advised to use the relative syntax for blocs.
In PHP, your data structure should be like :
$personarray = array();
$personarray[] = array('c01'=> 'Steve', 'c02'=>'Klutcher' , 'c05'=>'', 'c06'=>'Cook');
$personarray[] = array('c01'=> 'Tommy', 'c02'=>'Boonary' , 'c05'=>'Clarksville', 'c06'=>'Montgomery');
In your DOCX, your template snippet should be like :
Person 1
[person.c01;block=tbs:p] [person.c02], currently of [person.c05], as amazing ...
Assuming that those two lines are in the same paragraph.
The expression [person.01;block=tbs:p] is a TBS fields that merges column '01' and also define the block bound as the paragraph that embeds this field.
They are also some problems in your original PHP spinnet :
Column names should not has dot (.) because this is a separator character for TBS field's names.
I use the prefix 'c' because PHP will turn numeric string key as numerical index.

PHP add dynamic sections/tables to word template

I want to insert dynamic sections and table rows with images to a word template. As an example assume template contains 1-5 sections and I want to insert 6 - n sections/sub sections dynamically.
I checked the requirement with PHP Word and TinyButStrong. But both support only for replacing keywords.
I was able to achieve this using PHP Word by creating the document from the scratch but I want to do the same with template, because most of the part in the document are static, only few sections to be added dynamically and number of sections to be added are unknown.
Do you know any PHP library that meet the requirement? Is there any workaround for this?
You can create document from template and then put there new dynamic date replacing some variables with setComplexBlock functions:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_40_TemplateSetComplexValue.docx');
$inline = new TextRun();
$inline->addText('by a red italic text', array('italic' => true, 'color' => 'red'));
$templateProcessor->setComplexValue('inline', $inline);
See full example here: https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_40_TemplateSetComplexValue.php.

PHP pdf form parse regex

I have a two PDF forms that I'd like to input values for using PHP. There doesn't seem to be any open source solutions. The only solution seems to be SetaSign which is over $400. So instead I'm trying to dump the data as a string, parse using a regex and then save. This is what I have so far:
$pdf = file_get_contents("../forms/mypdf.pdf");
$decode = utf8_decode($pdf);
$re = "/(\d+)\s(?:0 obj <>\/AP<>\/)(.*)(?:>> endobj)/U";
preg_match_all($re, $decode, $matches);
print_r($matches);
However, my print_r is empty even after testing here. The matches on the right are first a numerical identifier for the field (I think) and then V(XX1) where "XX1" is the text I've manually entered into the form and saved (as a test to find how and where that data is stored). I'm assuming (but haven't tested) that N<>>>/AS/Off is a checkbox.
Is there something I need to change in my regex to find matches like (2811 0 obj <>/AP<>/V(XX2)>> endobj) where the first find will be a key and the second find is the value?
Part 1 - Extract text from PDF
Download the class.pdf2text.php # http://pastebin.com/dvwySU1a (Updated on 5 of April 2014) or http://www.phpclasses.org/browse/file/31030.html (Registration required)
Usage:
include('class.pdf2text.php');
$a = new PDF2Text();
$a->setFilename('test.pdf');
$a->decodePDF();
echo $a->output();
The class doesn't work with all pdf's I've tested, give it a try and you may get lucky :)
Part 2 - Write to PDF
To write the pdf contents use tcpdf which is an enhanced and maintained version of fpdf.
Thanks for those who've looked into this. I decided to convert the pdfs (since I'm not doing this as a batch) into svg files. This online converter kept the form fields and with some small edits I've made them printable. Now, I'll be able to populate the values and have a visual representation of the pdf. I may try tcpdf in the event I want to make it an actual pdf again though I'm assuming it wont keep the form fields.

php search and replace

I am trying to create a database field merge into a document (rtf) using php
i.e if I have a document that starts
Dear Sir,
Customer Name: [customer_name], Date of order: [order_date]
After retrieving the appropriate database record I can use a simple search and replace to insert the database field into the right place.
So far so good.
I would however like to have a little more control over the data before it is replaced. For example I may wish to Title Case it, or convert a delimited string into a list with carriage returns.
I would therefore like to be able to add extra formatting commands to the field to be replaced. e.g.
Dear Sir,
Customer Name: [customer_name, TC], Date of order: [order_date, Y/M/D]
There may be more than one formatting command per field.
Is there a way that I can now search for these strings? The format of the strings is not set in stone, so if I have to change the format then I can.
Any suggestions appreciated.
You could use a templating system like Smarty, that might make your life easier, as you can do {$customer_name|ucwords} or actually put PHP code in your email template.
Try a RegEx and preg_replace_callback:
function replace_param($matches)
{
$parts = explode(',',$matches[0]);
//$parts now contains an array like: customer_name,TC,SE,YMD
// do some substitutions and:
return $text;
}
preg_replace_callback('/\[([^\]]+)\]/','replace_param',$rtf);
You can use explode on it to separate them into array values.
For Example:
$customer_name = 'customer_name, TC';
$get_fields = explode(',', $customer_name);
foreach($get_fields as $value)
{
$new_val = trim($value);
// Now do whatever you want to these in here.
}
Sorry if I'm not understanding you.

Categories