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 (?))
Related
I'm wondering how i can go about reducing the font size on the generated table of contents.
I've read and reread their documentation here: https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
The bit i'm confused on is:
The generated XML document can be viewed by dumping it to a file using the
--dump-outline switch. For example:
wkhtmltopdf --dump-outline toc.xml http://qt-project.org/doc/qt-4.8/qstring.html qstring.pdf
I'm having difficulty understanding how to translate to my own project, and what do they mean by this part of the command:
http://qt-project.org/doc/qt-4.8/qstring.html qstring.pdf
If you want to change the default table of content (HTML and / or CSS styles), you can use the xsl-style-sheet parameter to specify a custom XLS stylesheet.
In your custom XLS you can add any HTML tag you need.
You can find an example of the default XLS file here. For your information, le XLS is generated from the tocstylesheet.cc file (see here).
I want to dynamically create an svg that I can use in an <img/> tag. This in itself is easy; create an svg, set the header and echo the generated parts in their correct place.
The problem is, I want to be able to embed fonts in the svg.
I've tried using the #font-face rule in the css of the svg, but that didn't work (MDN says that it only works on Android and Safari).
Is there any cross-browser way to do this?
Solutions I've Considered:
Possible Solution #01:
The solution:
In my main file, create an svg file which uses the #font-face css rule, and then use exec() to use inkscape to convert that svg into another svg, which converts all letters into paths. I then could use echo file_get_contents($inkscape_file) with the correct headers to output it as a svg which can be used with an <img/> tag.
The problem with this:
This creates 2 additional files, so seems very inefficient. Furthermore, since each user will end up generating several images, the space it takes up would grow phenomenally.
Possible Solution #02:
The solution:
Make a template in illustrator, then save it as svg, and tick the embed all glyphs option. Then replace the text & the styles with the options from the PHP script. Use the correct header and output this.
The problem with this:
This severely limits the amount of fonts that can be used, as it is limited to only those which I create a template for. My desired behaviour was to add the option for users to upload their own fonts and use them. This solution does not allow for that.
Additional information that may be of some relevance:
My development server runs fedora, and the production server uses redhat.
The #font-face rule I am currently using is as follows:
#font-face {
font-family: Potato;
src: url("/fonts/potato.otf");
}
You can't load any external resources declared in the svg from the <img> tag.
The only solutions would be some crappy ways to append the glyphs or the fonts into the svg file itself.
Actually there is a not so crappy way to do it as you found in this answer by lèse-majesté.
The best way is then still IMO to not use an <img> tag to display the svg documents, but rather use an <iframe> or an <object> tag, with the #font-face declared inside the svg file, or even directly include an inline version into the document. These methods do allow the loading of external resources such as fonts.
Then you just have to save the fonts on your server or just an url to the font in the #font-face declaration.
I just started using dompdf v0.6.0beta3 and the dompdf Codeigniter helper with Codeigniter. I have copied the Arial font family tff files from Windows 7 font folder to dompdf's /lib/fonts folder.
Problem: When I select a text using CSS and apply the font-weight: bold property, on the HTML output it is indeed bold, but after converting to PDF via dompdf, the text is no longer in bold!
font-size: 24px does not work, all text in the pdf are the same sizes. And the only font being used in the pdf appears to be Times New Roman!
How can I make my text bold and change its size and font in the pdf?
PHP (Controller)
function pdf() {
$this->load->model('resume_model');
$results = $this->resume_model->get_resume_details($user_id);
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view('resume_pdf', $results, true);
pdf_create($html, 'filename');
}
Is your stylesheet external to your HTML content? If so you may just have a path problem. The plugin appears to use $dompdf->load_html() to load the document. DOMPDF has no knowledge of your website when used in this way and will work off the local file system. What this means for you is that file paths are relative to the currently-executing file. If the path is absolute (e.g. /css/main.css) then DOMPDF will look for this file off the root of the file system. Instead of looking for the file at /wwwroot/content/css/main.css it will look for /css/main.css.
The quickest fix, if this is your problem, would be to add a full URL, including domain, to your file references (e.g. http://example.com/css/main.css).
I am having trouble using a script I found at http://www.marcofolio.net/webdesign/use_a_custom_font_on_your_website.html
The problem is, when I load a page, the text shows up then each word is replaced by a generated image of it using the PHP GD lib.
It creates a flicker effect that I can't seem to get rid of. There are options is the js file:
var hideFlicker = true;
var hideFlickerCSS = "replacement-screen.css";
var hideFlickerTimeout = 0;
But when I change any of those settings, nothing happens.
Please help!
Thank you.
For what you seem to be trying to do, image replacement is an extremely outdated method. All the ninja-devs are using technology called #font-face for their fonts and font replacements.
It's simpler, doesn't require anything to happen on the server and text on the page can be modified dynamically.
You can use services like http://www.fontsquirrel.com/ or http://code.google.com/webfonts for ready made font packages.
If you have a custom font (that you have a license for) you can create an #font-face package for it, using fontsquirrel's #font-face generator: http://www.fontsquirrel.com/fontface/generator
And then you just define your fonts in the CSS. Simple, elegant and works in 99% of browsers (yes, even IE6)
Cheers!
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);