SOunit / receipt-memo

1 stars 0 forks source link

hot to import font file without http #1

Open SOunit opened 2 months ago

SOunit commented 2 months ago

requirement

solution

SOunit commented 2 months ago

import font to html using Base64 string

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:

  1. Convert the Font File to Base64:

    • Use a tool or online service to convert your font file (.woff, .ttf, etc.) into a Base64 encoded string. There are several online converters available, such as Base64-encode.net.
  2. Embed the Font in Your HTML:

    • Once you have the Base64 encoded string, you can embed it in your HTML file within a <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.

Example with Base64 Encoding Tool:

  1. Convert the Font File:

    • Upload your font file to Base64-encode.net or use a local tool to convert it to Base64.
    • Copy the resulting Base64 string.
  2. Embed in HTML:

    • Paste the Base64 string in place of <YOUR_BASE64_ENCODED_FONT> in the HTML example above.

Note:

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.

SOunit commented 2 months ago

what is base64?

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.

How Base64 Works

Base64 Alphabet

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

Example

Let's take an example to illustrate how Base64 encoding works:

  1. Binary Data: Suppose we want to encode the ASCII string "Hello".

    • ASCII representation: H = 72, e = 101, l = 108, l = 108, o = 111
    • Binary representation: 01001000 01100101 01101100 01101100 01101111
  2. Grouping and Padding:

    • Group the binary data into 24-bit chunks: 01001000 01100101 01101100 01101100 01101111
    • Divide into 6-bit groups: 010010 000110 010101 101100 011011 001101 111000
    • Map each 6-bit group to a Base64 character:
      • 010010S
      • 000110G
      • 010101V
      • 101100s
      • 011011b
      • 001101N
      • 1110004
    • The result is: SGVsbG8
  3. Padding:

    • Add padding (=) if needed to make the length a multiple of 4.
    • "Hello" encoded in Base64 is: SGVsbG8=

Applications of Base64

Advantages and Disadvantages

Advantages:

Disadvantages:

Conclusion

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.