DomPDF - Changing the point/pixel ratio - php

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

Related

DOMPDF displaying pictures, but wrong

I'm using DOMPDF to generate several documents, and in the top left is an image that looks like this:
But whenever I generate the PDF there is a very small chance (let's say about 10%) that the image will look like this:
I have no idea why this happens. It doesn't happen to any other images in the document.
When I move the image a few pixles up or down, the white line moves as well, so it's not like there is a hidden element or something placed over the image.
Is this a known DOMPDF problem ?
It appears that this is caused by imagick. It can be disabled as follows:
Add the following line to "dompdf_config.custom.inc.php":
define("DOMPDF_ENABLE_IMAGICK", false);
And change line 4324 of "class.pdf.php" from:
elseif (extension_loaded("imagick")) {
to:
elseif (extension_loaded("imagick") && DOMPDF_ENABLE_IMAGICK) {
This solution worked for me
I found it here: https://github.com/dompdf/dompdf/issues/432
Thanks to BrianS

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.

Border arround image in report created in PHP with FPDF

i have added an image in a pdf generated in PHP with FPDF, the problem occurs when i insert an image the image gets inserted at the right location but with an ugly border around it whereas in the actual image there is no border. Another important thing that need to be mentioned here is that i have re-sized it with the FPDF Image() method parameters my code is:
$this->Image('report_footer.jpg',10,280,190,8);
The border appears only on the top and left side of the image as shown below:
Any idea to get rid of the border arround my image?
You can try providing the image type alongwith other parameters. You can also try editing the image to the required dimensions and then give it a try. If that doesn't work you can try using TCPDF with html write and image with border 0 attribute.

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