domPDF background image is not rendering in the PDF - php

<?php
include('../helper/app2.php');
$admin = new admin;
$preview = $admin->pdfPreview();
$html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title> New Document </title><meta name="Generator" content="EditPlus"><meta name="Author" content=""><meta name="Keywords" content=""> <meta name="Description" content=""> <link href="../../css/style.css" rel="stylesheet" type="text/css" /></head><body><div class="biegeBox"><div class="cardContainer"><div class="cardImg" style="background-image:url(http://'.$_SERVER['SERVER_NAME'].'/demo/'. $preview[0]['image'].'); background-repeat:no-repeat;"><div class="cardQuote">'.$preview[0]['desc'].'</div></div></div></div></body></html>';
if ( isset( $html ) ) {
require_once("dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper($_POST["paper"], $_POST["orientation"]);
$dompdf->render();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);
}
?>
The above code I am using to create pdf from dom, but its not rendering image in the pdf. Can any one help me to fix it. After reading this DOMPDF Page background (or alternatives?) question and answer I changed the background property.

This may not apply to you fully, but in my shared hosting environment the only way i was able to include images was to have the image in the same directory as the HTML file and refer to it via its relative path

Related

How to pass data to pdf in just DOMPDF?

I am stuck in a weird situation.
How can I send PHP data to dompdf's pdf ?
Here is my code
index.php
<?php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use Dompdf\Options;
function runPDF () {
$options = new Options();
$options->set('isPhpEnabled', true);
$dompdf = new Dompdf($options);
$data = array('name'=>'John Smith', 'date'=>'1/29/15');
$html = file_get_contents("pdf.php");
$dompdf->loadHTML($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("niceshipest", array("Attachment" => 0));
}
if (isset($_GET['pdf'])) {
runPDF();
}
?>
print
pdf.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Value To PDF</title>
</head>
<body>
<h1>
Hello <?php echo $data['name']; ?>
</h1>
</body>
</html>
So how can I access that $data value in generated pdf?
I have tried to change isPhpEnabled = false; to isPhpEnabled = true;
But got no results.
Framworke version of dompdf like laravel-dompdf provides feature to pass data in HTML like loadView(); but there is only loadHTML();
Any help really appreciate?
Thanks

How can I create DOM object from a HTML file in php language?

I want to parsing data from homepage on this url. As you can see this url is HTML file and I read below:
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
so I just type a code below
include "simple_html_dom.php";
$html = file_get_html('eecs.kookmin.ac.kr/site/computer/notice.htm');
echo $html->plaintext;
The error message is:
Error message Warning: file_get_contents(eecs.kookmin.ac.kr/site/computer/notice.ht‌​m): failed to open stream: No such file or directory in C:\Bitnami\wampstack-5.6.27-0\apache2\htdocs\simple_html_dom‌​.php on line 76
what should I do?
You can get the HTML code using the Snoopy Class (https://sourceforge.net/projects/snoopy). Next code displays the HTML code inside of a <textarea> tag, then it displays the page itself, copy-paste next code in a PHP file and open it in your browser:
<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-kr">
<META HTTP-EQUIV="Content-language" CONTENT="ko">
</head>
<body>
<?php
require("Snoopy.class.php"); // ◄■■ GET SNOOPY FROM https://sourceforge.net/projects/snoopy
$snoopy = new Snoopy;
$snoopy->fetch("http://eecs.kookmin.ac.kr/site/computer/notice.htm");
$html = mb_convert_encoding( $snoopy->results, "UTF-8", "EUC-KR" ); // ◄■■ GET HTML CODE.
echo "<textarea rows='25' cols='80'>$html</textarea>"; // ◄■■ DISPLAY THE HTML.
echo $html; // ◄■■ DISPLAY THE WEBPAGE.
?>
</body>
</html>
The Snoopy Class is only one file, make sure the file is in the same directory your PHP file is.

How to redirect after fopen() in php

I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).

Reading and encoding html

I am trying to read and display the content of the title (contained in a h1 tag) from many HTML files. These files are all in the same folder.
This is what the html files look like :
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>
<html>
<head>
<title>A title</title>
<style type='text/css'>
... Styles here ...
</style>
</head>
<body>
<h1>Être aidant</h1>
<p>En général, les aidants doivent équilibrer...</p>
... more tags ...
</body>
I have tried to display the content from the H1 tag with this PHP script :
<?php
foreach (glob("test/*.html") as $file) {
$file_handle = fopen($file, "r");
$doc = new DOMDocument();
$doc->loadHTMLfile($file);
$title = $doc->getElementsByTagName('h1');
if ( $title && 0<$title->length ) {
$title = $title->item(0);
$content = $doc->savehtml($title);
echo $content;
}
fclose($file_handle);
}
?>
But the output contains wrong characters. For the example file, the output is :
Être aidant
How can I achieve this output?
Être aidant
You should state a charset in the <head> of your HTML document.
<meta charset="utf-8">
you need to use utf-8 encoding
change echo $content to echo utf8_encode($content);

Can't read arabic txt file in PHP

I have so many text(.txt) files and I want to read it with PHP and display in web browser. Some of my files is in arabic language.
I am using file_get_contents function to read files. But I can't get proper result.
Here is sample of what I is my input and output.
Input Text ===> لة إلى الشعب الأردني العزيز والى شعوب العالم الحر والى المنظمات الدولية للحرية وحقوق الإنسان والى معاقل الديم
Output Text ===> J2 H'DI 49H( 'D9'DE 'D-1 H'DI 'DEF8E'* 'D/HDJ) DD-1J) H-BHB 'D%F3'F H'DI E9'BD 'D/JEHB
My page is has already UTF-8 charset. I have also tried fopen function and still same result.
What I am missing?
This works for me:
<html>
<head>
<!-- <link rel="stylesheet" href="css/style.css" /> -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<?php
$file = "arabic.txt";
$data = file_get_contents($file);
echo $data; ?>
</body>
</html>
, where arabic.txt is saved with UTF-8 encoding.
you can try this:
<html>
<meta http-equiv='Content-Type' content='text/html'; charset='UTF-8'/>
<body>
<?php
//put your file in this folder
$path='D:\test';
$files=scandir($path);
foreach ($files as $key => $value) {
if($value!="." && $value!="..")
{
print_r(file_get_contents($path."/".$value));
}
}
?>
</body>
<html>
you can see : How to print all the txt files inside a folder with php?

Categories