Add image on to dompdf - php

<script type="text/php">
if(isset($pdf)){
$w = $pdf->get_width();
$h = $pdf->get_height();
$footer = $pdf->open_object();
global $id
$pdf->image('s/'.$id.'-here.jpg', "jpg", 0, $h - 279, 611, 279);
$pdf->close_object();
$pdf->add_object($footer, "all");
}
</script>
I am trying to add a footer similar image on the end of the page and this is the only code I actually managed to have the picture on the bottom, however I got two problems now:
If the tables text (content) of the PHP html goes to long it's hidden by the image (in this case) i want to break the page and then add the image in the bottom on the new page.
How can I make it's only added on the last page if there are more than one by simply the dynamic output I am doing on a table.
Anyway I can overcome the problems above with simply this code? I've been trying using header/bottom CSSes like https://code.google.com/p/cleanstickyfooter/ as well but the method above is working expect the sligh issues just described.
THE HTML is simple tables with a width of 100%...
QUICK update (though)
I think I can solve the first issue by adding a empty div with a set height of the image, now I would only want to add this onto the last page... How?

Inline script is rendered starting with the page dompdf is currently laying out. If you want your image to apply only to the end of the document you can just relocate the script to the end of the HTML content.
Also, if you only need to apply the image to the last page you don't need to use detached objects ($pdf->open_object()/$pdf->close_object()/$pdf->add_object()). The purpose of detached objects is to provide a means of reusing content. So your inline script can be as simple as:
<script type="text/php">
if(isset($pdf)){
$w = $pdf->get_width();
$h = $pdf->get_height();
global $id
$pdf->image('s/'.$id.'-here.jpg', "jpg", 0, $h - 279, 611, 279);
}
</script>
Lastly, if you're using dompdf 0.6.0 (currently in beta) you can skip the inline script and just position the image. Positioned content is also rendered starting with the page dompdf is currently laying out. So you can also get the desired image layout adding the following to the bottom of your HTML content:
<div style="position: absolute; bottom: 10px;">
<img src="s/$id-here.jpg">
<div>
You must, of course, generate the required HTML for the image. I'm assuming you're already doing that since you're accessing the $id global variable in the inline script.

Related

DomPDF - Changing the point/pixel ratio

I'm trying to use DomPDF to generate a PDF out of some HTML. I need the page to be 20mm x 20mm, so I use the following:
CPDF_Adapter::$PAPER_SIZES['my_square_page'] = array(0, 0, 566.929134, 566.929134);
$dompdf->set_paper('my_square_page','portrait');
It works properly, if I check the PDF properties the size is ok. The HTML that will appear in the PDF has a container div of 490x490px. This size cannot be changed, as the elements inside that div are absolutely positioned.
The problem, then, is that in the generated PDF the div does not cover the entire page. I've tried setting the DPI, using different values in
def("DOMPDF_DPI", 150);
But it does not seem to make any difference at all. The output I get is this (gray borders are from the PDF reader):
I've tried setting the width and height of body and html in the CSS of the content, but it does not work.
You can check the source code of my sample case here.
Ok, I figured it out. Looks like the line
def("DOMPDF_DPI", 150);
does not actually do anything. I did change the dompdf_config.custom.inc file and then it worked. Added this:
define("DOMPDF_DPI", 62.230);
But now the images look too big :S

include php file only at certain screen resolutions

I'm having issues with adsense on responsive design. One solution I found is to not load them at all if window size is not big enough. So I thought I would create a separate php file with advertisement code, container etc... and than include it on a page. However, I can't figure out how to only include this file if, lets say, window width is 720px or above, else don't include this file.
Perhaps, javascript can be used some way, not sure how it will work with all the dom and php includes though.
You can try something like:
<script language=javascript>
if (screen.width >= 720 )
$('#place_holder_div').load('file_from_server.php');
</script>
Here #place_holder_div is a div in your html file. The syntax is Jquery but of course you can use plain javascript if you wish. The code looks at the screen width and if greater than 720 pixels, loads the php file file_from_server.php (which will contain your ad) into the placeholder div.
The only way to know what the window or screen size of a client is, is by using JavaScript.
window.innerHeight; // Available height of the browser for the document
window.innerWidth; // Available width of the browser for the document
window.outerHeight; // Browser height
window.outerWidth; // Browser width
window.screen.height; // Screen height
window.screen.width; // Screen width
After inspecting these, you could do a HTTP request for the relevant file. It is, however, probably not the best solution since the user can actually change any size mentioned above at any given time.

Convert a webpage to an image (thumbnail) [duplicate]

hi all my chart is exporting fine with highcharts i m getting the chart for my php project
but the problem is
that i am looking to import html content or the whole page along with the chart not just the chart
is it possible to do ??
can any one help me
or show here is a sample fiddle http://jsfiddle.net/YBXdq/
i need to export the text below the chart has well
There are so many direct and indirect way to achieve this
Use HTML Canvas : http://html2canvas.hertzen.com/
Using wkhtmltoimage
Example
exec('wkhtmltoimage --quality 50 http://www.bbc.com bbc.jpg');
Using wkhtmltopdf + ImageMagic
-- Convert the web page to pdf using wkhtmltopdf
-- Convert pdf to jpg using ImageMagic
Example
exec("convert a.pdf a.jpg");
Use PHP imagegrabwindow and imagegrabscreen
Example
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://localhost");
/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);
Advance Examples
-- Also See Get Website Screenshots Using PHP
For Possible Issues : Getting imagegrabscreen to work
use Webshort and call it from php using exec if you have python installed
Example
exec("python webshot.py https://example.com/webshot/php example.png");
Download and use Website Thumbnail Generator
Example
webthumb.php?url=http://www.google.com&x=150&y=150
Use boxcutter
Example
exec('boxcutter -f image.png');
Capture Screenshots in PHP with GrabzIt
Example
$grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
$id = $grabzIt->TakePicture("http://www.google.com", "http://www.example.com/GrabzItHandler.php");
using wimg.ca
Example with this current page
http://wimg.ca/https://stackoverflow.com/questions/10328457/how-to-export-the-whole-page-or-html-content-with-highcharts-not-just-the-chart/10330701#10330701
TimThumb – PHP Image Resizer
Example
timthumb.php?src=http://www.google.com/&webshot=1
I think have given more than enough example
You can try to print the page , or make a pdf using php file functions to get the desired html content .
or you can try the method told by baba to get the image :)
i found a simple workaround for exporting charts (printing)
and
i didn't use any plugin just little plain old css and some javascript
which in my case is
i wanted to print the chart with some specific html content of the page
or in my case i wanted to remove header , footer and left menu
i dint wanted to show buttons or unecessary content
and just show the page content , description table and chart
so here is i how i achieved it.
> CSS :-
<style type="text/css">
#media print{
#page
{
size: auto; /* auto is the initial value */
margin: 0mm; /* this affects the margin in the printer settings */
}
body{ background-color:#FFFFFF; background-image:none; color:#000000 }
.navimainbg{ display:none;}
.printhide{ display:none;}
.usercontent-footer{ display:none;}
.linkBTN{ display:none;}
.userheader{ display:none;}
.user-profile-left{ display:none;}
#userbgcontent{ width:100%;}
}
</style>
We are focussing on the print css here which we implied when the page is printed
acess the divs or portions of the page which you dont want ot be printed via their class or id depending upon your usage
for example
i didnt wanted to display the button
.linkBTN{ display:none;}
which can be simply called via javascript.
Javascript :->
<script type="text/javascript">
function printpage()
{
window.print()
}
<script type="text/javascript">
we can invoke the print function to print the page minus elements we dont want to print with the page via click of a button by calling function in my case "printpage" as you can
see this button will also not display while printing as printhide class display is set to none while priting
<input title="Print a Printer Friendly Page" class ="printhide" type="button" value="Print this page" onclick="printpage()">
isnt is it a simple way to print chart other than highcharts printing if you want to print more
only con is that sometime the image will slide down when you want to show it along with some conetent due to lack of size to render an image . it will shift to next page . other than its tested working .

dompdf watermark, image, header and footer issues

i am using dompdf as a plugin in codeigniter. it generates the pdf perfectly. but some problem are eating up me for some days.
I want to put an image as header in every page of the generated pdf. i did it according to the tutorial given here.
but no progress. i tried for images of all types(jpg,png,gif) in the same folder wheresript for pdf resides. it did not work. then i tried for setting the path variable for image and css as shown at http://code.google.com/p/dompdf/wiki/Usage .but i could not get a good example or tutorial for that. if any body has ever used this please help me.
i also want to add watermark on every page. for that i am using
$pdf->page_text(110, $h - 240, "TRIAL", Font_Metrics::get_font("verdana", "bold"),110, array(0.92, 0.92, 0.92), 0, -58);.
This works fine and generates watermark.but watermark comes over content.is there any thing to change the opacity of watermark. it is not changed by changing parameters in array.
3.at the end of every page ( except the last one) i want to put the text "continued..". for that i am using
$text = "Continued..";
$width = Font_Metrics::get_text_width($text, $font, $size);
$pdf->page_text($w - 16 - $width - 38, $y-15, $text, $font, $size, $color);
it puts the text in every page(as it should). is there any way to put text in all pages except the last one?
how to set font which are not available in the lib file of dompdf?
1) What, exactly, is the code you're using? It sounds like you're having success with inline script, just not images. So there could be a problem with your code.
2) This is a problem with inline script. It is rendered after the HTML content meaning it displays on top of the HTML content. There used to be an opacity option when adding text via inline scripting, but I'm not sure it was ever working correctly. It has been removed from the 0.6.0 code base.
You may have to wait for the next release, which will include more styling options that would enable what you want to do in HTML (specifically, fixed positioning and CSS translate).
3) I'm not sure you can display a header on every page but the last. There is an option to stop an object from displaying, but it appears to only affect subsequent pages. So you'd have to add the relevant code to the page prior to the last page.
4) If you want to add a font you need to be able to parse it using load_font.php. You will also need access to the executable ttf2afm (dompdf 0.5.1) or ttf2ufm (dompdf 0.6.0). There are instructions on how to load fonts, or you can also try a web-based font prep tool I developed.
Make sure that DOMPDF_ENABLE_PHP is set to true in dompdf_config.inc.php ~line 173 in 0.6.2
Be aware that this is a potential security risk if there is any unfiltered content coming from an untrusted source to the pdf.
/**
* Enable inline PHP
*
* If this setting is set to true then DOMPDF will automatically evaluate
* inline PHP contained within <script type="text/php"> ... </script> tags.
*
* Enabling this for documents you do not trust (e.g. arbitrary remote html
* pages) is a security risk. Set this option to false if you wish to process
* untrusted documents.
*
* #var bool
*/
define("DOMPDF_ENABLE_PHP", true);

Print stylish text on image in php

Is it possible to print html div tag with style on image in PHP?. if not, then what is the alternative way?
Some hosts have ImageMagick for PHP. To add text to your image, take a look at the syntax of the commands here. The example given on that page should help some - it's pretty easy to get text on an image.
The benefits of using ImageMagick over a fixed image is that you can vary the content of the text, which is what you might want (you didn't mention needing a static text; for this, I'd use an image with a transparent background). For more comprehensive font commands, take a look here.
To put a transparent image on top of your base image, take a look at this very nicely designed site.
I'll also give the code presented on that site here:
$photo = imagecreatefromjpeg("original.jpg");
$watermark = imagecreatefrompng("watermark.png");
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($photo, true);
// Copy the watermark onto the master, $offset px from the bottom right corner.
$offset = 10;
imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
// Output to the browser
header("Content-Type: image/jpeg");
imagejpeg($photo);
To output the image to a file, please Google that and replace the last two lines of the example given above.
For ImageMagick stuff, take a look here
I hope this helps :-)
James
You can set the image as the background graphic of any div using CSS. Then the text within that div will appear on top of the image.
(CSS)
.mydiv {
background:url(/path/to/image.gif);
width:100px; /* set to width of the image */
height:100px; /* set to height of the image */
}
(HTML)
<div class='mydiv'>Some text here</div>
There is no easy way to print text on images using html/css on server side, because php can't parse html, so you'd better find another solution like php GD.

Categories