TCPDF Textfield issues with default value - php

I'm trying to create a PDF file with a form. I'm using php and tcpdf.
The text fields need to be precisely positioned to fit the image in the background.
Some of the text fields have default values (via the 'v' attribute).
The issue is that when I click the text field to edit the contents, the text moves a few pixels to the side (probably because of the border around the active input field). When the input loses focus, the text does not return to the original position.
I've tried to visualize the problem with these screenshots:
(note the text position relative to the line below)
My form default settings:
$pdf->setFormDefaultProp(array('lineWidth'=>1, 'borderStyle'=>'solid',
'fillColor'=>array(), 'strokeColor'=>array(255, 128, 128)));
This is the code for the field:
$pdf->TextField('test', 47, 6, [], ['v' => 'test(12)'], 8, 13);
What I have tried so far:
setting the lineWidth to 0, but while the border is no longer visible, the text still moves.
setting the borderStyle to 'none', same result.
setting the strokeColor to [], same result.
What I would like to do:
set a padding to the default value (or otherwise influence its positioning)
or
remove the border from the active form field (not just make it invisible)
I noticed the same behavior on the TCPDF example here: https://tcpdf.org/examples/example_014/
If you click into the address field, the text moves up and to the left.

While I couldn't solve the problem with PHP, I managed to find a viable workaround (at least for our project):
If I insert the default values via Javascript, the effect doesn't occur.
$js = "
var fields = {'field1':'test', 'field2':'test2'};
for (var fieldName in fields) {
if (!fields.hasOwnProperty(fieldName)) continue;
var field = getField(fieldName);
if (field && field.value === '') {
field.value = fields[fieldName];
}
}
";
$this->pdf->IncludeJS($js);
This has the drawback, that if the user empties a field, the default value will be filled in again on next opening the file. This is no use case for our project, so it works for us, but keep it in mind!
So it's by no means perfect, but it helped us move on with our project. And maybe someone searching for this problem finds this helpful :)
If anyone finds an actual solution, I'd still be grateful!

I think you want to distinguish the first time a user opens the (blank) form, from when they re-open it after they saved it with the field emptied.
In the textfield, set the value (or default value) to 10 blanks. In your javascript, first check for 10 blanks, and if there, insert your default values. If the field has anything but 10 blanks, don't insert defaults. The user is unlikely to use exactly 10 blanks to clear a field.
But if that isn't robust enough, use another textfield that the user would never edit (like a version number). Start with it empty, and insert the version number (or whatever) only after you have checked the field, and inserted your set of defaults.

Related

Change image using innerHTML and show back and previous button according to result

I have images of alphabets. I want to to change the image from A to B when press next button usin innerHTML. When it is "A", only next button should display.When it is "Z", only previous button should displayed. else both button button should display..Which code will work in php??
In your case JS looks a bit better but with PHP a short hint for you. Build an array and look which image is shown in the array (the position). Then you can check if you are at the end of the array in that case the index is the length of the array.
Then you can work with if else. So i don't give you the exact code Stackoverflow is to learn and find solutions for existing problems.
$alphabet = array(
0 => 'pathtoA',
1 => 'PathtoB'
....
);
So when you now print A for example you know that its the key 0 and that 0 is the first position so you can show only the next button with an if.
count($alphabet);
With count you get the length and you can look if your current id is the last element and you show only the preview button.
This is a very simple solution but i think if you read a but you get a solution on your own.

TCPDF writeHTML()

I am working on two column layout input comes from an editor which has images and text. Images can come as one column or full width. Everything works fine when I insert images in one column. But when I insert image as 2 column it fits perfectly in the column but the text after that is somewhat not aligned.
Text covers the after space correctly but when it goes to second column some space from the top till where the image ends on the first column is blank and starts after that.
if ($this->myResetColumn) {
$this->resetColumns();
$this->setEqualColumns($this->myCols, $this->myWidth);
$this->setXY($this->GetX(), $this->GetY());
$this->selectColumn();
}
$this->writeHTML($content, true, false, true, false, $align);
Second Question
It there a way, I can make few checks while the writeHTML() function is executing or I have to change the function itself, which will not be a good idea to change the source code.
For AddPage, I override it, after the line at the end like this
$this->startPage($orientation, $format, $tocpage);
if (condition) {
// some function call
}
but writeHTML() is a lengthy function to override and it will lose many options it has.

Why isn't a .post and .html call able to compare in an if(condition) in jQuery?

The following script has been created to test if the value of a db field has changed and if so then reload the page and if not, alert the user that the change has not happened.
The alert is just to see what is being returned by the .post function.
The auto_refresh works fine as i need it to check every 5 seconds, when the if() condition is set to '==' the page alert shows and if it is set to '!=' the page continually reloads.
jQuery.post is getting the db field data but it doesn't seem to be able to compare the 2 values correctly.
any help would be greatly appreciated, thanks
var auto_refresh = setInterval(function(){
$.post("/index.php/listen", function(data) {
if($('#slide').html() != data)
{
window.location.reload()
}
else
{
alert('its the same'+ data);
}
});
}, 5000);
EDITED
Rather than trying to parse raw data, why not pass HTML from the $.post() like:
<p>4</p>
Then the jQuery inserts the the replaces the p tag with the new version from the $.post()
because the html is passed on there is no white space and the comparison can be made correctly.
I don't think it is very safe to compare the new value with an html. Some browsers might add spaces or unwanted chars. I'd try to save the old value in an input of type hidden and use the .val() or, event better, in a variable. It depends of your scenario.
If $('#slide').html() == data
then that means that the conditional failed, it was not equal, so it showed the alert.
The problem is that the data variable might come back with a few extra white spaces. If I were you, I'd try to parse a small section of the data variable, and a small section of the html in slider and compare those values.
Like if slider has something within a p tag or an input value, compare it to the data to see if it has that same value returned in that p tag or input value, then replace all the whitespaces with an empty string just to be safe.
Btw, try not to use alerts since you can't really know for sure if there is an extra whitespace. Try to use something like "debugger" if using IE with visual studios, or console.log when using chrome or firefox.
You are comparing two html strings: one is serialized from the DOM, and another is from a server response.
There's no guarantee that the two strings will ever be the same! Think about it: the same rendered html can have many string differences. E.g. click and click are both the same HTML, but different strings.
You can take two different approaches here:
You can create some kind of canonicalization routine that guarantees that two html fragments you consider "the same" will have the same string form. Run both html fragments through this routine, then compare.
You can manage versions more explicitly.
Include some kind of version indicator:
You can use the ETAG header (which means you can take advantage of http caching mechanisms).
You can include some kind of version number in the html itself (maybe in a data-version attribute), and compare those.
You can keep the html string from your server separately and compare against that.

Select text from a specific table cell on another page?

I have a weird question. I want to create script in javascript (or PHP if I have to, I'm just more comfortable in javascript) that reads through a table until it finds specific text (know how to do that) and then returns the text three cells to the right.
To be specific, I want a script that finds a row for a specific printer on this site and returns the printer's status. I know how to select unique text, but not so much about nonunique text.
Try this
$('.epi-dataTable tr:gt(0) td:first-child:contains("printerName")')
.closest('tr').find('td:eq(4)').text();
var printerName = 'yourname',
status = $(':contains(' + printerName + ')').siblings().eq(2).text();
If I understood your question correctly, this is how you could do it using jQuery:
$("td").filter(function() {
return $(this).text() === yourSeachText;
}).next().next().next().text();
Clarifying: the filter function will select only the column whose text equals your search text, and each call to next will return the column's next sibling - another td. Three calls later, you have the column you want, so you can get its text.
Update: you might also be interested in this question, if the site you're querying is not in the same domain as your script. If it is, you can simply load its contents to a div before querying it:
$("#placeholder").load("http://clusters.andrew.cmu.edu/printerstats/", function() {
// The contents were loaded to #placeholder, do your query here
});
If it's not, then you'll have to load that html data somehow, and according to an anwser to that question, your best route is really to do it in PHP (or at least use it to echo the other site's contents to your own).

substr and strlen explanation

Ok guys, this question is related to my previous one.
If I have set $textlimit = 500; that will limit my text to 500 characters.
Is there any way to "avoid" text limit, and then onclick function load rest of it?
For example, if I set:
$textpart = substr($fulltext, 0, 400);
$textpart will only contain 400 characters of string.
My question is, how to declare variable, which will contain the rest of the text which is much longer than 500 characters?
Example of variables:
$fulltext //Contains full text, but is limited to 500 characters.
$textpart //Contains part of the text, substr 400 characters out of 500.
$textrest //This variable to "hold" rest of the text, after 400 characters of $textpart.
Like I've asked in previous question, I wanted to make expand and collapse button, I now know how to do that, but I don't know how to divide text.
Form would go like this:
Random text here(400 characters long)
Random image for expand
After declared onclick function I, load rest of the text (Over 500 characters).
Random image for collapse
After declared onclick function collapse and return to previous state - citation 1.
I hope I explained my question the right way. I would really appreciate any kind of help, if I can choose, I would like just basic explanation on how to that, because I want to learn that, not copy/paste solution (it is easier, but I will not learn much).
Thanks in advance.
$textrest = substr($fulltext, 400)
$fulltext = substr($fulltext, 0, 500);
$textpart = substr($fulltext, 0, 400);
$textrest = substr($fulltext,400,strlen ( $fulltext ));
If I understand you correctly you want to show the user an initial page that shows only the first X characters and then show all the characters when the users clicks on the text.
There are three strategies to do this. From easy to hard:
Output the shortened text and include a link that will reload the whole page but with the whole text
Output all the text and use css and JavaScript to hide/show any overflow
Output the shortened text and perform an Ajax call to load the extra characters and append
Options 2 and 3 require the use of client side JavaScript and are therefore not pure PHP solutions.
Option 1 is a matter of adding a $_GET variable, e.g. ?expand=para1, to your url and expanding the text identified in PHP by $_GET['expand'].
Do not make the mistake of thinking PHP is still running on the page in the browser. Only JavaScript can run in the browser on the web page. (Not strictly true I know, but true enough in reality.)

Categories