PHP - Dynamically creating a svg with embedded fonts - php

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.

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 (?))

WP check specific class added to html tag

In Wordpress is there any way to check if any specific class added to html tag of document? More Likely Modernzir (browser based) classes for enqueuing scripts etc.
<html class="queryselector svg cssall willchange audio canvas video adownload bgpositionshorthand cssremunit rgba csscalc cssgradients csschunit supports no-touchevents **no-cssscrollbar** cssvhunit cssvwunit formvalidation cssanimations appearance no-backdropfilter backgroundcliptext csscolumns csscolumns-width no-csscolumns-span csscolumns-fill csscolumns-gap csscolumns-rule csscolumns-rulecolor csscolumns-rulestyle csscolumns-rulewidth no-csscolumns-breakbefore no-csscolumns-breakafter no-csscolumns-breakinside no-cssgridlegacy cssgrid cssfilters flexbox shapes csstransforms backgroundblendmode fa-events-icons-ready wf-opensans-n4-active wf-active" lang="en-US">
For example if you check above code block, you can see that modernizr has added multiple classes based on browser compatibility. So i simply want to check if <html> has any specific class, for instance no-cssscrollbar to check if browser supports CSS3 scroll bar.
I tried different approaches including php class_exists() function, but no luck so far. I want to detect class without JS or jQuery approach.

update/modify svg img tag from php file

I have SVG version of USA map, and wanted to modify each state there, however, the SVG file was attribute to img tag.
Code Fig. 1:
<img src="https://www.domain.com/path/to/svg/usa-map.svg" alt="">
Each State (svg path) from usa-map.svg will need to update from the admin, where it should be highlighted from the output where if the State (svg path) have property location. Shown in Example.
Image Fig. 1:
Note: Dark colored States have property locations
Case:
Previously in our code, we've managed to use direct SVG in our code and update the State (svg path) via admin using jQuery css to highlight certain State (svg path).
Code Fig. 2:
<script>$("{{$california_state}}").css({ fill: "#9DC75B" });</script>
We decide to use the img tag instead of full raw svg element to reduce weight of the file and speed-up the SEO performance.
Question:
Is it possible to update/modify the SVG path (while it was viewable in img tag) via internal PHP file where this internal file calling the SVG element and inject the css to highlight certain states using jQuery script using our method or is there better alternative solutions for this?
Maybe using webserver rewrites to an svg file path wich originally is an php file that opens the real svg and modifies it could be an solution.
So you could still include the image over an img tag.

binding font files with PDF document in TCPDF

Im scripting to generate a PDF file using TCPDF library, all I want now is to add font files into the document, I could add them using "SetFont" like
$pdf->SetFont('myfont', '', 10);
and its when I execute this script, the document in browser is rendering fonts perfectly, if I save it and open in another machine then fonts are working with, default fallback font is applying.
I did check it by going to File->Properties->Fonts in Adobe reader, font file is listed there, but text is missing the font.
Any idea will be helpful.
You will probably want to embed the font using $pdf->AddFont().
A longer description of the differences between SetFont(), AddFont() and AddTTFFont() here:
TCPDF, "Could not include font definition file" with OpenType fonts

Using Javascript and PHP to generate images using a TTF

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!

Categories