I am showing a table in HTML on my web page where a check mark is shown properly (I am using ✔ for the bold check mark).
I am using classic-asp to display the HTML. The html buildup (tableOutput) is then posted to a PHP script ($output = $_POST["output"]) that uses mPDF.php (version 6.0) to print to PDF the same HTML table and somehow the check mark wont print correctly (%u2714 in printed on my PDF). All the rest of the table is printed correctly, only the check mark has a problem.
I tried adding the Symbola.ttf and Symbola_hint.ttf fonts in the mpdf\ttfonts folder but it didnt do the trick.
HTML (classic-asp):
tableOutput = tableOutput & "<TD class='center pass_x' style='font-family:symbola;'>✔</TD>"
PHP (create_report_pdf.php):
$output = $_POST["output"]; //This is the html buildup tableOutput discussed previously
$header = Irrelevant;
$footer= Irrelevant;
$mpdf = new mPDF( 'utf-8', 'A4-L', 0, '', 5, 5, 20, 20);
$mpdf->allow_charset_conversion = true;
$mpdf->WriteHTML($style,1);
$mpdf->SetHTMLHeader( $header );
$mpdf->SetHTMLFooter( $footer );
$mpdf->WriteHTML( $output );
$mpdf->Output($temp ,'F' );
config_fonts.php (I addded symbola.ttf and Symbola_hint.ttf in the mpdf\ttfonts folder):
$this->fontdata = array (
"symbola" => array (
'R' => "Symbola.ttf",
'I' => "Symbola_hint.ttf",
),
CSS (PHP $style variable points to create_report_pdf.css):
.report-table{
border: 1px solid black;
border-collapse: collapse;
font-size: 7pt;
width: 100%;
}
th,td{
font-size: 7pt;
border: 1px solid black !important;
vertical-align : middle;
}
.center{ text-align: center; vertical-align: middle; }
INPUT{
border-color:#ffffff !important;
}
.pf{ width: 45px; }
.fix-cell{ width: 90px; }
.dr-column, .dr-input{ width: 100px; }
.comments, .comments-input{ width: 130px; }
Thank you very much for your help
There are two possible solutions:
Replacing manually the %u2714 with the respective html entity ✔ by doing the following:
$output = str_replace('%u2714', '✔', $output);
Implement more generic approach that will handle all such special chars. The approach is based on the fact that %uXXXX is a non-standard notation scheme for URL-encoding Unicode characters. So you need to convert %uXXXX notation to HTML entity notation &#xXXXX; and then this can be decoded to actual UTF-8 by html_entity_decode.
$output = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $output);
$output = html_entity_decode($output, ENT_COMPAT, 'UTF-8');
I am trying to set the background of the html body dynamically. Basically if a file exists, use it, otherwise use the default. But it keeps using the default regardless of whether the file exists or not.
<style>
body
{
padding-top: 50px;
background-image: url("<?php
clearstatcache();
if(file_exists("/profile_img/".$profileData["ID"]."_bg.jpg"))
{
echo "/profile_img/".$profileData["ID"]."_bg.jpg?". rand(5, 15);
}
else
{
//echo "/profile_img/".$profileData["ID"]."_bg.jpg?". rand(5, 15);
echo "/profile_img/default.jpg?". rand(5, 15);
}
?>");
background-size: cover;
background-position: 50% 50%;
}
</style>
I have tried using the file (the commented line) and it works. I can not see why this doesn't work
Some issues:
Using / will be absolute, causing it to look in the root directory.
Always check vars are set before using.
All that's changing is the filename, so you can use a ternary, which will reduce alot of that code.
<?php
$background = isset($profileData["ID"]) && file_exists('./profile_img/'.$profileData["ID"].'_bg.jpg')
? $profileData["ID"].'_bg.jpg' : 'default.jpg';
?>
<style>
body {
padding-top: 50px;
background-image: url("/profile_img/<?= $background.'?_='.microtime(true) ?>");
background-size: cover;
background-position: 50% 50%;
}
</style>
If it's still not working:
Check the file actually exists.
I'm building emails from external variables of CSS and HTML and then combining them as inline styles. Before they are combined, however, I want to alter the CSS.
In the below, I want to remove the padding and margin from the body. How should I do this - preg_match or is there something "cleaner"?
$css = "body {
background: #fff;
color: #000;
padding: 0;
margin: 0;
font: 83%/1.4 verdana,sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
a, a:link, a:visited, a:hover, a:active {
background: transparent;
text-decoration: none;
cursor: pointer;
}";
Also, it's well worth mentioning that I am getting the CSS (then inline-styling) like this with the below so maybe the DOMDocument can help me - that seems cleaner, but I don't know where to start.
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
$dom = new DOMDocument();
#$dom->loadHTML($row['htmlbody']); //from a database
$cssobj = $dom->getElementsByTagName('style');
$bodyobj = $dom->getElementsByTagName('body')->item(0);
$plainbody = new DOMDocument();
foreach ($bodyobj->childNodes as $child){
$plainbody->appendChild($plainbody->importNode($child, true));
}
$body = $plainbody->saveHTML();
$css = $cssobj->item(0)->nodeValue;
$cssToInlineStyles = new CssToInlineStyles();
$originalemail = $cssToInlineStyles->convert(
$body,
$css
);
If you know the exact text that you want to remove, you can use a str_replace() from php. Example: $str_replace('margin:0;','',$css)
or
$str = preg_replace('/margin:(.*?);/', '', $css)
How can I set style to var_dump() function and PHP errors style, like on the next image?
At the moment I have next view of var_dump() (with <pre>var_dump(...)</pre>, without it will be all in one line) and just plain text for errors.
I searched something for PHP colored errors, var_dump styles, but could not find anything.
I used OpenServer as localhost, and on previous version I had same styles for errors, but now just plain text. Is it real to customize?
Use this code. I've been using it for years. I can't even remember where it originally came from.
function var_dump_pretty($data, $label='', $return = false) {
$debug = debug_backtrace();
$callingFile = $debug[0]['file'];
$callingFileLine = $debug[0]['line'];
ob_start();
var_dump($data);
$c = ob_get_contents();
ob_end_clean();
$c = preg_replace("/\r\n|\r/", "\n", $c);
$c = str_replace("]=>\n", '] = ', $c);
$c = preg_replace('/= {2,}/', '= ', $c);
$c = preg_replace("/\[\"(.*?)\"\] = /i", "[$1] = ", $c);
$c = preg_replace('/ /', " ", $c);
$c = preg_replace("/\"\"(.*?)\"/i", "\"$1\"", $c);
$c = preg_replace("/(int|float)\(([0-9\.]+)\)/i", "$1() <span class=\"number\">$2</span>", $c);
// Syntax Highlighting of Strings. This seems cryptic, but it will also allow non-terminated strings to get parsed.
$c = preg_replace("/(\[[\w ]+\] = string\([0-9]+\) )\"(.*?)/sim", "$1<span class=\"string\">\"", $c);
$c = preg_replace("/(\"\n{1,})( {0,}\})/sim", "$1</span>$2", $c);
$c = preg_replace("/(\"\n{1,})( {0,}\[)/sim", "$1</span>$2", $c);
$c = preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim", "$1<span class=\"string\">\"$2\"</span>\n", $c);
$regex = array(
// Numberrs
'numbers' => array('/(^|] = )(array|float|int|string|resource|object\(.*\)|\&object\(.*\))\(([0-9\.]+)\)/i', '$1$2(<span class="number">$3</span>)'),
// Keywords
'null' => array('/(^|] = )(null)/i', '$1<span class="keyword">$2</span>'),
'bool' => array('/(bool)\((true|false)\)/i', '$1(<span class="keyword">$2</span>)'),
// Types
'types' => array('/(of type )\((.*)\)/i', '$1(<span class="type">$2</span>)'),
// Objects
'object' => array('/(object|\&object)\(([\w]+)\)/i', '$1(<span class="object">$2</span>)'),
// Function
'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&object)\(/i', '$1<span class="function">$2</span>('),
);
foreach ($regex as $x) {
$c = preg_replace($x[0], $x[1], $c);
}
$style = '
/* outside div - it will float and match the screen */
.dumpr {
margin: 2px;
padding: 2px;
background-color: #fbfbfb;
float: left;
clear: both;
}
/* font size and family */
.dumpr pre {
color: #000000;
font-size: 9pt;
font-family: "Courier New",Courier,Monaco,monospace;
margin: 0px;
padding-top: 5px;
padding-bottom: 7px;
padding-left: 9px;
padding-right: 9px;
}
/* inside div */
.dumpr div {
background-color: #fcfcfc;
border: 1px solid #d9d9d9;
float: left;
clear: both;
}
/* syntax highlighting */
.dumpr span.string {color: #c40000;}
.dumpr span.number {color: #ff0000;}
.dumpr span.keyword {color: #007200;}
.dumpr span.function {color: #0000c4;}
.dumpr span.object {color: #ac00ac;}
.dumpr span.type {color: #0072c4;}
';
$style = preg_replace("/ {2,}/", "", $style);
$style = preg_replace("/\t|\r\n|\r|\n/", "", $style);
$style = preg_replace("/\/\*.*?\*\//i", '', $style);
$style = str_replace('}', '} ', $style);
$style = str_replace(' {', '{', $style);
$style = trim($style);
$c = trim($c);
$c = preg_replace("/\n<\/span>/", "</span>\n", $c);
if ($label == ''){
$line1 = '';
} else {
$line1 = "<strong>$label</strong> \n";
}
$out = "\n<!-- Dumpr Begin -->\n".
"<style type=\"text/css\">".$style."</style>\n".
"<div class=\"dumpr\">
<div><pre>$line1 $callingFile : $callingFileLine \n$c\n</pre></div></div><div style=\"clear:both;\"> </div>".
"\n<!-- Dumpr End -->\n";
if($return) {
return $out;
} else {
echo $out;
}
}
You get the colored output when you ínstall and enable Xdebug:
Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.
You can enable/disable this with the ini setting xdebug.overload_var_dump
By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to 1. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.
Check the documentation for further information.
Note that you do not want to have the Xdebug extension installed on a production server as it will significantly slow down code execution.
In xdebug 3 there is no more xdebug.overload_var_dump setting. When you upgrade to xdebug 3 and you use xdebug.mode=debug as said in xdebug upgrade guide, you will not get colored output anymore. To get the same result as it was in xdebug 2 you must set xdebug.mode to develop,debug
xdebug.mode=develop,debug
Of course if you only want colored output you can only use develop mode but then you lost step debugging. With both options you get colored output and also step debugging. You can specify as many modes as you want/need. They must be separated with ,. All available modes are explained here https://xdebug.org/docs/all_settings#mode
You can also use this extension for a colored debug: (this is very easy to install)
http://www.sitepoint.com/var_dump-introducing-symfony-vardumper/
Symfony VarDumper is a component designed to replace your var_dumps. It performs essentially the same functionality, but provides you with much, much more information in a much prettier format. It’s the var_dump you’ve always wanted.
Xdebug is what you are looking for.
Sample scripts for install on Ubuntu:
[Search for Xdebug]
$ apt-cache search xdebug
[Install Xdebug]
$ sudo apt-get install php-xdebug
[Restart Apache to make it work]
$ sudo /etc/init.d/apache2 restart
In latest version of xdebug (now is 3) you should just :
1 . open php.ini
2 . change (xdebug.mode=debug) to (xdebug.mode=develop,debug)
found a couple answers here on StackOverflow and used them as my models, but I must be missing something. I'm trying to set a couple of background colors dynamically in CSS based on what is in my database, but it's not working - when I check Inspect Element in Chrome, background-color has a line through it and a warning mark for 'Invalid property value'.
Here's my code; it's in two separate files - the first is in the header include file, and the second is in the linked .php / css-esque file.
Header include: [Edited 4/29 to include session code]
session_start();
// check if $_SESSION was set before
if (!isset($_SESSION['email'])) {
header("Location: bad_login.php");
exit();
}
$_SESSION['companyid'] = $_POST['companyid'];
$companyID = $_SESSION['companyid'];
$email = $_SESSION['email'];
require_once('../includes/_connection.inc.php');
$connect = dbConnect('read');
$sql = 'SELECT colorone, colortwo, logo
FROM companies
WHERE companyid = ' . $companyID;
$result = $connect->query($sql) or die(mysqli_error());
$row = $result->fetch_assoc();
$colorOne = '#' . $row['colorone'];
$colorTwo = '#' . $row['colortwo'];
$carrierLogo = '/companylogos/' . $row['logo'];
PHP/CSS file:
<?php header("Content-type: text/css");
?>
#main {
width: 85%;
margin: 0 auto;
padding: 0.75em 0;
}
#colorOne {
width: 100%;
height: 12px;
background-color: <?php echo $colorOne; ?>;
}
#colorTwo {
width: 100%;
height: 7px;
background-color: <?php echo $colorTwo; ?>;
}
EDIT 4/29:
This is the CSS generated:
#main {
width: 85%;
margin: 0 auto;
padding: 0.75em 0;
}
#colorOne {
width: 100%;
height: 12px;
background-color: ;
}
#colorTwo {
width: 100%;
height: 7px;
background-color: ;
}
I also echoed the variable back in the html so I know that there should be something in the variable. Should I be opening the database and assigning the variable inside the css.php file?
CSS/PHP is linked this way in header:
<link type="text/css" rel="stylesheet" href="../css/carrier.php">
Instead of using the .css file extension, use .php
in the html file: is it linked to .php?
<link rel='stylesheet' type='text/css' href='css/style.php' />
in the style.php add
<?php
header("Content-type: text/css; charset: UTF-8");
?>
Now you can set up variables for whatever you like:
source
Edit:
Don't forget about session_start(); since you're using sessions (I don't understand how, since nothing gets posted to css/carrier.php you should rather have it in session from a different file & then just use the $companyID = $_SESSION['companyid'];
$email = $_SESSION['email'];).
is this the way your code looks?
<?php
session_start();
header("Content-type: text/css; charset: UTF-8");
$_SESSION['companyid'] = $_POST['companyid'];
$companyID = $_SESSION['companyid'];
$email = $_SESSION['email'];
require_once('../includes/_connection.inc.php');
$connect = dbConnect('read');
$sql = 'SELECT colorone, colortwo, logo
FROM companies
WHERE companyid = ' . $companyID;
$result = $connect->query($sql) or die(mysqli_error());
$row = $result->fetch_assoc();
$colorOne = '#' . $row['colorone'];
$colorTwo = '#' . $row['colortwo'];
$carrierLogo = '/companylogos/' . $row['logo'];
?>
#main {
width: 85%;
margin: 0 auto;
padding: 0.75em 0;
}
#colorOne {
width: 100%;
height: 12px;
background-color: <?php echo $colorOne; ?>;
}
#colorTwo {
width: 100%;
height: 7px;
background-color: <?php echo $colorTwo; ?>;
}
The answer of yesitsme is correct. Other thing you can do is that each storage changes in the database, run the process of creating this "new" CSS file with the appropriate .css extension.
What if with every request you create a new CSS file?
I mean, you have two paths, when creating the first call to the Web and update it from time to time, either, at the time you keep the data in the database associating it to a script.
With this new CSS and stored is generated through fwrite () and other functions that PHP has to manage files, keep the name of the CSS created in the BDD and then in your place the link as appropriate.