<?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
<?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 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();
<?php
include ('mpdf/mpdf.php');
$mpdf = new mpdf;
ob_start();
?>
<html>
<head></head>
<body>
My various content with table, dropdown menu and 2 include files.
</body>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();
$mpdf->Output();
exit;
?>
Wondering why this function doesn't work and will only output empty pdf file. I have tried many ways but it just doesn't work. I have placed this function at the beginning of my code but it always outputs an empty file.
Try this code:-
<?php
$html = ob_get_contents();
ob_end_clean();
// You need to write html
$mpdf->WriteHTML($html);
// define path of your output file and set mode 'F' for saving
$mpdf->Output('filename.pdf','F');
exit;
?>
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();