dompdf watermark, image, header and footer issues - php

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

Related

mPDF generating blank pdf when importing css

When using PHP 8.1 and mpdf 8.1.2, when generating a PDF, the body of the resulting pdf is empty. While when I just output the HTML used in writehtml, I do get the full content including the css interpreted correctly.
However, when I comment out the <style>{% include '#App/inlineStyles/pdf.css' %}</style> part, it does render the PDF correctly, however (obviously) without the required styling.
I also tried seperately including both using 2 different writeHTML calls (with different modes for css). This resulted in the css not being applied (but the content being written).
The pdf.css file doesn't contain anything weird/invalid. It's partially based on tailwind and generated from .scss files.
I tried new Mpdf(['debug'=>true]) and also tried to catch any mpdfexceptions, but there were none.
Can somebody help me out?
EDIT:
The reason for this happening is within CssManager.php that the entire css gets removed:
// Remove CSS (tags and content), if any
$regexp = '/<style.*?>(.*?)<\/style>/si'; // it can be <style> or <style type="txt/css">
$html = preg_replace($regexp, '', $html);
EDIT2:
How am I supposed to include a file? I think I know the problem; I require the tailwind css component in my .css file which I'm including. The length of my .css file is 38601 lines (1000kb). The problem is in Mpdf/CssManager.php, line 481 (the code above). The return of the preg_replace is null because the file is too long.
In the past (with PHP 7.3 & an older version of mpdf) I did manage to include the tailwind css also into the mpdf writehtml. But in this version it does not work any longer.
My main question is, what is the best way to include the tailwind component? As I'm guessing that the length is what is causing $html to be null and therefore resulting in a blank page
(I'm not sure whether it would still be required to include a file (?))

Add image on to dompdf

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

CKEditor outputting text doesn't match input when using htmlspecialchars_decode(stripslashes($variable))

I'm trying to use CKEditor to input rich text into my database, which works except the formatting is not the same when I try to output the rich text the user entered. The position of elements such as pictures is incorrect.
I currently have the following code:
Input:
$pitch = htmlspecialchars($_POST['editor1']);
$sql = mysql_query("UPDATE projects SET pitch='$pitch' WHERE id='$proj_id'");
Output:
$pitch = htmlspecialchars_decode(stripslashes($pitch));
The result is then echoed back which creates the incorrect formatting.
Does anyone know what I'm doing wrong?
This has nothing to do with PHP.
Contents of the framed editor (which you use) is styled by the contents.css file which you can find in the main CKEditor directory. However, this stylesheet is not used on your page, so content created in editor is not styled by the same rules.
The correct approach is - style content on your site and then copy (or somehow reuse, e.g. by setting config.bodyClass and config.contentsCss) these styles in editor contents.
Remember, that you also need to set styles available in styles drop down. They are by default configured as for the sample, but when you'll change available styles, you need to update styles.js file too.

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

why can't I overlay text on the bottom part of the page using fpdfi?

Background:
I'm developing an application that involves taking an existing pdf form and overlaying text on top of it. The pdf is version 1.3. I'm using the fpdfi class (written in php) that can be found here:
http://www.setasign.de/support/manuals/fpdi/
I am using fpdfi as an extension of the tcpdf class found here:
http://www.tcpdf.org/index.php
I use a line of (php) code that looks like this:
$this->SetXY(25, 250);$this->Cell(0, 8.6, $data['my_data_to_overlay']);
where $this refers to the instance of the fpdfi class, the SetXY function tells it the coordinates I wish to place the text (x,y), and the Cell function tells it what text I want displayed and how large and such.
The page height is 279.4 (all units here will be in mm)
The problem:
If I set my y coordinate higher than 250 (even at 251), the text will be placed on the next page instead of near the bottom of the current page as expected. All other coordinates above that line at 250 will display properly, even at the top edge of the page.
Why can I not overlay the text near the bottom of the page? What am I doing wrong?
Also of note: all margins for the page are set to 0 and headers and footers are disabled.
Disable auto page break by calling FPDF::SetAutoPageBreak with false, or true but with 0 margin.
EDIT: Using FPDF::SetMargins you can only set left, top and right margins, but not the bottom one. This is the proper way to specify a bottom margin.

Categories