Skip to main content

Html Examples

Passing in HTML is the easiest, most flexible, and fastest method for generating a document. But, unfortunately, it can be the most confusing and technically challenging. In this section we provide some examples to make it easier to begin using this powerful feature.

By using HTML, you have complete control of the content. For example, you can embed images, CSS styles, fonts, and more. In addition, publicly accessible resources, such as images, and fonts from CDNs, can be used and get fetched on rendering.

Html Encoding

Html must get encoded as a base64 string before being passed into the JSON request. By doing this, it ensures that there are no encoding issues with the JSON. For example, in many cases passing in raw HTML into a JSON string, you will encounter problems such as double quotes getting used. By base64 encoding, all of these issues get resolved, which ensures no issues with parsing the JSON payload and the HTML you pass into the service getting corrupted.

HTML
<html>
<body>
<h1>Hello world!</h1>
</body>
</html>
JSON Payload
{ "html": "PGh0bWw+PGJvZHk+PGgxPkhlbGxvIHdvcmxkITwvaDE+PC9ib2R5PjwvaHRtbD4=" }
tip

If you are using VSCode you can download the vscode-base64 extension to help you encode/decode your HTML while you develop.

Json Request

Viewport Example
{
"html": "PGh0bWw+PGJvZHk+PGgxPkhlbGxvIHdvcmxkITwvaDE+PC9ib2R5PjwvaHRtbD4="
}

Pdf Example

cURL Example
curl --request POST \
--url https://api.cloudlayer.io/v2/html/pdf \
--header 'Content-Type: application/json' \
--header 'x-api-key: <YOUR-API-KEY>' \
--data '{
"html": "PGh0bWw+PGJvZHk+PGgxPkhlbGxvIHdvcmxkITwvaDE+PC9ib2R5PjwvaHRtbD4="
}' \
--output "response.json"

Image Example

cURL Example
curl --request POST \
--url https://api.cloudlayer.io/v2/html/image \
--header 'Content-Type: application/json' \
--header 'x-api-key: <YOUR-API-KEY>' \
--data '{
"html": "PGh0bWw+PGJvZHk+PGgxPkhlbGxvIHdvcmxkITwvaDE+PC9ib2R5PjwvaHRtbD4="
}' \
--output "response.png"

Embedding Images

In some cases, you may want to embed images into your HTML so that your HTML page is fully self-contained and does not require any external resources. Fortunately, this is easily achievable since the HTML spec supports embedding images as base64 strings in the src parameter of the img element tag.

The following image will be embedded into the html.



<html>
<body style="margin: 10; background-color: #4E279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jM+IhnqhsYJg6nx0p6x6ARaDTwhJGS3+oPIqQqnAuiUBYFXQdGkfGQ3fUwkJCkgpABLq1LMe+QqssbFbnQy/Qck4IGkb0tfa2CKbYTmoRiEPj4PBojH2DiikUg5bmUuEoTlnMKHU+X3WD0g20CFr7kiEdNWHIa4M4JwFx1Iv5DieoijklBqXkEd7X4qpCPFLW8Ykuhz1aLU86e1LvCWJ0wyROi0HtnvzksPKrLFwQgwwGUXI5GDaIMzVMOuoUeFoVXBCBODacEEegWz0ggWuDebWzKqgTgzpGEO8I9MtQ+4dO/ic5NJmvncqhBRYKBnOXVYfgUxvIWekua/ci0dlz6LZuFRP+t3PINhYVcHjInNTdtUxt4o60lklRg1HLWmVMtatltqHZwNLHrfaFSrZ88Ca7+lPt2z053lPtu0fKejY6uA/9sFLP+IG+Dx2QEyO9Dx33oGNo3xgfdafOWcJMaeyaFALy3mO/SUWnCHSpIoot217DUxq/zw+11Zc7Tt+p+/vqEJQ8VTgPH0sC3gP+6ujJk0zmDtS7bE8QRY7eZc0yF7sjhuW8mp3GFcnL9a5ZheDsy3XndPbavVx9+PawnDH9qtKr9Ew3pnLJlbd9VRQjt4eRVRPKRbu3/ct8flBhn1+uodLwz03mXB/dj5v/dj8OPKr70ehP22N3P4Zkf0jcQROSSHC1g7Y3wP48GliWG+4FicyyJGDaFM/xkjjOl8XQWR7NlcpIvquKoHn7TDJc7TH+A9HgAM7jYxmJAAAAAElFTkSuQmCC"
/>
<div
style="
margin-left: 10px;
margin-top: -6px;
font-size: 24px;
color: #fff;
font-weight: 500;
font-family: sans-serif;
"
>
cloudlayer.io
</div>
</div>
</body>
</html>

Once you have a working set of HTML, you will follow the same procedures, as usual, to pass a base64 encoded HTML string into the request object. Having a base64 string within another base64 string is perfectly acceptable.

{
"html": "PGh0bWw+CiAgPGJvZHkgc3R5bGU9Im1hcmdpbjogMTA7IGJhY2tncm91bmQtY29sb3I6ICM0RTI3OUM7Ij4KICAgIDxkaXYgc3R5bGU9ImRpc3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M3hnZmRhZk9XY0pNYWV5YUZBTHkzbU8vU1VXbkNIU3BJb290MjE3RFV4cS96dysxMVpjN1R0K3ArL3ZxRUpROFZUZ1BIMHNDM2dQKzZ1akprMHptRHRTN2JFOFFSWTdlWmMweUY3c2podVc4bXAzR0Zjbkw5YTVaaGVEc3kzWG5kUGJhdlZ4OStQYXduREg5cXRLcjlFdzNwbkxKbGJkOVZSUWp0NGVSVlJQS1JidTMvY3Q4ZmxCaG4xK3VvZEx3ejAzbVhCL2RqNXYvZGo4T1BLcjcwZWhQMjJOM1A0WmtmMGpjUVJPU1NIQzFnN1kzd1A0OEdsaVdHKzRGaWN5eUpHRGFGTS94a2pqT2w4WFFXUjdObGNwSXZxdUtvSG43VERKYzdUSCtBOUhnQU03all4bUpBQUFBQUVsRlRrU3VRbUNDIgogICAgICAvPgogICAgICA8ZGl2CiAgICAgICAgc3R5bGU9IgogICAgICAgICAgbWFyZ2luLWxlZnQ6IDEwcHg7CiAgICAgICAgICBtYXJnaW4tdG9wOiAtNnB4OwogICAgICAgICAgZm9udC1zaXplOiAyNHB4OwogICAgICAgICAgY29sb3I6ICNmZmY7CiAgICAgICAgICBmb250LXdlaWdodDogNTAwOwogICAgICAgICAgZm9udC1mYW1pbHk6IHNhbnMtc2VyaWY7CiAgICAgICAgIgogICAgICA+CiAgICAgICAgY2xvdWRsYXllci5pbwogICAgICA8L2Rpdj4KICAgIDwvZGl2PgogIDwvYm9keT4KPC9odG1sPg==",
"viewPort": {
"height": 32,
"width": 200
}
}

The output looks like the following.



You can tweak the parameter options to achieve transparency, add text, etc.