How to pass data to pdf in just DOMPDF? - php

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

Related

How generate n pages whith dompdf PHP [duplicate]

<?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.

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

How to use loop in creating multiple pdf file in dompdf

<?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 want to store tweets that I find after a search, in a text file

Does anyone know how could I save the tweets after searching in a .txt file?
My index file is the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Twitter</title>
</head>
<body>
<?php
require('twitter.class.php');
$twitter = new twitter_class();
echo $twitter->getTweets('tomato', 15);
?>
</body>
</html>
I'm new to all this so I would appreciate any help.
Here is the code to save the tweets:
<?php
require('twitter.class.php');
$twitter = new twitter_class();
$tweets = $twitter->getTweets('tomato', 15);
$currentfile = file_get_contents('tweets.txt');
file_put_contents('tweets.txt', $currentfile.$tweets);
?>
This will append the tweets instead of erasing the data if you don't want to append tweets just do this:
<?php
require('twitter.class.php');
$twitter = new twitter_class();
$tweets = $twitter->getTweets('tomato', 15);
file_put_contents('tweets.txt', $tweets);
?>
fwrite() is your friend. In loop where you echo tweets instead of echoing it write them to textfile
Have you tried the function file_punt_contents?
You could do:
<?php
$file = 'tweets.txt';
$tweets = $twitter->getTweets('tomato', 15);
// Write the contents back to the file
file_put_contents($file, $tweets);
?>
More info.
You can create file and write into in php usine fwrite , here is a simple code:
$fp = fopen('data.txt', 'w');
fwrite($fp, $twitter->getTweets('tomato', 15););
fclose($fp);

domPDF background image is not rendering in the PDF

<?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

Categories