I was looking for a way to create images (think screenshots) of CSS layouts that I can use in HTML, EPUB and PDF files. This blog post describes my solution – which produces SVG images.
satori
uses JSX to produce SVGshtml-to-svg
converts DOM nodes to SVGs
Both are useful, but the latter library works much better for my use case.
I use html-to-svg
as follows (check out my solution online): A file can contain zero or more sections with CSS layout demos. This is an example of such a section:
<h2><code>box-sizing</code></h2>
<section id="css-box-sizing">
<style>
.outer {
...
}
</style>
<div class="outer">
...
</div>
</section>
JavaScript code iterates over the sections and adds a <details>
element:
download
attribute of the link.href
of the link is set up like this:const fileBlob = new Blob(
[svgElement.outerHTML], { type: 'text/plain' }
);
link.href = URL.createObjectURL(fileBlob);