banner



How Do I Upload An Html File To Dev.to

Cover image for How to turn HTML webpage into an Image?

Jasmin Virdi

How to plough HTML webpage into an Epitome?

While working in one of my project I had to implement a feature where I have plough an HTML webpage to an Paradigm. The first thought that occurred to me was to utilise an inbuilt library simply like dom-to-image or using Chrome Headless or a wrapper library similar Puppeteer. While working I came across this technique using pure Javascript.

Allow's endeavour to achieve this without using any library.

Converting HTML webpage into an Image past using Sail.

We cannot directly describe the HTML into Canvas due to the security reasons. We will follow some other arroyo which volition exist safer.

Steps

  • Create SVG Paradigm that will incorporate the rendering content.
                              <svg                xmlns=                "http://www.w3.org/2000/svg"                width=                "200"                height=                "200"                >                </svg>                          

Enter fullscreen style Exit fullscreen mode

  • Insert a <foreignObject> element inside the SVG which volition contain the HTML.
                              <svg                xmlns=                "http://www.w3.org/2000/svg"                width=                "200"                meridian=                "200"                >' +                <foreignObject                width=                "100%"                height=                "100%"                >                </foreignObject>                </svg>                          

Enter fullscreen mode Exit fullscreen mode

  • Add the XHTML content inside the <foreignObject> node.
                              <svg                xmlns=                "http://www.w3.org/2000/svg"                width=                "200"                summit=                "200"                >' +                <foreignObject                width=                "100%"                height=                "100%"                >                <div                xmlns=                "http://www.w3.org/1999/xhtml"                >.                <manner>em                {                color                :                cherry                ;}                </way>                Hey there...                </div>                </foreignObject>                </svg>                          

Enter fullscreen mode Exit fullscreen fashion

  • Create the paradigm object and set the src of an image to the data url of the paradigm.
                              const                tempImg                =                document                .                createElement                (                '                img                '                )                tempImg                .                src                =                '                data:image/svg+xml,                '                +                encodeURIComponent                (                '                <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><foreignObject width="100%" height="100%"><div xmlns="http://world wide web.w3.org/1999/xhtml">Hey there...</div></foreignObject></svg>                '                )                          

Enter fullscreen mode Get out fullscreen way

  • Depict this Image onto Canvas and set canvas information to target img.src.
                              const                newImg                =                document                .                createElement                (                '                img                '                )                newImg                .                src                =                '                data:epitome/svg+xml,                '                +                encodeURIComponent                (                '                <svg xmlns="http://www.w3.org/2000/svg" width="100" peak="100"><foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml">Hey there...</div></foreignObject></svg>                '                )                // add consequence listener to image.                newImg                .                addEventListener                (                '                load                '                ,                onNewImageLoad                )                // method to draw image to sheet and gear up data to target img.src                part                onNewImageLoad                (                eastward                ){                ctx                .                drawImage                (                e                .                target                ,                0                ,                0                )                targetImg                .                src                =                canvass                .                toDataURL                ()                }                          

Enter fullscreen mode Exit fullscreen manner

You can observe the consummate lawmaking in the CodeSandox!

Reasons why using SVG and Canvass is safety?

Implementation of SVG image is very restrictive as we don't allow SVG prototype to load an external resource fifty-fifty ane that appears on the aforementioned domain. Scripting in an SVG prototype is not allowed, there is no way to access the DOM of an SVG image from other scripts, and DOM elements in SVG images cannot receive input events. Thus there is no manner to load privileged information into a form control (such every bit a full path in a <input blazon="file">) and render information technology.

The restriction that script tin can't directly touch DOM nodes that get rendered to the sheet is important from the security point of view.

I tried to cover this topic and steps in cursory. Please experience gratuitous to add together on related to this topic. 😅

Happy Learning!👩‍💻

Source: https://dev.to/jasmin/how-to-turn-html-webpage-into-an-image-n1c

Posted by: herrerarialf1948.blogspot.com

0 Response to "How Do I Upload An Html File To Dev.to"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel