<?php
require_once 'classes/postgredb.class.php';
require_once 'include/functions.php';
require_once("/tools/dompdf/dompdf_config.inc.php");
$con=new PostgreDB();
ob_start();
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'<div style="page-break-after: always;"></div>'.
'</body></html>';
for($i=0;$i<5;$i++)
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
}
?>
I want to print the "Hello World" in each page,but my code is printing it in only one page.
How do i print "Hello World" in each page using a loop.Please help me.
Try this:
Your loop is wrong. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
for($i=0;$i<5;$i++)
{
$html .= '<div style="page-break-after: always;"><p>Hello World!</p></div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
$dompdf->clear();
NOTE: You need to make sure, is that heredoc closer HTML is in a new line and not indented.
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
I convert html to pdf.
Is it possible to make a part of the PDF start only on an odd or even page, for example, using the page-break-after parameter?
Thanks!
This is my solution that works for me.
This allows new pages to be output from odd.
If you need output new pages from even, change '$PAGE_NUM%2 != 0' to '$PAGE_NUM%2 == 0'
$html ='<body>';
$html .='here is my html';
$html.='<script type="text/php">
if ( isset($pdf) ) {
if($PAGE_NUM%2 != 0) //add empty page if odd one
{
$pdf->new_page();
}
}
</script>';
$html.='<div style="page-break-before: always;"></div>'; //start new page
$html .='this is my html which will start at odd page';
$html .='</body>';
$options = new Options();
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->set_option("isPhpEnabled", true);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
I'm trying to write an Joomla plugin to add width and height tag to each <img> in HTML file.
Some image file names are Persian, and getimagesize faces error.
The code is this:
#$dom->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<img src="images\banners\س.jpg" style="max-width: 90%;" >
</body>
</html>
');
$x = new DOMXPath($dom);
foreach($x->query("//img") as $node)
{
$imgtag = $node->getAttribute("src");
$imgtag = pathinfo($imgtag);
$imgtag = $imgtag['dirname'].'\\'.$imgtag['basename'];
$imgtag = getimagesize($imgtag);
$node->setAttribute("width",$imgtag[0]);
$node->setAttribute("height",$imgtag[1]);
}
$newHtml = urldecode($dom->saveHtml($dom->documentElement));
And when Persian characters exist in file name, getimagesize shows:
Warning: getimagesize(images\banners\س.jpg): failed to open stream: No such file or directory in C:\wamp64\www\plugin.php
How can I solve this?
Thanks to all,
I couldn't reach to results on WAMP server (local server on Windows),
but when I migrated to Linux server, finally this code worked properly.
$html = $app->getBody();
setlocale(LC_ALL, '');
$dom = new DOMDocument();
#$dom->loadHTML($html);
$x = new DOMXPath($dom);
foreach($x->query("//img") as $node)
{
$imgtag = $node->getAttribute("src");
if(strpos($imgtag,"data:image")===false)
{
$imgtag = getimagesize($imgtag);
$node->setAttribute("width",$imgtag[0]);
$node->setAttribute("height",$imgtag[1]);
}
}
$bodytag = $x->query("//body");
$node = $dom->createElement("script", ' /* java script which may be necessary on client */ ');
$bodytag[0]->appendChild($node);
$html = '<!DOCTYPE html>'."\n" . $dom->saveHtml($dom->documentElement);
Some hints:
the code, shouldn't touch base64 image sources, so I added an condition to the code.
if some script (or whatever, div, p, ....) should be added to body tag, you can use appendChild method.
<!DOCTYPE html> should be added to final DOM object output :)
<?php
require_once 'classes/postgredb.class.php';
require_once 'include/functions.php';
require_once("/tools/dompdf/dompdf_config.inc.php");
$con=new PostgreDB();
ob_start();
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'<div style="page-break-after: always;"></div>'.
'</body></html>';
for($i=0;$i<5;$i++)
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
}
?>
I want to print the "Hello World" in each page,but my code is printing it in only one page.
How do i print "Hello World" in each page using a loop.Please help me.
Try this:
Your loop is wrong. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
for($i=0;$i<5;$i++)
{
$html .= '<div style="page-break-after: always;"><p>Hello World!</p></div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
$dompdf->clear();
NOTE: You need to make sure, is that heredoc closer HTML is in a new line and not indented.
I'm using mdpf and php to generate a PDF. I need to create a different footer every time I use the tag pagebreak.
My code is something like this (and it's not working this way)
$mpdf = new mPDF('c', 'A4');
$mpdf->SetHTMLFooter('First Article','O');
$html = 'Lots of text';
.
.
.
$html .= "pagebreak"; (this is a html tag)
$html .= 'More lots of text';
$mpdf->SetHTMLFooter('Second Article','O');
$mpdf->WriteHTML($html);
print $mpdf->Output();
How could I do that?
You need to flush the content with WriteHTML, then set a new footer.
$mpdf = new mPDF('c', 'A4');
$mpdf->SetHTMLFooter('First Article','O');
$html = 'Lots of text';
.
.
.
$html .= "pagebreak"; (this is a html tag)
$mpdf->WriteHTML($html); //flush
$html .= 'More lots of text';
$mpdf->SetHTMLFooter('Second Article','O');
$mpdf->WriteHTML($html);
print $mpdf->Output();