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.
Related
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.
I'm using php xlsx writer (https://github.com/mk-j/PHP_XLSXWriter) to create my excel file and try to figure out how to force excel to make a line break.
My source data contains html tags (<br />) and i want to replace them with the correct formular. The replace function is working but i need how the string needs to be formated to force the break.
The line wrap option at format cells is enabled for the column as shown in the picture below (excel is installed german at the moment - please don't hate me for that)
What i tried so far is using CHAR(10) and CHAR(13) - altough CHAR(13) should be for mac and i am on windows.
I tried the following inputs to get my line break working. Also i tried all of the combinations with \n and \r\n instead of CHAR(10) and CHAR(13)
="text"&CHAR(10)&"text"
='text'&CHAR(10)&'text'
=text&CHAR(10)&text
The data was entered in the formular row in excel (picture below) - i think this is the only possible location to enter this isn't it?
Whatever i try my output always looks like this:
Any suggestions what i could do?
Thanks for your help!
The question got answered in https://github.com/mk-j/PHP_XLSXWriter/issues/114.
There was a release of a new brunch supporting new line character which fixed the problem.
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.
I'm trying to read a file and look for a specific element within the file. Once it finds it, I would like the script to add the element to a variable. So far I have not been able to find anything on this.
This is kind of what I'm looking for:
$var = read(($file), "<!--Everything in the comment line-->")
You should use file_get_contents(…) to read the full file at once and strpos() to locate the part of interest, or use a regex if it is something more abstract.
I am trying to make a news feed type thing in php.
I have a text file - news.txt and a php file index.php.
I have done the surrounding code and opening/closing the text file. Now I am stuck how to insert the new news item $newsnew to the top of the news.txt file and how to delete the old bottom news file in the news.txt file.
Is there any way to do this without deleting the whole file and writing it all again?
EDIT: Each news item is just a small string, say 500 characters, a single line.
Use a database.
If you really must use text files, use a different file for every news-item and name them sequentially like:
news001.txt
news002.txt
etc.
Then you can just add and delete files, read the directory and display what´s there.
Use the file() function to import the items in news.txt as an array, and use array_unshift() to add the new first item, and array_pop() to remove the last item. Join the array back into a single string and write it to news.txt:
$items = file('news.txt');
array_unshift($items, 'New item 1');
array_pop($items);
$newstext = implode(PHP_EOL, $items);
// write $newstext to the external file
If this is a XML file you could read it, parse it and delete the last child in the DOM. But if you have all your data in a DB it could be much easier to rewrite the file every time.
EDIT: after your edit: yes, you can do it like this:
write your new line to a new file
read the old file line by line and write it to the new one
skip the last line (detected by counting or EOF)
delete the old file and rename the new
No, there is not. But you might consider storing the messages in revers order. That way you only need to append to news.txt when new news arrive.
You are not going to be able to prepend to the beginning of the file without writing the whole thing out again. You could append to the end of it with the "a" mode flag to fopen(), but still to delete the oldest item you'll need to write out the entire file again.
Really, a database is solution here instead of a single text file.
There are many ways you can do it using the flat text file, but I'm not really sure it it's worth it. You can use some lightweight structured file or embedded database. For example SQLite, which would store it in normal file, no additional setup needed.