Including CSS file to mpdf - php

I am trying to implement a CSS file to my mPDF file. Here is my mPDF file:
$pdf_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>' . get_bloginfo() . '</title>';
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
// echo $cssFile;
if (file_exists($cssFile)) {
$pdf_output .= '<style type="text/css">'.file_get_contents($cssFile).'</style>';
}
$pdf_output .= '</head>
<body xml:lang="en">
<pagebreak>
<div id="header"><div id="headerimg">
<h1>' . get_bloginfo('name') . '</h1>
<div class="description">' . get_bloginfo('description') . '</div>
</div>
</div>
<div id="content" class="widecolumn"><div id="test">Lorem ipsum dolorem sit amet</div>';
if(have_posts()) :
while (have_posts()) : the_post();
$pdf_output .= '<div class="post">
<h2>' . $pageTitle . '</h2>';
$pdf_output .= '<div class="entry">POST: ' . wpautop($post->post_content, true) . '</div>';
$pdf_output .= '</div> <!-- post -->';
endwhile;
else :
$pdf_output .= '<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn\'t here.</p>';
endif;
$pdf_output .= '</div> <!--content-->';
$pdf_output .= '
</body>
</html>';
Everything the styles.css says is completely ignored as if the file wasn't even loaded. Interestingly enough, if I append
echo '<pre><div id="output">'.$pdf_output.'</div></pre>';
exit;
to the very end I get an HTML page and everything works just fine. Where exactly is my error here? I guess it's some kind of option in the mPDF settings.

OP found out the solution from the comments, so I'll add it here just for the sake of closure:
It seems mPDF doesn't support CSS in style tags (see here: https://mpdf.github.io/css-stylesheets/introduction.html). In this case, you could replace this:
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
if (file_exists($cssFile)) {
$pdf_output .= '<style type="text/css">'.file_get_contents($cssFile).'</style>';
}
By this:
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
if (file_exists($cssFile)) {
$pdf_output .= '<link rel="stylesheet" href='.$cssFile.'></link>';
}
Or else use the alternative approach that's calling mPDF's WriteHTML() twice, once for the stylesheet and another for the body, as on the example on the documentation above.

Related

PHP Custom blog system, Duplicates message for every user

so I'm making a blog system on https://aindrigo.com/blog.php?bid=1 and for some reason the post duplicates for every user that exists.. any help?
<?php
$con = mysqli_connect("localhost","root","don't try hacking my db","data");
$bid = $_GET["bid"];
$queryb = $con->query("SELECT bid,title,content FROM blogposts WHERE bid=$bid");
$queryu = $con->query("SELECT id,username,password,avatarurl FROM accounts");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="aindev's Website">
<link rel="stylesheet" href="style.css?v=2222">
<link href="https://fonts.googleapis.com/css?family=Inter&display=swap" rel="stylesheet">
<title>Adam Indrigo</title>
</head>
<body>
<div class="start">
<h1>aindev</h1>
</div>
<div class="info">
<div class="inner">
<h1>aindev's Blog</h1>
<?php
if(!$bid){
echo "<h1>Please enter a Blog ID (bid) in the url</h1>";
}
while($row = mysqli_fetch_assoc($queryb)){
while($row2 = mysqli_fetch_assoc($queryu)){
echo "<img src='" . $row2["avatarurl"] . "?v=22222' style='max-width: 60px; max-height: 60px; border-radius: 6px;'>";
echo "<h2>" . $row["title"] . " by " . $row2["username"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
}
?>
</div>
</div>
</body>
</html>
I don't know the problem. I tried adding it so that it only shows it for the 1 user that created it. Even adding a cid tag, but that didn't work..
So apparently I made a mistake when I added the CID tag. I added the cid to the user query and not the blog query.

Echo PHP in HTML

I'm trying to show my PHP in a div in the HTML section of my web page,
here is the PHP
$result = mysqli_query($connect,"SELECT * FROM adopter");
if(mysqli_num_rows($result)>0){
while($row=mysqli_fetch_assoc($result)){
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';
I would like this to be displayed in my div in the HTML since it is always displaying this information at the top literally above my page instead of the blank space I want in my website design.
<div class="title">
</div>
<title> PHP UPDATE DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
</div>
</div>
I'm trying to put it into this section of the DIV in HTML
There is a problem about the understanding of the timing between the actions.
PHP is a server side language, so it will be processed ALWAYS before the client side language (likehtml and css)
so, if you cast echo is normal that PHP will put it on the top of the page (because is processed first, so printed first)
The way to do that is to assign the output to a variable and echo that variable is the point you need.
So instead of
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';
You should
$myvar = "";
while (youcode) {
$myvar .= 'AdopterID: '. $row['AdopterID'] . '<br>';
$myvar .= 'Name: '. $row['Name'] . '<br>';
$myvar .= 'Address: '. $row['Address'] . '<br>';
$myvar .= 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br><br>';
}
than in some point you will have your HTML:
<div>
<?php echo $myvar; ?>
</div>
i hope i was clear enough
This is what you want. You need to place your output within the <body> for it to display as intended.
<!doctype html>
<html>
<head>
<title> PHP UPDATE DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="title">
<?php
$result = mysqli_query($connect, "SELECT * FROM adopter");
if ( mysqli_num_rows($result) > 0 ) {
while ( $row = mysqli_fetch_assoc($result) ) {
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';
}
}
?>
</div>
</body>
</html>

Session with included header

I am having trouble getting this if statement to work using the included php file. I know that you have to test if the $_SESSION['name'] is set but for some reason its not letting me use this function in php to test if I set the session for the user.
Here is my Index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/mystyles.css"/>
<link href='http://fonts.googleapis.com/css?family=Comfortaa:700' rel='stylesheet' type='text/css'>
<title></title>
</head>
<body>
<div id="wrapper">
<?php include 'includes/header.php';?>
<?php include 'includes/footer.php';?>
</div>
</body>
</html>
Here is my updated header.php
<?php
echo "<header>
<div class='social'>
<ul>
<li><a href='#'><img src='images/Social Media/Facebook.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Twitter-Bird.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Google-Plus.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Linkedin.png'/></a></li>
</ul>
</div>
<div class='users'>";
if(isset($_SESSION['user']){
echo "
<ul>
<li><a href='profile.php'>Profile</a></li>
<li><a href='logout.php'>Logout</a></li>
</ul>";
} else{
echo "
<ul>
<li><a href='signin.php'>Sign-In</a></li>
<li><a href='register.php'>Register</a></li>
</ul>
</div>";
}
echo "</div>
<h1><a href='index.php'>CollegeConnection</a></h1>
</header>";
?>
Now I am getting this error
Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\CollegeConnection\includes\header.php on line 13
This never will work.
Better way:
$content = "<div class='users'>";
if(isset($_SESSION['user']){
$content .= "<ul>";
$content .= "<li><a href='profile.php'>Profile</a></li>";
$content .= "<li><a href='logout.php'>Logout</a></li>";
$content .= "</ul>";
} else {
$content .= "<ul>";
$content .= "<li><a href='signin.php'>Sign-In</a></li>";
$content .= "<li><a href='register.php'>Register</a></li>";
$content .= "</ul>";
}
$content .= "</div>";
echo $content;
What you want to do is probably :
if(isset($_SESSION['user']){
echo "
<ul>
<li><a href='profile.php'>Profile</a></li>
<li><a href='logout.php'>Logout</a></li>
</ul>";
} else{
echo "
<ul>
<li><a href='signin.php'>Sign-In</a></li>
<li><a href='register.php'>Register</a></li>
</ul>
</div>";
}

PHP variables in HTML shows up as 'zero' in page title

I have a programming newbie. I am learning from the alphabets. Pardon me of my very basic questions. I have a HTML template inside a PHP gallery script. In the title tag of the HTML two variables are placed. <title>"$gtitle $pcaption"</title> ($gtitle is gallery title and $pcaption is photo caption) My problem is $pcaption shows up as zero in the title tag of the page on the gallery page.It shows up in the title like
"example gallery-0"
( I am thinking it is because $pcaption is empty on gallery page and when a picture is clicked the title tag shows up with the caption ). My question is, is there anyway to avoid the zero showing up?
I am working on a localhost and is using PHP 5.5.12.
My HTML part is below ( Sorry if this is full of mistakes )..
// Final Output
echo <<<__HTML_END
<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head>
<base href="/viewgallery.php/">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<!-- <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/> -->
<title><? php echo $gtitle . " " . $pcaption;?></title>
<link rel="stylesheet" type="text/css" href="/styles.css">
<meta name="author" content="by .">
<meta name="Copyright" content="All rights reserved by .">
<meta name="keywords" content="$p_keywords,Photography by .">
<meta name="title" content="$gallery_listing">
<meta name="description" content="$gallery_description,$pcaption.">
<link rel="image_src" href="/photos/$cname/$pcaption/">
<link rel="icon" type="image/ico" href="/favicon.ico">
</head>
<body>
<div class="wrapper">
<h1 id="header1"><span></span></h1>
<div class="main">
<ul class="nav">
<li>HOME |</li>
<li>MY GALLERIES |</li>
<li>SEARCH |</li>
<li>ABOUT ME</li>
</ul>
$result_final
</div>
</div>
<div class="footer">
<div class="footinner">
<div id="seeker">
<form action="/searchmyway.php" method="get"><input type="text" name="q" id="search">
<input type="submit" id="find" value="Search">
<input type="hidden" name="form_id:search" value="1">
</form></div></div>
<p class="footnote"> </p>
</div>
</body>
</html>
__HTML_END;
?>
Thanks for any help...
EDIT - I have to admit this is not my own gallery script. I am trying to learn from this code. After looking at this for a long time. I see that there is an " echo " just above the HTML code. Looks like the final HTML is echoed out with the $results_final and I am trying to make changes after the echo command, may be that's where I am doing it wrong?
You can perform a test -
if(0 == $pcaption) {
echo '<title>' . $gtitle . '</title>';
} else {
echo '<title>' . $gtitle . ' - ' . $pcaption . '</title>';
}
$galleryTitle = $gtitle;
if(!empty($pcaption) && $pcaption > 0) { $galleryTitle .= " " . $pcaption; }
Put the 2 lines above right above the 'echo' line of your code and replace your title tag inside of that echo with the following.
<title>$galleryTitle</title>
That statement that is being echoed out is called a heredoc statement.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
You could use an if/then/else statement to accomplish this, something like:
echo "<title>".$gtitle;
if($pcaption != 0){echo " - ".$pcaption;}
echo "</title>";
You already know that you want to output your open and closing title tags and your $gtitle variable so you only need to write an if statement that checks the value of $pcaption and if it's not 0 then echo it. You might also want to add a check to see if $pcaption is empty (http://php.net/manual/en/function.empty.php) as well, so your new if statement would be:
if($pcaption != 0 && !empty($pcaption)){...}
This way you don't end up out putting something like <title>$gtitle - </title> and having something that looks weird.

External PHP file - parcing data from echo outside the doctype in a HTML file

I have 2 files: file1.php and file2.html. So in file1.php I have:
...
//some variables like $title and $message
echo '<p><h1>' . $title . '</h1></p>' .
'<p>' . $message . '</p>';
require('file2.html');
...
And I want to echo it in the center tag of my HTML page. But here's what happens when I open file2.html in my browser:
<p><h1>Title</h1></p><p>Message</p>
<!-- And so the doctype comes AFTER the PHP input. This ruins the whole code -->
<!DOCTYPE html>
<html dir="ltr" lang="en-us">
<head>
<title>Example</title>
<script type="text/javascript" src="/scripts/script.js></script>"
</head>
<body style="background-color: #FBFBFB;">
<p>Home</p>
<p>Contact Us.</p>
<p>Forums</p>
<center>
</center>
</body>
</html>
Your code that echo the title is used before displaying "..."
Your main page it should had the php extension instead of html.
After changing the extension insert your code into your html tags like that:
<center>
<?php
echo '<p><h1>' . $title . '</h1></p>' .
'<p>' . $message . '</p>';
require('file2.html');
?></center>
Inside your center tag, you can echo your value like this:
<center>
<?php if( isset($title) ) echo "<p><h1>" . $title . "</h1></p>"; ?>
<?php if( isset($message) ) echo "<p>" . $message . "</p>"; ?>
</center>
Note that you cannot use php if the file extension is .html. You need to change your file extension to .php before trying above solution.
First of all I would write the PHP as follows:
$var = "<p><h1>{$title}</h1></p>\n
<p>{$message}</p>";
Then name both files .php !
At the very top of your html (now php file) you put this:
<?php
include("path/to/file");
?>
then write this within your center tag:
<?php
echo $var;
?>
What you're doing is basic templating. You have a template that forms a shell around your content, and you want to embed content into that template.
Drk_alien and Console are on the money, your main problem is that you're echo statement comes before your require, so the output will follow that order.
A general solution is to make your template, file2.html, a container by converting it into something like this:
<!DOCTYPE html>
<html dir="ltr" lang="en-us">
<head>
<title>Example</title>
<script type="text/javascript" src="/scripts/script.js></script>"
</head>
<body style="background-color: #FBFBFB;">
<p>Home</p>
<p>Contact Us.</p>
<p>Forums</p>
<center>
<?php echo $content; ?>
</center>
</body>
</html>
Then, in file1.html, you can simply have:
$content = "<p><h1>{$title}</h1></p>\n
<p>{$message}</p>";
require 'file2.html'
$content will be available to file2.html and file2.html can be a general template that you can reuse for other pages.
As for .html and .php extensions, it will depend on your server settings whether .html files can be processed by PHP. The default on Apache, say, is that .html will not.

Categories