PHP add dynamic sections/tables to word template - php

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.

Related

OpenTBS/TinyButStrong Is Not Replacing Merge Fields In DOCX

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.

Algolia highlighting in Laravel 5.3

I am using Laravel 5.3 and Algolia,
I want to highlight the search result,I read the documentation but I still don't know how to do it.
https://www.algolia.com/doc/api-client/php/parameters#attributestohighlight
Any ideas?
When search results are returned from Algolia, they will wrap the "highlighted part" with <em> </em> tags by default. This happens right out of the box, so all you really need to do is use CSS to customize the look of <em>s within your search results div to get the effect you want.
Of course if you prefer that they wrap highlighted text in something other than <em> then you can customize it with anything you wish (such as maybe a span tag with a "highlighted-search" class or something). You customize this when initializing the search in your PHP.
$index = $client->initIndex('contacts');
$result = $index->search('search query', ['attributesToRetrieve' => 'firstname,lastname', 'hitsPerPage' => 50, 'highlightPreTag' => '<span class="highlighted-search">', 'highlightPostTag' => '</span>']);
Now let's say you search 'John D' and submit that search query. Algolia will return a string to the effect of:
<span class="highlighted-search">John D</span>oe
Now with your CSS you customize it like
span.highlighted-search {
background-color:yellow;
}
and now it will highlight the search query with yellow.
Of course this is all assuming you want the static (PHP Library) server side search results. I highly recommend that you use the autocomplete.js library so you can get live search results as you type. This requires using the js libraries to return results client-side while the user types. It is a much better experience.
In each item returned by the search engine, there is an extra "_highlightResult" attribute that contains some metadata and the value of the searchable attributes modified with search terms surrounded with <em>.
For instance, for the search term "toux", the returned JSON will looks like:
{
"medicament" : "VICKS TOUX SECHE 7,33 mg ADULTES MIEL, pastille",
"_highlightResult" : {
"medicament" : {
"value" : "VICKS <em>TOUX</em> SECHE 7,33 mg ADULTES MIEL, pastille",
"matchedWords" : ["toux"]
....
...
}
To highlight the search results, with the search terms, you simply have to display the attribute value under "_highlightResult" instead of the raw one.
If you are not using it already, I would recommend you to use Instantsearch.js.
Highlighting the typed keywords of the search results is dealt whitin the hits widget the same way, in its templates.item parameter.
You may find live code example of this feature here https://community.algolia.com/instantsearch.js/examples/

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.

Converting HTML code to plain TEXT with formatting available to text-files [duplicate]

This question already has answers here:
Converting HTML to plain text in PHP for e-mail
(14 answers)
Closed 8 years ago.
The task: taking HTML page and keeping only text from it with formatting available for simple text: so if there was <br> tag I'd like to convert it to /r/n, if there was a table - I'd like to keep the initial structure of this table in the resulting text and so on.
There are built-in PHP function strip_tags() which is not really fits my requirements as it will keep the contents of styles and scripts and will not keep the formatting deleting <br>, <table> and other tags.
I also have read the stack question 'strip html,css from string' but there's no answer I'm looking for.
Essentially I'm looking for a way to render an HTML page to TXT file (with no links and images). Is it possible? Is there any libraries doing this thing?
One thing you can do with this is, you can do a reverse Markdown. There are a lot of implementation of HTML to Markdown, which does the job you want. They just convert the HTML to text, including the breaks, etc.
One such implementation is html2markdown. It uses NodeJS and you just need to add this:
html2markdown("<h1>Hello markdown!</h1>")
At least, this will strip the tags and give you the result as text, that can be easily markdown-stripped, coz it has less number of characters, say #s and ---s.
There is also one more implementation of html2markdown in PHP in GitHub. The syntax is again simple:
$html = "<h3>Quick, to the Batpoles!</h3>";
$markdown = new HTML_To_Markdown($html);
And this returns you with:
echo $markdown; // ==> ### Quick, to the Batpoles!
This plugin has an ability to strip the tags too:
$html = '<span>Turnips!</span>';
$markdown = new HTML_To_Markdown($html, array('strip_tags' => true)); // $markdown now contains "Turnips!"

How to read .php file and display tagged areas within php file [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to parse django style template tags
I need some help with making a code that can read any number of .php files in a given directory, look for "tagged" areas on those pages, such as:
<? ####
# ((((((Test Area)))))
#### ?>
(some html)
<? ####
# ((((((Test Area2)))))
#### ?>
and display those "tagged" areas. I currently have code that reads .php files in a given directory and pulls text out of there between {{{{{ }}}}} (for example {{{{{Content}}}}} in the head of the page and displays it in a drop down menu. So what I am trying to do is when I select for instance Content from the drop down menu, the code would look in that directory, look for Content.php, and scan the file looking for the "tagged" areas, and then echo them. Any thoughts?
I'd research the Smarty Template system.
Essentially you're doing regex searches in long strings. So, first get the string:
$template = file_get_contents('myfile.tpl.php');
Then do a search using the regex method of your choice. Probably using the following php method like this:
$search = "/^\{\{\{\{\{somePlaceholder\}\}\}\}\}/";
$replace = $yourNode;
$scrubbedTemplate = preg_replace($search, $replace, $template);
print $scrubbedTemplate;
NOTE: My intuition is telling me that you're approaching this the wrong way. The way MVC frameworks include templates is probably better. They'd actually do an include_once('file.tpl.php') and they'd have real php variables in that file that just have values before the include is called.

Categories