Embedding Bitmaps and Images
To ensure that HTML content does not require an internet connection to retrieve data, images can be embedded into HTML as base64. Embedding an image in HTML as base64 requires loading the image and converting its information to base64.
The code above uses the fs.readFile
function to read the image file asynchronously. Inside the file reading callback, it converts the binary data from the image file into a binary string using a loop. This binary string is then converted into a base64-encoded string using the btoa
function in JavaScript.
Construct an HTML img
element with the src
attribute set to the base64-encoded image data. Afterward, utilize IronPDF's PdfDocument.fromHtml
method to render the HTML content, including the embedded image, and generates a PDF document.
Finally, save the resulting PDF.