Open SOunit opened 2 months ago
To include a font in an HTML file for conversion using the CoreHtmlToImage package without making HTTP requests, you can embed the font directly into your HTML using Base64 encoding. This involves converting the font file into a Base64 string and embedding it within a <style>
tag.
Here are the steps to achieve this:
Convert the Font File to Base64:
Embed the Font in Your HTML:
<style>
tag using the @font-face
rule.Here's an example of how you can do this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
@font-face {
font-family: 'MyCustomFont';
src: url(data:font/woff2;charset=utf-8;base64,<YOUR_BASE64_ENCODED_FONT>) format('woff2');
/* Adjust the format according to your font type: 'woff', 'ttf', etc. */
}
body {
font-family: 'MyCustomFont', sans-serif;
}
</style>
<title>Embedded Font Example</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph using the custom embedded font.</p>
</body>
</html>
Replace <YOUR_BASE64_ENCODED_FONT>
with the actual Base64 encoded string of your font file.
Convert the Font File:
Embed in HTML:
<YOUR_BASE64_ENCODED_FONT>
in the HTML example above.Ensure your HTML file encoding is UTF-8 to avoid any character encoding issues. This method embeds the font directly within the HTML, allowing it to be used without external HTTP requests, making it suitable for server-side HTML to image conversion.
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It is commonly used to encode data that needs to be stored and transferred over media designed to handle text. This includes embedding image data within HTML or CSS files, as well as encoding other types of files for storage in text formats such as JSON or XML.
=
characters to make the Base64 output length a multiple of 4.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
Let's take an example to illustrate how Base64 encoding works:
Binary Data: Suppose we want to encode the ASCII string "Hello".
H
= 72, e
= 101, l
= 108, l
= 108, o
= 11101001000 01100101 01101100 01101100 01101111
Grouping and Padding:
01001000 01100101 01101100 01101100 01101111
010010 000110 010101 101100 011011 001101 111000
010010
→ S
000110
→ G
010101
→ V
101100
→ s
011011
→ b
001101
→ N
111000
→ 4
SGVsbG8
Padding:
=
) if needed to make the length a multiple of 4.SGVsbG8=
<img src="data:image/png;base64,...">
@font-face { src: url(data:font/woff2;base64,...); }
Advantages:
Disadvantages:
Base64 is a useful encoding scheme that converts binary data into a text format, making it suitable for embedding files within HTML, CSS, JSON, and other text-based formats. It is widely used for ensuring compatibility and safe transmission of binary data over text-based protocols and storage systems.
requirement
CoreHtmlToImage
to convert html to image inside serversolution