Set Custom Page Title for HTML2PDF created pdf - php

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 ;)

Related

dompdf exclude menu and footer

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;}

DomDocument inserting CSS from file in style element

I am building a system where the requirement says no links to CSS are allowed. They do allow to place all CSS content within the style element.
I am using DOMDocument to build the XML/XHTML.
The CSS stylesheets are around 320 lines so I would pefer to construct them in separate CSS files and solve the insertion of the CSS content in the DomDocument build.
Question:
What is the best way of inserting an external CSS file content
and get it in place between the DOMDocument built style element?
Index.php
<?php
$xml = new DomDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$html = $xml->createElement('html');
$xml->appendChild($html);
$head = $xml->createElement('head');
$html->appendChild($head);
//
$style = $xml->createElement(
'style',
'css-content....' // The CSS content from external file should be inserted here.
);
$style->setAttribute('type', 'text/css');
$head->appendChild($style);
echo $xml->saveXML();
Main.css
body {
background-color: pink;
}
Wanted result
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<style type="text/css">
body {
background-color: pink;
}
</style>
</head>
</html>
Try something along these lines:
Add
$css = file_get_contents('main.css');
and change $style to:
$style = $xml->createElement('style', $css);
and it should work.

How to display text in center of the page in mpdf

I want to display text in center of the page. But, it's not display proper. I used mpdf and i want to display using mpdf in php.
What I tried :
<?php
require_once __DIR__ . '/../bootstrap.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDisplayMode('fullwidth');
$stylesheet = '<style>' . file_get_contents('example1.css') . '</style>'; // external css
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML('<div id="center">
Hello World
</div>', 2);
$mpdf->Output('filename.pdf');
CSS :
#centrar{
margin:0;
width: 80%;
background-color: #000;
}
body { text-align: center }
Output :
enter image description here
It's display center in first line. I want to display in full page center part.
How to do that? Please help me. Thanks.

WKHTMLTOPDF (knplabs - snappy) not showing header nor footer page

I'm using wkhtmltopdf to generate my pdf files. I've left the generation alone for some time and for whatever reason it's not generating the header and footer anymore.
Things I've tried so far (will update this when more answers come in):
Having the doctype, html, head and body tags ion the header and footer
Chaning the paths to the header and footer to absolute paths (Even using complete absolute paths from the drive forward C:/xampp... does not work.) It might be worth pointing out that changing the filename to something that doesn't exist does not throw an error. So I don't know if it finds the files. Maybe someone can tell me a good way to test this?
This is my header file:
<!DOCTYPE html>
<html>
<head>
<title>PDF header</title>
<style>
html {
display: block;
}
body {
font-family: Calibri, "Segoe Ui Regular", sans-serif;
letter-spacing: 0px;
}
</style>
</head>
<body style="padding-top: 30px">
<img src="../../images/logo_extra.jpg" style="width: 100%;"/>
</body>
</html>
This is my main file:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
// header('Content-Disposition: attachment; filename="offerte.pdf"');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
// I know there is a 'cover' function in WKHTMLTOPDF
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
And as always, please:
Give me an explanation and maybe an example but not just a bunch of working code!
PS: If you see any other mistakes, please let me know.
wkhtmltopdf has an issue with header/cover/footer. I didn't dig into it very deep as adding margins did solve it for me:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
//just set margins
$pdf->setOption('margin-top', 20);
$pdf->setOption('margin-bottom', 15);
$pdf->setOption('margin-left', '0');
$pdf->setOption('margin-right', '0');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
Description
Second problem is weird - nonexisting filename should throw an error. Comment out header and try then with false filename, snappys AbstractGenerator should say something...

wkhtmltopdf passing variable in html file

I use wkhtmltopdf with php, and it works well.
Now, i want to add some variables from the php file to the html one, but i don't find a solution.
the PHP file:
<?php
require '/path/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;
$pdf = new Pdf(array(
'no-outline',
'margin-top' => 0,
'margin-right' => 0,
'margin-bottom' => 0,
'margin-left' => 0,
// Default page options
'disable-smart-shrinking',
));
if($_GET['file'] == 'public'){
$pdf = new Pdf('template_public.html');
}else{
$pdf = new Pdf('template_pro.html');
}
$pdf->send();
?>
The HTML file:
<html>
<head>
<title>Generated PDF file</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div>
{title}
</div>
</body>
</html>
How can i replace the {title} in the HTML file?
This should replace all of the {variables} in the html files with their values, given an array of parameters.
$params = ["title"=>"title_val", "content" => "something", "key" => "value"];
$pdf = new Pdf();
$html = file_get_contents($templateFile);
foreach($params as $key=>$value) {
$html = str_replace("{".$key."}", $value, $html);
}
//Renders the pdf directly from the html string, instead of loading the file directly
$pdf->addPage($html);
$pdf->send();
Refer this for setting page options.
https://github.com/mikehaertl/phpwkhtmltopdf#setting-options
You can set title,page number etc...
Use addPage option to apply page options to specific page.
My task is to pass real client ip value to pdf. When I read IP as $ENV{REMOTE_ADDR} I got server IP, not client IP.
I use this code to solve the problem:
wkhtmltopdf --cookie remote_addr $IP
It passes IP variable to pdf. Then, my html page can read cookie values and show them inside text.

Categories