DEV Community

Cover image for Building a Web Page Generator with Next.js, tldraw, and OpenAI
Madhu Gupta
Madhu Gupta

Posted on

Building a Web Page Generator with Next.js, tldraw, and OpenAI

In this blog post, I'll walk you through how I built a web page generator using Next.js, tldraw, and OpenAI. This tool allows users to visually design a web page by drawing an image, which is then converted into an HTML web page.

Steps to Build the App
Step 1: Setting up the tldraw Editor
The first step was to install and integrate tldraw, a powerful and simple drawing editor, into the Next.js application. This editor allows users to sketch or draw the structure of their desired web page.

Install tldraw

Once installed, I integrated the tldraw editor into a web page in my Next.js app. Here's a basic setup for the editor:

Add tldraw editor

With this, users can now draw their web page design using the editor's interactive canvas.

Step 2: Converting the Drawing to a Base64 Image
After the user completes their drawing, the next step is to take the SVG image generated by tldraw and convert it to a format that can be sent to OpenAI for interpretation. I first converted the SVG to JPG and then to a base64-encoded image to streamline the process.

Here’s how I handled this conversion:

Convert svg image to base64

This code captures the drawing as an SVG, converts it into a canvas, and then converts it to a base64-encoded JPEG image. Once the image is ready, it's time to send it to OpenAI.

Step 3: Sending the Base64 Image to OpenAI for HTML Conversion
Using the OpenAI API, I sent the base64 image along with instructions to generate HTML code representing the drawing. This call leverages the power of OpenAI to interpret visual data and return code.

Here’s how I did it:

Send image to openAI thorough API call

This sends the base64 image to the backend, where OpenAI processes it and returns HTML code. The response contains the HTML for the web page based on the design.

Step 4: Parsing and Displaying the Generated HTML
The next step is to parse the returned HTML from the <!doctype> declaration to the closing

Top comments (0)