New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not applying true css / styles : line-height and fonts issue #3369
Comments
The main thing you need for resources accessed over HTTP is to set the isRemoteEnabled option to true, which you have. You would also need to make sure that allow_url_fopen is set to true, and that Dompdf has read/write access to the temp directory (which you can specify using the tempDir option) if you have images or fonts to load. The chroot option is not relevant if the files are accessed via HTTP. I can't really help with the file loading issues because I don't have enough info. You mention having to add HTTP to the resource URLs. That would be required if the resources need to be accessed via HTTP unless you're loading the document itself over HTTP (e.g. As for missing text, the core PDF fonts do not support any characters other than those supported by Windows ANSI. The bundled DejaVu fonts support some additional characters, but not Chinese or Arabic. Read through the font information and Unicode how-to for more information. You might try some of the configuration options outlined in the troubleshooting document. |
Thanks for your response. Re EDIT, I switched to saving pdf on php backend to exclude anything the front-end is causing, and no change to my situation. I checked allow_url_fopen in php.ini, and it's open Dompdf has read/write access to the tmp folder, because it's creating the pdfs no problems. It's just not handling style sheets, fonts. Re non-latin languages, I've used the Hello World example you provide in your non-latin font docs https://github.com/dompdf/dompdf/wiki/UnicodeHowTo. I selected a chinese character font, which has been downloaded locally but called using the localhost server location. It produces a pdf with boxes (ie. the font coding isn't working.)
|
Good news everyone, hello world is working but only after a) changing to a custom arabic font (google noto font for arabic), and b) using The following updated code produced a pdf with the arabic script. I wish there was a universal font to make this easier. Are you aware of any? Otherwise I need to detect / tag each line of text with a language and use a font-family for per tag. Very messy.. doable but not exactly graceful. Is there a universal font?
I'm still stuck on the issue re Style sheets which are supposed to, but aren't being read by dompdf when embedded into the encoded html. |
The fact that the loading the font via Google instead of your local server probably means that your URL is not quite right. If you write a simple fetch script...
...are you able to retrieve the font? If not then it's probably the same for your stylesheets. As for the boxes, that means the font supports Unicode, just not the characters on the page. There are very few "universal" fonts. MS Arial Unicode is one but it's not generally available and has license restrictions. Unless you can find one you'll need to style each block of text with the appropriate font family. That is, until the next version of Dompdf (3.0.0). It adds support for detecting which of the listed fonts support the characters being rendered. With that release you'll be able to just style your document something like:
However, regardless of which version of Dompdf you're using languages that require complex text layout will not render correctly without pre-processing. For example, shaping required by Arabic has to be performed before passing the HTML document to Dompdf. |
Hi @bsweeney , yes the font and style sheet are definitely read - otherwise it wouldn't work for the rest of the site, hwoever I checked using: echo (file_get_contents("http://localhost/projectName/css/stylesheet.css") ? "read" : "not read"), and it is "read". For example, I have bootstrap in the head section of the site. But as I'm only printing a section of the page DOM at html code, I surround it in a new set with link back to the bootstrap css that I know works using your tester. None of the bootstrap style are recognised though. Eg. cards, me-1, px-2, etc. I'll keep playing around to see if I can resolve, but I'm current workarounding it by putting inline styling into the code that's being sent to dompdf (eg. <\div style="blah blah">. This works but means I can't apply it to the rest of my site, which is ok for now. Question:
Btw. I'm looking forward to your auto-language detection version. That's pretty hectic! When's the release eta? |
No specific release date, and I avoid giving estimates just because I have to fit in the work when I can. That being said, it shouldn't be too long before it's ready I'm trying to finish up a few pending updates. It's entirely possible the Bootstrap styling you're using isn't supported. For example, Dompdf doesn't yet support flexbox or grid so some of the styling from Bootstrap 5 in particular just won't work. Older versions of Bootstrap work OK, though there are still some issues to resolve. Minified CSS shouldn't be a problem. If you can provide a sample document, I'm happy to take a look and see if there's anything that just won't work. |
I've being running some tests and have now resolved most issues, except for one more niggling item. Line-height seems to get hijacked - it's the only css/style that's not getting replicated from stylesheet, inline, or classes. The line-height rendered by dompdf is the default for the font. Is this a known issue? These are from previous version of dompdf: |
The only line-height issue I'm aware of has to do with lines that contain varying size block-level inline element such as images. That's being worked on with #2762. Can't say if that's impacting you without seeing a sample of your HTML. |
Cool.. ok, here are the screenshots and code: Current setupOnscreen
DOMPDF pdf output
TestingHypothesis Experiment 1 Results 1
which DOMPDF pdf'd to this: Experiment 2 Results 2 Experiment 3 Results 3 Conclusion
Hope this helps. |
Hard to say what's going on here. I would need to see a whole lot more of your actual HTML and CSS (instead of the logic that generates it). It's just too hard to debug with what you've provided. Regarding your thought about line height. A font does not have a line height. The line height "specifies the minimum height of line boxes within the element" (mdn). The actual height of a line depends on how it's specified as well as the content of the line. So, while a font does not come with a line height, the height of that font (based on the font size) does impact the height of the line. The sample line height styling you show is in px, which is not recommended. That sets a static line height as opposed to one calculated based on the content. You can see the impact by rendering the css_line_height.html document in the debug helper. |
You've just solved it. You are a freakin genius! Ok, here's the use-case I'm working on. I'm scanning doc using tesseract, then reproducing the elements onscreen overlaid onto the original image in the same positions. Because tesseract and other OCR don't provide font, font-size or line-spacing data, I have to infer everything based on the fixed width and height blocks. eg. if there are two sentences on a block with 30px height, then line-height is 15px. This was working perfectly for the onscreen rendering... but NOT for dompdf rendering. I've replaced line-height px with relative non-unit measure, removed the vertical alignment, and made the spans absolute to top=0 padding=0. Line-height is now controllable and I can put a workaround in place, which mostly works but still not 100% faithful to the oscreen rendering.
Also found this: $options->set('fontHeightRatio', 1.1); is default. That's been a big issue too. I'm still a bit weirded out that:
BIG BIG THANKS :) |
I can't speak as to what you're seeing in the browser vs dompdf without a simple, self-contained, reproducible example so I think we can leave that for now. Per the mdn documentation "Prefer unitless numbers for line-height values." Meaning you should avoid using px or any other unit, just a bare number. It is supported but can produce unexpected results for lines with different font sizes. |
Ok, I'll test it out and see what I can do. I'll let you know the outcome. |
Using
<link href="<?php echo base_url(); ?>css/bootstrap523.min.css" rel="stylesheet" type="text/css" />
<link href="<php echo base_url(); ?>css/customstyle.css" rel="stylesheet" type="text/css" />
Using the following code, I am able to produce a pdf, but it does not pick up css style sheet and only picks up "style: xxxx" tag in the DOM elements. Furthermore, arabic and chinese (non-latin code) is not printed although it appears correctly onscreen.
If I have a translation onscreen with non-Latin characters like arabic or chinese, these disappear completely from the printed pdf, even though they are rendered correctly in the html onscreen.
Attemped fixes:
EDITED.. I updated my code per the inclusion of styling in the html code, and saved to pdf in php, to exclude any issues occurring with front-end JS saving to pdf.
The text was updated successfully, but these errors were encountered: