Odoo and WkHtmlToPDF: issue with fonts - css

After googling several hours, I still cannot have custom fonts displayed in PDF generated by Odoo. Help much appreciated, please! Here is the complete story.
I created a custom report inheriting account.report_invoice_document. This is needed as invoices are customized to display extra information (like a Harmonized System Codes summary, for example). I was also asked to use a custom font to customize the layout of our invoices.
In this template, I added an inline style sheet and applied a font-face to the whole document:
<style>
#font-face {
font-family: 'My Font';
font-weight: normal;
font-style: normal;
src: url(data:font/opentype;charset=utf-8;base64,d09GRg<!--[ LONG BASE64 STRING ]-->DN6ag4) format('woff'),
url(data:font/truetype;charset=utf-8;base64,AAEAAA<!--[ LONG BASE64 STRING ]-->moOA==) format('truetype');
}
html, body
{
font-size: 8;
font-family: 'My Font';
}
</style>
Tuning the Invoices report to produce HTML, I'm able to get the expected results on screen in my browser (given the font is not installed on my computer running the browser). Pressing the Print button export the same report as a PDF file through wkhtmltopdf I get my invoice with the correct data, but not the right font family nor size.
Things I tested, without success:
use only WOFF
use only TTF
use both WOFF and TTF
direct PDF generation without primarily HTML display on screen
add a .mytest class in style an apply it to specific html tags
Still the same: OK on screen, not as expected as PDF file.
For experimentation, also tried to include Material Icon as follow:
<style>
#font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url('data:font/truetype;charset=utf-8;base64,AAEA<!--[ LONG BASE64 STRING ]-->ABc=') format('truetype');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}
</style>
and later adding this somewhere in the template.
<span>TEST <i class="material-icons"></i></span>
Same as above: works well on screen and displays "TEST" + a smiling face icon, works partially on PDF and only displays TEST without the face icon.
I'm stuck there. Could someone help?
Thanks.

Related

To show characters "ă" and "â" in my website from Poppins font, I need to load 2 different font files. How can I load only one single file?

In order to show the character ă în my website, I need to load from google fonts the next font: https://fonts.gstatic.com/s/poppins/v20/pxiEyp8kv8JHgFVrJJnecmNE.woff2
To show the character â in my website, I need to load from google fonts the next font: https://fonts.gstatic.com/s/poppins/v20/pxiEyp8kv8JHgFVrJJfecg.woff2
Here you can see what I'm talking about:
For ă:
<style>
#font-face {
font-family: 'Poppins';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/poppins/v20/pxiEyp8kv8JHgFVrJJnecmNE.woff2) format('woff2');
}
h1{
font-family:Poppins;
font-size:46px
}
</style>
<h1>ă â</h1>
https://jsfiddle.net/y75on1fb/
For ă:
<style>
#font-face {
font-family: 'Poppins';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/poppins/v20/pxiEyp8kv8JHgFVrJJfecg.woff2) format('woff2');
}
h1{
font-family:Poppins;
font-size:46px
}
</style>
<h1>ă â</h1>
https://jsfiddle.net/z3vn9cx1/
Not sure why is this happening, but what should I do to load only one font file ?
Not sure where you got the gstatic URLs, but those appear to be locators for cached, character-subsetted variants of the Poppins Reglar font (or, maybe actually the Poppins Bold font). If you go to the Google Fonts page for Poppins and select Regular, they'll provide you with the specific lines of HTML or CSS they recommend you use to use the font in your site. For one way of doing this, use this in your HTML and CSS:
<style>
#import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');
</style>
h1{
font-family:Poppins;
font-size:46px
}
If you want to use a subsetted font to optimize load times, you can use the Google Font API to indicate text that needs to be supported and then include both those characters (URL-encoded) in the text:
<style>
#import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap&text=%c4%83%c3%a2');
</style>
See https://developers.google.com/fonts/docs/getting_started or
https://developers.google.com/fonts/docs/css2 for more info on the API.

Why different font are used by Safari on iPad and Mac

When I am loading our website in Safari (need Github authentication) : https://app.alpha-1.cloud.responsive.software/team/633ebdae942ada36567750b0
Look at Responsive Test
Have these settings:
.breadcrumb-link {
font-family: SFPro-Medium;
font-size: 22px;
color: #c14953;
}
#font-face {
font-family: SFPro-Medium;
src: url(/fonts/SF-Pro.ttf);
font-weight: medium;
font-display: swap;
}
Mac:
iPad:
Try to be as specific as possible:
specify the font format also (so "truetype")
specify the font-weight also in your css class
Different safari/browser versions can be more or less "forgiving" when it comes to font-face rules.
css
#font-face {
font-family: SFPro-Medium;
src: url(/fonts/SF-Pro.ttf) format('truetype');
font-weight: medium;
font-display: swap;
}
.breadcrumb-link {
font-family: SFPro-Medium;
font-size: 22px;
color: #c14953;
font-weight: medium;
}
Common issues:
Make sure your font is actually loaded via #font-face by checking the devtools network tab. You might actually see a local version in the first screenshot.
Try numeric font-weight values like 600 instead of medium.
Search for overriding css rules.

Change default font in Safari with CSS

I want to use a custom font on safari to increase readability, but Safari doesn't seem to have an easy way to change fonts. I tried to use Safari's support for custom CSS stylesheets but the font still isn't rendering, what am I doing wrong with this?
#font-face {
font-family: 'Comic Code Ligatures';
font-style: normal;
font-weight: normal;
src: local('Comic Code Ligatures'), url('./Comic Code Ligatures.otf') format('opentype');
}
body {
font-family: 'Comic Code Ligatures';
font-size: 16px;
}

Css font-face using ttc file

This is my first time using font-face, and it's really hard for me to actually render the exact font, especially when it comes to *.ttc files.
Here is what I've done so far:
#font-face {
font-family: 'FontName';
src: url('../fonts/font.ttc') format('truetype');
font-weight: normal;
}
.header {
font-family: 'FontName;
}
When I check the network tab in Chrome's inpector, I can see that the font is loaded successfully, but the result text still uses another font.
What did I do wrong? Please help me to fix this problem. Thanks a lot in advance.
Update
One more thing that I figured out. When I style inline, the font is rendered correctly.
<p style="font-family:'FontName'">test 2</p>
Is there any delay in loading or something like that?
You can't use font collections for CSS #font-face declarations as the purpose of this syntax is to, unambiguously, specify which single font resource must be used by the browser when you specify some specific combination of font-{family, weight, style, etc} in your actual page CSS.
Specifying a font collection makes this impossible: there is no syntax to specify which font inside that collection you would need, so ttc are not supported by design. Extract the individual font assets you need (if legally allowed!) and then be explicit about which single font you need for which single #font-face declaration.
And remember that this is possible:
#font-face {
font-family: myfont;
font-weight: normal;
src: url('that-font-I-like-Regular.woff') format('WOFF');
}
#font-face {
font-family: myfont;
font-weight: bold;
src: url('that-font-I-like-Regular.woff') format('WOFF');
}
...
:root {
font-family: myfont;
}
h1 {
font-weight: normal; /* will use that-font-I-like-Regular.woff */
...
}
p {
font-weight: bold; /* will _also_ use that-font-I-like-Regular.woff */
...
}

Custom font is displayed weird

From the start I need to say that I know what I'm trying to do is not "the right way to do it", but the client I'm working for desperately wants THIS specific font.
So, I need to use on a client's website the exact font as VOGUE uses. So I took the .eot & .ttf and uploaded them on my server. Then I added the CSS definitions:
/*fonts fonts for IE*/
#font-face {
font-family: VogueDidot;
src: url('font/FBDidotL-Regular.eot') format('embedded-opentype');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: "VogueDidot Light";
src: url('font/FBDidotL-Light.eot') format('embedded-opentype');
font-weight: normal;
font-style: normal;
}
/*fonts for other browsers*/
#font-face {
font-family: VogueDidot;
src: url('font/FBDidotL-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: "VogueDidot Light";
src: url('font/FBDidotL-Light.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
And the CSS for my element is:
.post h1 {
display: block;
height: 100%;
font-family: VogueDidot;
font-size: 55px;
text-transform: uppercase;
overflow: hidden;
line-height: 58px;
}
And, normally, I expected to see everything working like a charm.
But it's not...
Here's how it should look like:
https://lh.rs/8M9Q7EvRBapv
And here's my version :
https://lh.rs/Lbini5YbQZlX
Any ideas?
It's important to note that using custom fonts are not pixel perfect on all browsers since all browsers tend to render the fonts differently. Another issue can be people not enabling ClearType within windows which I'm sure in this case its not but for other readers I decided to include this information.
It would seem that your font support is pretty limited since you are only using .ttf and .eot - for maximum compatibly and limiting render issues it's best to use 'ALL' 5 font types which are:
ttf (TrueType Font)
oft (Adobe/Microsoft Open Font Type, AKA OpenType)
eot (Embedded OpenType)
woff (Web Open Font Format)
svg (Scalable Vector Graphics)
Ideally you want to use SVG as much as you can since this provides the best quality, SVG is supported in modern versions of Andriod Browser, Firefix, Safari, Opera, Chrome but not in IE.
Personally I'd convert the fonts to all these file types and see if the outcome improves, browsers will automatically use their preferred font type. You can use Font 2 Web to convert to the other formats, its important to note that fonts are copyrighted and your client will require licensing, some fonts don't even allow web use.

Resources