Hy,
I use dompdf and when generating the pdf, the respective menu and the footer from the "mother" page are included. I don't know where to edit the code to exclude the menu and footer.
Code controller:
function export_invoice($param1 = 'export' ) {
$page_data['action'] = $param1;
$page_data['page_name'] = 'export_invoice';
$page_data['page_title'] = get_phrase('export_invoice');
$this->load->view('frontend/'.get_frontend_settings('theme').'/index', $page_data);
$html1 =$this->load->view('frontend/'.get_frontend_settings('theme').'/index',$page_data, true);
$html = mb_convert_encoding($html1, 'HTML-ENTITIES', 'UTF-8');
$this->pdf->loadHtml($html);
$this->pdf->set_paper("a5", "portrait" ); //landscape
$this->pdf->render();
// FILE DOWNLOADING CODES
$url = current_url();
$str = substr(strrchr($url, '/'), 1);
$str1=$str;
$fileName = 'Invoice-'.$str.'.pdf';
$this->pdf->stream($fileName, array("Attachment" => 0)); //initial era 1 pentru descarcare si 0 pentru preview in broswer
}
And view:
<html>
<head>
<meta charset="utf-8">
<title>PDF Output</title>
<style>
/* your style here */
</style>
</head>
<body>
{contents}
</body>
</html>
Image from pdf
I use css and it works with:
.menu-area{display: none;}
Related
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
lets assume that I want to organize my pages as following:
<?php include('header.inc.php'); ?>
my-page-content
<?php include('footer.inc.php'); ?>
in the header we have .css files and in the footer we have .js files.
how do I do if I want to load only the CSS and JS that the current page needs?
for example, on the article page I don't need to load the JS resources that manage maps and calendar.
Personally, I think it is useless to insert other files into html that will never be used - for cache management purposes. The smaller the html cache space used, the more efficient and powerful the html page will be.
Consider the following example:
file: library.php
<?php
function includeFiles(string $typeFile, array $source_arr, array $request_file): array
{
$tmp = [];
$element = $typeFile === "css" ? "<link rel=\"stylesheet\" href=\"%s\">" : "<script src=\"%s\"><script>";
foreach ($request_file as $file) {
if (array_key_exists($file, $source_arr)) {
array_push($tmp, [sprintf($element, "https://example.com" .$css[$file])]);
}
}
return count($tmp) > 0 ? $tmp : false;
}
// Make a list of all .js and .css files using the php array:
$css = [
// List all the .css files you are using here
"css1" => "/css/css1.css",
"css2" => "/css/css2.css",
"css3" => "/css/css3.css",
"css4" => "/css/css4.css",
"css5" => "/css/css5.css"
];
$js = [
// List all the .js files you are using here
"js1" => "/js/js1.js",
"js2" => "/js/js2.js",
"js3" => "/js/js3.js",
"js4" => "/js/js4.js"
];
?>
file: main_html.php
<?php
include "library.php";
$css_files = ["css1", "css3", "css5"];
$headers = implode(PHP_EOL, includeFiles("css", $css, $css_files));
$js_files = ["js1", "js3", "js5"];
$footer = implode(PHP_EOL, includeFiles("js", $js, $js_files));
?>
<!-- Here html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<?php echo $headers; ?>
<!-- and other your required head element parameters -->
</head>
<body>
<!-- includes .js files -->
<?php echo $footer; ?>
</body>
</html>
I am using HTML2PDF library of PHP to create PDF from HTML. My page url is as below ::
http://domain.com/admin/invoice/invoicedownload
To create pdf using below code ::
ob_start();
$width_in_mm = 240;
$height_in_mm = 250;
$html2pdf = new HTML2PDF('P', array($width_in_mm,$height_in_mm), en, true, 'UTF-8', array(15, 10, 15, 10));
$html2pdf->setDefaultFont('Arial');
$html2pdf->pdf->SetTitle('Invoice Details');
$html2pdf->writeHTML($pdf_template);
$html2pdf->Output($file_name,'I');
die();
This code open pdf view in browser new tab.
Issue I am facing is that that new tab title is set as "Invoice Details - invoicedownload" but I want it as "Invoice Details". Always url's last part is append in title.
Please help me for this.
Thank you in advance.
In order to define your own tab title, you can use the 'embed' html tag.
File n°1 (main):
html, body, embed
{
width: 100%;
height: 100%;
}
body
{
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Your title</title>
</head>
<body>
<embed src=/path-to-your-pdf type='application/pdf'/>
</body>
</html>
File n°2 (/path-to-your-pdf):
<?php
$html2pdf = new Html2Pdf('P, 'A4', 'en');
$html2pdf->writeHTML($pdf_template);
$html2pdf->output();
Hope this helps ;)
I'm using DOMDocument class in order to change link tag attribute when the page is load.
The tag that I want to manipulate is inside iframe block, so my code doesn't take effect in that specific case.
Here's the code:
$page_content = file_get_contents($page_link);
$dom = new DOMDocument();
$dom->loadHtml($page_content);
$links = $dom->getElementsByTagName('link');
foreach( $links as $k => $link ){
if( $link->getAttribute('rel') === 'stylesheet' ){
$link->setAttribute('rel', 'test'); //just fot testing
}
}
$newHtml = $dom->saveHtml();
echo $newHtml;
But only the main tag is affected, while the <link> inside the iframe block don't:
<head>
<link rel="test" href="style.css"/>
</head>
<body>
.
.
.
.
<iframe>
<head>
<link rel="stylesheet" href="style.css"/><!--"rel" stays the same-->
</head>
</iframe>
.
.
.
</body>
<footer>
</footer>
Much appreciate your help!
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);