<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Madhu Gupta</title>
    <description>The latest articles on DEV Community by Madhu Gupta (@madhu_gupta).</description>
    <link>https://dev.to/madhu_gupta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2058333%2Fefa035e0-56f2-4f1a-96bd-3a7be14cbf55.jpg</url>
      <title>DEV Community: Madhu Gupta</title>
      <link>https://dev.to/madhu_gupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/madhu_gupta"/>
    <language>en</language>
    <item>
      <title>Building a Web Page Generator with Next.js, tldraw, and OpenAI</title>
      <dc:creator>Madhu Gupta</dc:creator>
      <pubDate>Mon, 14 Oct 2024 13:16:43 +0000</pubDate>
      <link>https://dev.to/madhu_gupta/building-a-web-page-generator-with-nextjs-tldraw-and-openai-14i7</link>
      <guid>https://dev.to/madhu_gupta/building-a-web-page-generator-with-nextjs-tldraw-and-openai-14i7</guid>
      <description>&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;Steps to Build the App&lt;br&gt;
&lt;strong&gt;Step 1: Setting up the tldraw Editor&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn44toyz1toaiotm3v0g6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn44toyz1toaiotm3v0g6.png" alt="Install tldraw" width="800" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f5m4t2s37viuibor23m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f5m4t2s37viuibor23m.png" alt="Add tldraw editor" width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, users can now draw their web page design using the editor's interactive canvas.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Converting the Drawing to a Base64 Image&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;Here’s how I handled this conversion:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdq2j05wmkys719sb6sq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdq2j05wmkys719sb6sq.png" alt="Convert svg image to base64 " width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Sending the Base64 Image to OpenAI for HTML Conversion&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;Here’s how I did it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxxd74r15jel0ttn4ftn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxxd74r15jel0ttn4ftn.png" alt="Send image to openAI thorough API call " width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Parsing and Displaying the Generated HTML&lt;/strong&gt;&lt;br&gt;
The next step is to parse the returned HTML from the &amp;lt;!doctype&amp;gt; declaration to the closing &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
