As per documentation, we print the page number on each pdf page but my problem is I want to use the PHP variables inside dom script tag which I can't do that.
<script type="text/php">
if (isset($pdf)) {
$font = $fontMetrics->getFont("Arial", "bold");
$pdf->page_text(555, 745, "{PAGE_NUM}", $font, 7, array(0, 0, 0));
}
</script>
#php
$page = "{PAGE_NUM}";// it prints the string "{PAGE_NUM}" but I want current page number here
#endphp
My problem is that I want to use this {PAGE_NUM} inside my normal #php laravel blade file tag so that I can be able to detect the page change. Is there any way to do this stuff?.
Related
I'm using dompdf 0.7.0 and try to write down text in PHP on my pdf after rendering. I need text on specific pages and found the following from Answer from Brian
DomPDF {PAGE_NUM} not on first page
The function page_script sound like the correct answer.
I could check if loop is currently on page 3 or whatever.
Does I have to enable any options for this function?
Example:
$dompdf = new Dompdf( $options );
$dompdf->set_option('default_font', 'open sans');
$dompdf->set_option('fontHeightRatio', 1);
$dompdf->setPaper('A4')
$dompdf->set_option('enable_html5_parser', true);
$dompdf->set_option('enable_php', true);
$dompdf->loadHtml( $html );
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
if ($PAGE_NUM > 1) {
$current_page = $PAGE_NUM-1;
$total_pages = $PAGE_COUNT-1;
$canvas->text(0, 0, "$current_page / $total_pages", "open sans condensed", 10, array(0,0,0));
}
');
It still be shown on my first page.
$canvas wouldn't be available from the page script as it is out of scope. The canvas object can be referenced inside your page script as $pdf.
Try the following page_script call instead:
$canvas->page_script('
if ($PAGE_NUM > 1) {
$current_page = $PAGE_NUM-1;
$total_pages = $PAGE_COUNT-1;
$font = $fontMetrics->getFont("open sans condensed", "normal"); // or bold, italic, or bold_italic
$pdf->text(0, 0, "$current_page / $total_pages", $font, 10, array(0,0,0));
}
};
There are a few variables available from within page scripts or embedded script:
$PAGE_NUM: current page number
$PAGE_COUNT: total number of pages
$pdf: the canvas object
$fontMetrics: an instance of the FontMetrics class
If you're using page text you have access to the following template variables:
{PAGE_NUM}: current page number
{PAGE_COUNT}: total number of pages
Note: Support for parsing embedded script or page scripts is disabled by default. Enable it with the following command: $dompdf->set_option("isPhpEnabled", true);. This must be done prior to calling $dompdf->render();.
I am trying to draw line/image on every page of pdf using dompdf but it starts from second page, why this is so ? anyone has any idea ?
Here is my code
$dompdf = new DOMPDF();
$dompdf->load_html($message2);
$dompdf->set_paper('a4','portrait');
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For Header
$header = $canvas->open_object();
$canvas->image($header_image1,'jpg',0, 0, 595, 100);
$canvas->line(0,100,595,100,array(0,0,0),1);
$canvas->close_object();
$canvas->add_object($header, "all");
//For Footer
$footer = $canvas->open_object();
$canvas->line(0,740,650,740,array(0,0,0),1);
$canvas->image($footer_image1,'jpg',0, 742, 595, 100);
$canvas->close_object();
$canvas->add_object($footer, "all");
$output = $dompdf->output();
this code draw line/image on pdf but it only display on last page.
I have two pages in pdf and my line/images are drawn on second page not on first page.
Please suggest any solution.
Adding objects in DomPDF works from the current page onwards. In other words, your objects will get added, but only from the page you currently have and then onwards to any new pages you add.
In your code, you've already converted your HTML to PDF, so the current page is more than likely the last page in your document. So your header / footer are added there, but not to any previous pages.
To place content on every page, domPDF provides two methods: page_text and page_script.
In your case the following type of code should do the trick:
$canvas->page_script('
$pdf->line(10,730,800,730,array(0,0,0),1);
');
Code in the page_script function is then executed for every PDF page.
The line is not displayed for me because embedded php was disabled.
This solved the problem:
$dompdf->set_option("isPhpEnabled", true);
This line has to be placed before $dompdf->render().
how to add image in all pdf page using header and footer.below is my code.
$pdf = App::make('dompdf');
$pdf->loadFile('invoice.html');
$pdf->output();
$dom_pdf = $pdf->getDomPDF();
$canvas = $dom_pdf ->get_canvas();
$image1="logo.png";
$canvas->image($image1,'png', 0, 0, 50, 25);
$canvas->page_text(10, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, array(0, 0, 0));
$pdf->save('pdf_report/eft_payment-'.$RandomAccountNumber.'.pdf');
I want to print HTML to PDF using TCPDF so I do this:
$html = '<h1>Hello</h1>
How are you?
<h2>Answer</h2>
I am well, thanks';
// ... etc. Long HTML document.
$pdf->writeHTML($html);
The question is if there is a way how to add a table of contents. If TCPDF can recognize page numbers of headings.
I also sometimes use HTML page breaks:
<div style="page-break-after: always"><span style="display:none"> </span></div>
I understand that I need bookmarks. But in this case TCPDF would have to create them automatically. If I add an ID to html-heading-tag, TCPDF probably does not find it and cannot use it.
<h1 id="abc123">header1</h1>
// but following does not do anything:
$pdf->addTOC()
Since the ToC page is created as the very last it will show that as the last page.
So instead use
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
//It will show page numbers
First of all apologies, if the question title misleads you. Here is what I want to achieve.
I want webmasters to come to my site, copy a piece of code(basically it displays an image on the webmasters's website) and then paste it on their website for promotion of my website. I am good till this and have succeeded in doing so. Now, I want the image to have a dynamic rank that will be fetched from my website. So, when webmasters paste the code on their website, the rank displayed on the image(as a text) changes based on my Database setting.
Can anyone let me know how this can be achieved..
You can do an iframe as aniruddha suggested or you can also use javascript. See Twitter, Facebook, and Google Calendar on how their various widgets work.
I've provided some very simplified implementations for you to see how they work. But it's probably better to look at the examples mentioned above. Ignore the security issues in my examples here. Just wanted to show how it works on the simplest level.
iFrame Example
Client Code For Iframe Example:
<html>
<head>
</head>
<body>
<h1>User Website Title</h1>
<p>Some random user text</p>
<p>Iframe version here</p>
<!--This is the code that the client would paste in to their website -->
<iframe src="http:/your.domain.com/server_iframe_test.php?user=TestUser" height="30" width="500" scrolling="no" frameBorder="0"></iframe>
<!-- End client iframe code to paste-->
</body>
</html>
On your server, you can have page to use as the source for the iFrame. Here I'm calling mine server_iframe_test.php:
<?php
//Generating a random number to show that page will update on reload
$randomNumber = rand(1, 999);
//In the src of the iframe we will send a get parameter called user to determine which user we should look up
$data = array('user' => $_GET['user'], 'rank' => $randomNumber, 'image' => 'http://url/to/some/image.png');
?>
<!-- This is the output that will be generated in the iframe -->
<div class="widget_wrapper"><?php echo $data['user'] ?>: <?php echo $data['rank'] ?> and <? echo $data['image'] ?></div>
Javascript Example
For the javascript example it is possible to make ajax calls across domains using JSONP. You can read up on how it works here: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/
Here is the client side for the javascript version:
<html>
<head>
<!-- I'm cheating here by including jquery in the head. My test script needs it-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<h1>User Website Title</h1>
<!--Code that user pastes into their page-->
<!--First get the script from your domain-->
<script src="http://your.domain.com/test_widget.js"></script>
<!--This code is also pasted by the client. We use this to set user name and then render the html. Also I added an id to the script tag here to know where to append the html. This is a more of a shortcut.-->
<script id="test_widget_script">
var widget = new testWidget('TestUser');
widget.render();
</script>
<!-- Widget will render html here -->
<!--End Code to Paste-->
<p>Some random user text</p>
</body>
</html>
Here is the server side js script called test_widget.js:
var testWidget = function(user){
var _user = user;
this.render = function() {
//We make a JSONP call here to our php script which generates the data for the user. Note the &callback?. We need to add this to make it a JSONP call.
$.getJSON('http://your.domain.com/test_js.php?user=' + _user + '&callback=?', function(data){
//Append html result after the js tag with the following id
$("#test_widget_script").after('<div class="widget_wrapper">' + _user + ': ' + data.rank + ' and ' + data.image + '</div>');
});
}
}
Here is the php file called test_js.php that will handle the request:
<?php
$randomNumber = rand(1, 999);
$data = array('user' => $_GET['user'], 'rank' => $randomNumber, 'image' => 'http://url/to/some/image.png');
//We need to make the content type javascript
header("Content-type: text/javascript");
?>
<?php //Here we take the name of the callback passed as a GET parameter and use it as a function to pass in our json data. We don't call the function in php, but when the text gets returned, jQuery knows how to handle the response. ?>
<?php echo $_GET['callback'] ?>(<?php echo json_encode($data); ?>);
I think a iframe is required with src of your server side script page which will fetch the rendered code with rank over the image from your server. The purpose of the iframe will be to render the whole html.
Modify the code from here http://php.net/manual/en/function.imagettftext.php
to make an image with your rank in it. Here is tested example based on that code, note you need GD and truetype for this. All you need do is send then a link and use a get variable to set which user so you can get the right ranking back from your DB. I won't code that part.
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw (your ranking)
$text = '21'; // <== change this as you need over time
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Hey I have dynamic image begin created with imagecreate(), and I have random values being produced, and I want to pass that random number, to a variable, and use that variable inside the page where Im using the image source.
The image is on random.php, and Im using <img src="random.php" /> on page index.php if that matters, and I want to pass from random.php (the image), to index.php. I already tried sessions and cookies but when I refresh, its always 1 step behind what the image is producing...
Im using a for loop to echo random numbers, I need to pass those numbers to a variable. Basicly how do I get numbers outside an image, in real time, not 1 step back.
What about using a temporary session variable?
On your first page,
<?php
session_start();
$_SESSION['mykey'] = 'myrandomval';
session_write_close(); // Helpful if you're using a header redirect
?>
and on the second page
<?php
session_start();
$value = $_SESSION['mykey'];
?>
The problem you are having is that index.php is requested, then the browser realizes that random.php is needed, so- yes, random.php will be a step AFTER index.php...
If you switch your "random creation" logic around, (have index create the key stored in a session, which can then be read from random.php) it may solve your problem.
Instead of passing variables from the page to the image, you could set the random number generator seed to the same value on both scripts. Here is a full sample:
<?php
function randomcolor()
{
srand( $_SERVER['REQUEST_TIME'] );
$colors = array('red', 'green', 'blue', 'black', 'orange');
return $colors[ array_rand($colors) ];
}
$color = randomcolor();
if(isset( $_GET['image'] ))
{
header('Content-type: image/png');
$im = imagecreate(75, 50);
imagecolorallocate($im, 0xee, 0xee, 0xee);
$black = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 2, 10, 10, $color, $black);
imagepng($im);
exit;
}
?>
<p>color: <?php echo $color; ?></p>
<img src="test5.php?image=1" />
This is two separate requests being seeded with the REQUEST_TIME, which means array_rand will return the same value both times.